相关疑难解决方法(0)

Jackson 2.0与Spring 3.1

Spring MVC 3.1与Jackson 2.0兼容吗?Spring MVC是否会在类路径上自动检测Jackson,并且使用JSON内容类型的请求委托给Jackson仍有效?

java spring json spring-mvc jackson

28
推荐指数
4
解决办法
5万
查看次数

并非所有内容类型都支持Spring REST Controller内容/类型

我正在尝试创建使用HTML的Web服务,该HTML随后用于与iText创建PDF。

我使用Postman来将请求发送到服务器,但是无论选择哪种内容类型,每次遇到以下错误时,我都可以使用:

{
    "status": "CONFLICT",
    "code": 40902,
    "message": "Content type 'text/plain' not supported",
    "developerMessage": "null",
    "moreInfoUrl": "class org.springframework.web.HttpMediaTypeNotSupportedException",
    "throwable": null
}
Run Code Online (Sandbox Code Playgroud)

消息根据所选内容类型而变化:

"message": "Content type 'text/xml' not supported"
"message": "Content type 'text/html' not supported"
Run Code Online (Sandbox Code Playgroud)

这是我的终点:

@RequestMapping(path = "/reports/pdf", method = RequestMethod.POST, consumes = MediaType.TEXT_HTML_VALUE)
public ResponseEntity<byte[]> generatePdfReport(@RequestBody String html) throws Exception {
    byte[] pdfBytes = reportsService.generatePdf(html);
    ResponseEntity<byte[]> response = new ResponseEntity<byte[]>(pdfBytes, HttpStatus.OK);
    return response;
}
Run Code Online (Sandbox Code Playgroud)

我已将consumes属性更改为:

  • MediaType.TEXT_PLAIN_VALUE
  • MediaType.TEXT_XML_VALUE

匹配我在Postman上发送的内容。

和@dnault注释一样,将其从RequestMapping注释中完全删除,结果相同。

我研究了类似的问题:

java spring spring-mvc media-type postman

4
推荐指数
1
解决办法
1万
查看次数

标签 统计

java ×2

spring ×2

spring-mvc ×2

jackson ×1

json ×1

media-type ×1

postman ×1