Note that there are some explanatory texts on larger screens.

plurals
  1. POSpring REST: HttpMediaTypeNotSupportedException: Content type 'application/json;charset=UTF-8'
    text
    copied!<p>I am getting the above error, due to a problem with Jackson attempting to deserialize my POJO.</p> <p>I've debugged the code and it returns false within Jackson's ObjectMapper:</p> <pre><code>public boolean canRead(Type type, Class&lt;?&gt; contextClass, MediaType mediaType) { JavaType javaType = getJavaType(type, contextClass); return (this.objectMapper.canDeserialize(javaType) &amp;&amp; canRead(mediaType)); } </code></pre> <p>this.objectMapper.canDeserialize(javaType) returns false which causes the error</p> <p>My Controller is as follows:</p> <pre><code>@Controller public class CancelController { @Autowired private CancelService cancelService; @RequestMapping( value="/thing/cancel", method=RequestMethod.POST, consumes="application/json" ) public @ResponseBody CancelThingResponseDTO cancelThing(@RequestBody CancelRequestDTO cancelThingRequest) { return cancelService.cancelThing(cancelThingRequest); } </code></pre> <p>My CancelRequestDTO implements Serializable:</p> <pre><code>public class CancelRequestDTO implements Serializable{ /** * Default serialization ID */ private static final long serialVersionUID = 1L; /** * Reason code associated with the request */ private final String reasonCode; /** * Identifier of the entity associated with the request */ private final EntityIdentifier entityIdentifier; /** * Default constructor * * @param reasonCode Reason code associated with the request * @param entityIdentifier Identifier of the entity associated with the request */ public CancelRequestDTO(String reasonCode, EntityIdentifier entityIdentifier) { super(); this.reasonCode = reasonCode; this.entityIdentifier = entityIdentifier; } /** * @return Returns the reasonCode. */ public String getReasonCode() { return reasonCode; } /** * @return Returns the entityIdentifier. */ public EntityIdentifier getEntityIdentifier() { return entityIdentifier; } } </code></pre> <p>My Spring configuration is as follow:</p> <pre><code>&lt;!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure --&gt; &lt;!-- Enables the Spring MVC @Controller programming model --&gt; &lt;mvc:annotation-driven /&gt; &lt;!-- Scan for stereotype annotations --&gt; &lt;context:component-scan base-package="com.cancel.web.controller" /&gt; &lt;bean id="viewNameTranslator" class="org.springframework.web.servlet.view.DefaultRequestToViewNameTranslator" /&gt; &lt;bean class="org.springframework.web.servlet.view.BeanNameViewResolver" /&gt; &lt;bean id="jsonView" class="org.springframework.web.servlet.view.json.MappingJacksonJsonView" &gt; &lt;property name="contentType" value="application/json;charset=UTF-8"/&gt; &lt;/bean&gt; &lt;!-- Register JSON Converter for RESTful Web Service --&gt; &lt;bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"&gt; &lt;property name="messageConverters"&gt; &lt;list&gt; &lt;bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"&gt; &lt;/bean&gt; &lt;/list&gt; &lt;/property&gt; &lt;/bean&gt; </code></pre> <p></p> <p>Anyone know what might be causing this deserialization issue? </p> <p>Thanks</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload