Spring Rest POST Json RequestBody Content type not supported

JavaJsonSpringSpring MvcJackson

Java Problem Overview


When I try to post new object with post method. RequestBody could not recognize contentType. Spring is already configured and POST could work with others objects, but not this specific one.

org.springframework.web.HttpMediaTypeNotSupportedException: Content type 'application/json;charset=UTF-8' not supported

If I try the same request just changing requestbody object. It works.

Java Solutions


Solution 1 - Java

I found solution. It's was because I had 2 setter with same name but different type.

My class had id property int that I replaced with Integer when à Hibernitify my object.

But apparently, I forgot to remove setters and I had :

/**
 * @param id
 *            the id to set
 */
public void setId(int id) {
    this.id = id;
}

/**
 * @param id
 *            the id to set
 */
public void setId(Integer id) {
    this.id = id;
}

When I removed this setter, rest resquest work very well.

Intead to throw unmarshalling error or reflect class error. Exception HttpMediaTypeNotSupportedException seams really strange here.

I hope this stackoverflow could be help someone else.

SIDE NOTE

You can check your Spring server console for the following error message:

> Failed to evaluate Jackson deserialization for type [simple type, > class your.package.ClassName]: > com.fasterxml.jackson.databind.JsonMappingException: Conflicting > setter definitions for property "propertyname"

Then you can be sure you are dealing with the issue mentioned above.

Solution 2 - Java

Be aware also if you have declared getters and setters for attributes of the parameter which are not sent in the POST (event if they are not declared in the constructor), for example:

@RestController
public class TestController {

    @RequestMapping(value = "/test", method = RequestMethod.POST)
    public String test(@RequestBody BeanTest beanTest) {
        return "Hello " + beanTest.getName();
    }


	public static class BeanTest {
		
		private Long id;
		private String name;
		
		public BeanTest() {
		}
	
		public BeanTest(Long id) {
			this.id = id;
		}
	
		public Long getId() {
			return id;
		}
	
		public void setId(Long id) {
			this.id = id;
		}
	
		public String getName() {
			return name;
		}
	
		public void setName(String name) {
			this.name = name;
		}
	}
}

A post request with the next structure: {"id":"1"} would not work, you must delete name get and set.

Solution 3 - Java

If you use lombok, you can get that error if you screw up naming of @JsonDeserialize, for example:

@Value
@Builder(toBuilder = true)
@JsonDeserialize(builder = SomeClass.SomeOtherClassBuilder.class)
public class SomeClass {
    ...
    public static class SomeOtherClassBuilder {
        ...
    }
}

It should be:

@Value
@Builder(toBuilder = true)
@JsonDeserialize(builder = SomeClass.SomeClassBuilder.class)
public class SomeClass {
    ...
    public static class SomeClassBuilder {
        ...
    }
}

It is very easy to do that when you do refactoring of class name and forget about Builder... and then you have many hours of joy while you search for reason of error, while having as help only extremely unhelpful exception message.

Solution 4 - Java

Really! after spending 4 hours and insane debugging I found this very strange code at com.fasterxml.jackson.databind.deser.DeserializerCache

if (deser == null) {
    try {
        deser = _createAndCacheValueDeserializer(ctxt, factory, type);
    } catch (Exception e) {
        return false;
    }
}

Ya, the problem was double setter.

Solution 5 - Java

I had the same issue when I had two setters one with Enum and one String. I had to use @JsonSetter annotation which tells Jackson what setter method to use during serialization. This solved my issue.

Solution 6 - Java

I met the same problem which i solved by deserializing myself the posted value :

@RequestMapping(value = "/arduinos/commands/{idArduino}", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE)
@ResponseBody
public String sendCommandesJson(@PathVariable("idArduino") String idArduino, HttpServletRequest request) throws IOException {
    // getting the posted value
    String body = CharStreams.toString(request.getReader());
    List<ArduinoCommand> commandes = new ObjectMapper().readValue(body, new TypeReference<List<ArduinoCommand>>() {
    });

with theses gradle dependencies :

  compile('org.springframework.boot:spring-boot-starter-web')
  compile('com.google.guava:guava:16.0.1')

Solution 7 - Java

In my case I had two Constructors in the bean and I had the same error. I have just deleted one of them and now the issue is fixed!

Solution 8 - Java

So I had a similar issue where I had a bean with some overloaded constructor. This bean also had Optional properties.

To resolve that I just removed the overloaded constructors and it worked.

example:

public class Bean{

Optional<String> string;
Optional<AnotherClass> object;

public Bean(Optional<String> str, Optional<AnotherClass> obj){
string = str;
object = obj;
}

///The problem was below constructor

public Bean(Optional<String> str){
string = str;
object = Optional.empty();
}



}

}

Solution 9 - Java

It looks an old thread, but in case someone still struggles, I have solved as Thibaut said it,

Avoid having two setter POJO class, I had two-setters for a specific property , The first one was in the regular setter and another one in under constructor after I removed the one in the constructor it worked.

Solution 10 - Java

specify @JsonProperty in entity class constructor like this.

......
......
......

 @JsonCreator
 public Location(@JsonProperty("sl_no") Long sl_no, 
          @JsonProperty("location")String location,
          @JsonProperty("location_type") String 
          location_type,@JsonProperty("store_sl_no")Long store_sl_no) {
  this.sl_no = sl_no;
  this.location = location;
  this.location_type = location_type;
  this.store_sl_no = store_sl_no;
 } 
.......
.......
.......

Solution 11 - Java

I had the same issue when I used this as a foreign key.

@JsonBackReference
@ManyToOne(cascade = {CascadeType.PERSIST, CascadeType.DETACH},fetch = FetchType.EAGER)
@JoinColumn(name = "user_id")
private User user;

Then I removed @JsonBackReference annotation. After that above issue was fixed.

Solution 12 - Java

For the case where there are 2 getters for the same property, the deserializer fails Refer Link

Solution 13 - Java

try to add jackson dependency

    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-core</artifactId>
        <version>2.9.3</version>
    </dependency>

    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-annotations</artifactId>
        <version>2.9.3</version>
    </dependency>

    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
        <version>2.9.3</version>
        <exclusions>
            <exclusion>
                <artifactId>jackson-annotations</artifactId>
                <groupId>com.fasterxml.jackson.core</groupId>
            </exclusion>
        </exclusions>
    </dependency>

Solution 14 - Java

I had the same issue. Root cause was using custom deserializer without default constructor.

Solution 15 - Java

I had this problem when using java 9+ modules. I had to open the module in order for the com.fasterxml.jackson.databind to access the objects with reflection. Alternatively you could only open the package where the models are located if you have one.

Solution 16 - Java

In my case my function in the controller was annotated like this:

 @PatchMapping(path = "/{machineGroupName}", consumes = "application/json-patch+json")

Once I removed the "Consumes" parameter it worked.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionThibautView Question on Stackoverflow
Solution 1 - JavaThibautView Answer on Stackoverflow
Solution 2 - JavaalvgarvillaView Answer on Stackoverflow
Solution 3 - JavaMader LevapView Answer on Stackoverflow
Solution 4 - JavaReza ShahbaziView Answer on Stackoverflow
Solution 5 - JavaKartheek MannepalliView Answer on Stackoverflow
Solution 6 - JavaSerge TahéView Answer on Stackoverflow
Solution 7 - JavaosoitzaView Answer on Stackoverflow
Solution 8 - Javauser_CCView Answer on Stackoverflow
Solution 9 - JavaDapper DanView Answer on Stackoverflow
Solution 10 - JavaKiran Kumar AView Answer on Stackoverflow
Solution 11 - JavaPramudithaView Answer on Stackoverflow
Solution 12 - JavaPratik ChaudhariView Answer on Stackoverflow
Solution 13 - JavaAborn JiangView Answer on Stackoverflow
Solution 14 - JavagstackoverflowView Answer on Stackoverflow
Solution 15 - JavaQutoryView Answer on Stackoverflow
Solution 16 - JavaGilad DahanView Answer on Stackoverflow