简体   繁体   中英

Spring Boot Unsupported Media Type

I am building some API with Spring Boot , but I get some errors about Content-Type when I try to query with Postman.

@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?

I would simply:

  • send XML to API
  • receive JSON from API

API的邮递员调用

If you're getting and Error related to Content-Type , then I surely assume that you use HTTP REST as mean to communicate between your components.

The Content-Type is related to Content Negotiation topic within HTTP REST .

Content Negotiation topic within HTTP REST means that clients and services must agree on representation media type. ie 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.

Client specifies what it wants through Accept header

Server specifies what produces through Content-Type header

Try adding jackson-dataformat-xml dependency to your classpath or if you are using a build tool to maven (pom.xml) or gradle (build.gradle).

NOTE : You can support both text/xml and application/xml by doing ... consumes = { "text/xml", "application/xml" } ... .

NOTE: Instead of strings you can use MediaType members: ... consumes = { MediaType.APPLICATION_XML, MediaType.TEXT_XML } ...

Add following line in your application.properties:

logging.file = myapp.log

Restart your server, make a request. Check myapp.log to find what is failing. For me, I got this error:


2018-05-08 07:08:34.404 WARN 348 --- [http-nio-8080-exec-5] .cjMappingJackson2HttpMessageConverter : Failed to evaluate Jackson deserialization for type [[simple type, class com.atta.entity.User]]: com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot handle managed/back reference 'defaultReference': back reference type (java.util.List) not compatible with managed type (com.atta.entity.State)

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.

Change

1) value of header of postman request to application/json

2) from

consumes = "text/xml"

to

"application/json"

In mycase I had incorrect JSON Property mapping which resulted in same exception

@JsonProperty("Total")
private List<XXX> SafeMembers;

I mapped JSON property to Total instead of SafeMembers. I changed as below

@JsonProperty("SafeMembers")
private List<XXX> SafeMembers;

What i did was to remove jackson libray and now I don't longer have that error, spent 2 days trying to figure out this, I hope this works for you!

    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
        <version>2.11.3</version>
        <scope>test</scope>
    </dependency>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM