java Spring Boot 不支持的媒体类型
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/43613091/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Spring Boot Unsupported Media Type
提问by Andrea Limoli
I am building some APIwith Spring Boot, but I get some errors about Content-Type when I try to query with Postman.
我正在使用Spring Boot构建一些API,但是当我尝试使用 Postman 进行查询时,我收到了一些关于 Content-Type 的错误。
@RequestMapping(path = "/verify", method = RequestMethod.POST, consumes = "text/xml", produces = "application/json")
String verify(@RequestBody Map<String, Object> payload, HttpServletRequest request) {}
I don't understand where there is the problem.
我不明白哪里有问题。
I noticed that the error disappears when I remove @RequestBody as parameter of method. Why?
我注意到当我删除 @RequestBody 作为方法的参数时错误消失了。为什么?
I would simply:
我只想:
- send XML to API
- receive JSON from API
- 将 XML 发送到 API
- 从 API 接收 JSON
回答by Moshe Arad
If you're getting and Error related to Content-Type
, then I surely assume that you use HTTP RESTas mean to communicate between your components.
如果您遇到和 Error 相关的错误Content-Type
,那么我肯定会假设您使用HTTP REST作为在组件之间进行通信的手段。
The Content-Type
is related to Content Negotiationtopic within HTTP REST.
这Content-Type
与HTTP REST 中的内容协商主题相关。
Content Negotiationtopic within HTTP RESTmeans that clients and services must agree on representation media type. i.e they need to agree how to communicate with each other, what will be the content of each payload that is sent and received between the parties.
HTTP REST 中的内容协商主题意味着客户端和服务必须就表示媒体类型达成一致。即他们需要就如何相互通信达成一致,双方之间发送和接收的每个有效载荷的内容是什么。
Client specifies what it wants through Accept
header
客户端通过header指定它想要的东西Accept
Server specifies what produces through Content-Type
header
服务器通过标头指定产生什么Content-Type
回答by Ashutosh
Add following line in your application.properties:
在 application.properties 中添加以下行:
logging.file = myapp.log
logging.file = myapp.log
Restart your server, make a request. Check myapp.log to find what is failing. For me, I got this error:
重新启动您的服务器,发出请求。检查 myapp.log 以查找失败的内容。对我来说,我收到了这个错误:
2018-05-08 07:08:34.404 WARN 348 --- [http-nio-8080-exec-5] .c.j.MappingHymanson2HttpMessageConverter : Failed to evaluate Hymanson deserialization for type [[simple type, class com.atta.entity.User]]: com.fasterxml.Hymanson.databind.exc.InvalidDefinitionException: Cannot handle managed/back reference 'defaultReference': back reference type (java.util.List) not compatible with managed type (com.atta.entity.State)
2018-05-08 07:08:34.404 WARN 348 --- [http-nio-8080-exec-5] .cjMappingHymanson2HttpMessageConverter:无法评估类型 [[简单类型,类 com.atta.entity.User] 的 Hymanson 反序列化]:com.fasterxml.Hymanson.databind.exc.InvalidDefinitionException:无法处理托管/返回引用“defaultReference”:返回引用类型(java.util.List)与托管类型(com.atta.entity.State)不兼容
回答by andreim
Try adding Hymanson-dataformat-xml
dependency to your classpath or if you are using a build tool to maven (pom.xml) or gradle (build.gradle).
尝试将Hymanson-dataformat-xml
依赖项添加到您的类路径,或者如果您使用的是 maven (pom.xml) 或 gradle (build.gradle) 的构建工具。
NOTE: You can support both text/xml
and application/xml
by doing ... consumes = { "text/xml", "application/xml" } ...
.
注意:您可以同时支持text/xml
和application/xml
做... consumes = { "text/xml", "application/xml" } ...
。
NOTE:Instead of strings you can use MediaTypemembers: ... consumes = { MediaType.APPLICATION_XML, MediaType.TEXT_XML } ...
注意:您可以使用MediaType成员代替字符串:... consumes = { MediaType.APPLICATION_XML, MediaType.TEXT_XML } ...
回答by Athula
Change
改变
1) value of header
of postman request to application/json
1)header
邮递员请求的值application/json
2) from
2)从
consumes = "text/xml"
to
到
"application/json"
回答by Praneeth Ramesh
Try setting the Http Header Content-Type to text/xml in your postman. You can also check what is the Content-Type header going currently in Chrome DevTools -> Network Tab -> Click on your request -> Headers Tab.
尝试在邮递员中将 Http Header Content-Type 设置为 text/xml。您还可以在 Chrome DevTools -> Network Tab -> 单击您的请求 -> Headers Tab 中检查当前的 Content-Type 标头是什么。