Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Postman call of API

like image 847
Andrea Limoli Avatar asked Apr 25 '17 14:04

Andrea Limoli


2 Answers

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. 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.

Client specifies what it wants through Accept header

Server specifies what produces through Content-Type header

like image 106
Moshe Arad Avatar answered Oct 01 '22 16:10

Moshe Arad


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] .c.j.MappingJackson2HttpMessageConverter : 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)

like image 36
Ashutosh Avatar answered Oct 01 '22 16:10

Ashutosh