Java 如何在spring boot中将HTML内容作为JSON值获取
我正在开发一个API,在这里我收到一些文章相关的数据作为POST请求。我拥有的接收器如下:Java 如何在spring boot中将HTML内容作为JSON值获取,java,json,spring-boot,jsonparser,Java,Json,Spring Boot,Jsonparser,我正在开发一个API,在这里我收到一些文章相关的数据作为POST请求。我拥有的接收器如下: @ApiOperation(value = "Add a new Article", produces = "application/json") @RequestMapping(value = "/create", method = RequestMethod.POST) public ResponseEntity createPost(@RequestBody String postContent)
@ApiOperation(value = "Add a new Article", produces = "application/json")
@RequestMapping(value = "/create", method = RequestMethod.POST)
public ResponseEntity createPost(@RequestBody String postContent) {
try {
// Here I need to conver the postContent to a POJO
return new ResponseEntity("Post created successfully", HttpStatus.OK);
} catch (Exception e) {
logger.error(e);
return responseHandler.generateErrorResponseJSON(e.getMessage(),
HttpStatus.INTERNAL_SERVER_ERROR);
}
}
现在它适用于简单的请求,如:
{
"id": "1",
"title": "This is the Post Title",
"body": "This is the Post Body",
"authorName": "Test",
"tagList": [
"tag-1",
"tag-2",
"tag-3"
]
}
但在实际场景中,我将得到一个HTML内容作为请求JSON中“body”键的值,该键可以有“”,也可以有很多内容。然后字符串到JSON的转换将失败。是否有任何api、库或示例,可以将HTML内容作为JSON键的值
以下是我的输入请求,其中代码未能将JSON解析为对象:
{
"menuId": "1",
"title": "This is the Post Title",
"body": "<p style="text-align:justify"><span style="font-size:16px"><strong>Mediator pattern</strong> is a Behavioral Design pattern. It is used to handle complex communications between related Objects, helping by decoupling those objects.</span></p>",
"authorName": "Arpan Das",
"tagList": [
"Core Java",
"Database",
"Collection"
]
}
它给出了一个解析异常:
Unexpected character (t) at position 77.
我用于解析JSON的库是:
<dependency>
<groupId>com.googlecode.json-simple</groupId>
<artifactId>json-simple</artifactId>
<version>1.1.1</version>
</dependency>
com.googlecode.json-simple
简单json
1.1.1
那么字符串到JSON的转换将失败:为什么会失败?你真的认为JSON解析器不能正确地转义/unescape需要的值吗;JSONObject JSONObject=(JSONObject)jsonParser.parse(content);未能解析jsonPost是一个完整的最小示例(包括硬编码的JSON输入),再现了问题。并告诉我们您正在使用哪个库来解析JSON。并发布异常的完整堆栈跟踪。顺便说一句,你为什么不让Spring帮你解析JSON呢?所以你想把“Hello World一些文本”“转换成这个”{“html”:{“head”:{“title”:“Hello World”},“body”:{“p”:“Some text”}}}”?@Rashin不,他想能够解析包含“body”:“一些html\”和\“引号”“
<dependency>
<groupId>com.googlecode.json-simple</groupId>
<artifactId>json-simple</artifactId>
<version>1.1.1</version>
</dependency>