java - 我想通过 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 字符串外部使用邮件正文。
解决方案
推荐阅读
- ruby-on-rails - 迁移变化
- python - 具有几乎相同代码的两个图形导致 matplotlib 中不同大小的饼图
- python - 如何将 Python IDE 中的串行输出数据转换为 C 中的整数?
- windows - 从 .bat 文件中关闭打开的 cmd 窗口打开应用程序并执行文件
- api - Vue警告]:渲染错误:“TypeError:无法读取未定义的属性'map'”Vue Google maps
- .net - ADAL 到 MSAL 迁移保护 Web API
- c++ - C++中的嵌套链表
- wpf - 更改标题栏的高度
- python - Python中的RegEx字符串模式匹配
- python - 如何在动画期间更新子图的颜色条?