我想通过REST API调用将电子邮件详细信息保存在数据库中

时间:2018-08-31 05:19:30

标签: java json spring rest

@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字符串外的邮件正文。

0 个答案:

没有答案