Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring REST: HttpMediaTypeNotSupportedException: Content type 'application/json;charset=UTF-8'

I am getting the above error, due to a problem with Jackson attempting to deserialize my POJO.

I've debugged the code and it returns false within Jackson's ObjectMapper:

public boolean canRead(Type type, Class<?> contextClass, MediaType mediaType) {
    JavaType javaType = getJavaType(type, contextClass);
    return (this.objectMapper.canDeserialize(javaType) && canRead(mediaType));
}

this.objectMapper.canDeserialize(javaType) returns false which causes the error

My Controller is as follows:

@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);
    }

My CancelRequestDTO implements Serializable:

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;
  }
}

My Spring configuration is as follow:

<!-- DispatcherServlet Context: defines this servlet's request-processing
    infrastructure -->

<!-- Enables the Spring MVC @Controller programming model -->
<mvc:annotation-driven />

<!-- Scan for stereotype annotations -->
<context:component-scan base-package="com.cancel.web.controller" />

<bean id="viewNameTranslator"
    class="org.springframework.web.servlet.view.DefaultRequestToViewNameTranslator" />

<bean class="org.springframework.web.servlet.view.BeanNameViewResolver" />





<bean id="jsonView"
    class="org.springframework.web.servlet.view.json.MappingJacksonJsonView" >
    <property name="contentType" value="application/json;charset=UTF-8"/>
    </bean>

<!-- Register JSON Converter for RESTful Web Service -->
<bean
    class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
    <property name="messageConverters">
        <list>
            <bean
                class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
            </bean>
        </list>
    </property>
</bean>

Anyone know what might be causing this deserialization issue?

Thanks

like image 213
DJ180 Avatar asked May 14 '13 16:05

DJ180


2 Answers

Caused by my DTO not having a default constructor with setters! So looks like an inaccurate Exception from Jackson

like image 54
DJ180 Avatar answered Nov 15 '22 01:11

DJ180


For anyone who still facing this problem, you cannot have two @JsonBackReference in a single class, add value to one of the reference like this @JsonBackReference(value = "secondParent") also add the same value to @JsonManagedReference(value ="secondParent") in parent class.

like image 29
Praveena DG Avatar answered Nov 15 '22 01:11

Praveena DG