首页 > 解决方案 > 我想通过 REST API 调用将电子邮件详细信息保存在数据库中

问题描述

@RequestMapping(value = "/createXXX", method = RequestMethod.POST ,consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public @ResponseBody ResponseEntity<String> createXXX(@Context HttpServletRequest request, HttpSession httpSession, 
            @Valid @RequestParam("json") String jsonString,@Valid @RequestPart("file") MultipartFile[] files)  {

        ObjectMapper mapper = new ObjectMapper();
        AModel aModel = null;
        try {
            aModel = mapper.readValue(jsonString, AModel.class);
        } catch (IOException e) {

        }

其他人在使用我们发布的 REST API,内容类型是 multipart/form-data,API 的使用者会发送一个 JSON 字符串和一些文件附件,JSON 字符串就像

{
    "mail":"xxx.com",
    "mailSubject":"Hi how are you",
    "mailBody":"here we can have anything rich text, html etc which we are generally writing while sending the mails"
}

我在使用这个 mailBody 时遇到问题,因为这很少是正常的字符串,所以 JSON 解析器会出错。

error: {
    "errorCode": 400,
    "status": "BAD_REQUEST",
    "message": "ERR-0001: Required parameters are blank or invalid.",
    "timestamp": "29-08-2018 07:06:17",
    "Errors": [
        {
            "field": "",
            "rejectedValue": "json",
            "message": "Illegal unquoted character ((CTRL-CHAR, code 13)): has to be escaped using backslash to be included in string value\n at [Source: java.io.StringReader@74be551a; line: I, column: 134]"
        }
    ]
}

所以我想更改 API 结构,以便我们可以在 json 字符串内部或 json 字符串外部使用邮件正文。

标签: javajsonspringrest

解决方案


推荐阅读