Reputation: 2625
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.
Upvotes: 70
Views: 157971
Reputation: 2434
Just adding request header
Content-Type: application/json
worked for me.
Upvotes: 0
Reputation: 558
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.
Upvotes: 0
Reputation: 87
For the case where there are 2 getters for the same property, the deserializer fails Refer Link
Upvotes: 1
Reputation: 109
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.
Upvotes: 1
Reputation: 738
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.
Upvotes: 1
Reputation: 37002
I had the same issue. Root cause was using custom deserializer without default constructor.
Upvotes: 0
Reputation: 11
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;
}
.......
.......
.......
Upvotes: 1
Reputation: 464
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.
Upvotes: 6
Reputation: 1072
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.
Upvotes: 1
Reputation: 1059
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>
Upvotes: 0
Reputation: 4776
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();
}
}
}
Upvotes: 1
Reputation: 2625
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.
Upvotes: 87
Reputation: 181
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!
Upvotes: 2
Reputation: 2089
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')
Upvotes: 2
Reputation: 186
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.
Upvotes: 2
Reputation: 1086
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.
Upvotes: 8
Reputation: 451
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.
Upvotes: 4