Affects: \spring-web-6.1.1
I have an @Entity annotated class that I also use as a type for @RequestBody argument in an @PostMapping class. This entity has a @OneToMany collection field that i forgot to annotate with @JsonIgnore. When I try to do a POST request to the endpoint method using a json object that does not include the collection field Spring will throw the very confusing and unusual error
org.springframework.web.HttpMediaTypeNotSupportedException: Content-Type 'application/json;charset=UTF-8' is not supported
at org.springframework.web.servlet.mvc.method.annotation.AbstractMessageConverterMethodArgumentResolver.readWithMessageConverters(AbstractMessageConverterMethodArgumentResolver.java:213) ~[spring-webmvc-6.1.1.jar:6.1.1]
at org.springframework.web.servlet.mvc.method.annotation.RequestResponseBodyMethodProcessor.readWithMessageConverters(RequestResponseBodyMethodProcessor.java:159) ~[spring-webmvc-6.1.1.jar:6.1.1]
at org.springframework.web.servlet.mvc.method.annotation.RequestResponseBodyMethodProcessor.resolveArgument(RequestResponseBodyMethodProcessor.java:134) ~[spring-webmvc-6.1.1.jar:6.1.1]
at org.springframework.web.method.support.HandlerMethodArgumentResolverComposite.resolveArgument(HandlerMethodArgumentResolverComposite.java:122) ~[spring-web-6.1.1.jar:6.1.1]
at org.springframework.web.method.support.InvocableHandlerMethod.getMethodArgumentValues(InvocableHandlerMethod.java:218) ~[spring-web-6.1.1.jar:6.1.1]
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:171) ~[spring-web-6.1.1.jar:6.1.1]
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:118) ~[spring-webmvc-6.1.1.jar:6.1.1]
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:917) ~[spring-webmvc-6.1.1.jar:6.1.1]
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:829) ~[spring-webmvc-6.1.1.jar:6.1.1]
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87) ~[spring-webmvc-6.1.1.jar:6.1.1]
Spring boot web supports application/json by default so this doesn't make sense. The error remains if i explicitly add the value application/json;UTF-8 to the consumes argument of @PostMapping. The error is caused by something entirely different, namely that the incoming json object doesn't contain the collections field that i forgot to annotate with @JsonIgnore. If i add this annotation and retry the request everything works fine again.
Please make spring return a more informative error when the json object is missing a collection field. The actual error has nothing to do with an unsupported mediatype. Here's the post method and entity class to reproduce the problem
@Entity
public class Faq extends Timestamp implements Translatable<FaqTitleAndContent>, Serializable{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private Integer faqRank;
@Transient
private String question; //TODO: Inhoud eerst overzetten naar FaqTitleAndContent en daarna deze fields weghalen.
@Transient
private String answer;
@OneToMany(mappedBy = "parent", fetch = FetchType.LAZY, cascade = CascadeType.ALL)
private Set<FaqTitleAndContent> translations;
}
@PostMapping("/create/faq")
private ResponseEntity<?> createFaq(@RequestBody Faq faq){
//business logic
return faq;
}
Comment From: snicoll
Here's the post method and entity class to reproduce the problem
Thanks for the report, but please move that code in text into a project that we can actually run. You can attach a zip to this issue or push the code to a separate GitHub repository.
Comment From: spring-projects-issues
If you would like us to look at this issue, please provide the requested information. If the information is not provided within the next 7 days this issue will be closed.
Comment From: spring-projects-issues
Closing due to lack of requested feedback. If you would like us to look at this issue, please provide the requested information and we will re-open the issue.