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.
This question is related to
java
json
spring
spring-mvc
jackson
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();
}
}
}
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>
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.
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.
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.
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')
For the case where there are 2 getters for the same property, the deserializer fails Refer Link
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.
I had the same issue. Root cause was using custom deserializer without default constructor.
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.
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.
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!
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;
}
.......
.......
.......
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.
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.
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.
Source: Stackoverflow.com