Spring REST:HttpMediaTypeNotSupportedException:内容类型'application/json; charset = UTF-8'

JJ1*_*180 17 json spring-mvc jackson

由于杰克逊试图反序列化我的POJO,我收到了上述错误.

我调试了代码,它在Jackson的ObjectMapper中返回false:

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

this.objectMapper.canDeserialize(javaType)返回false,导致错误

我的控制器如下:

@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);
    }
Run Code Online (Sandbox Code Playgroud)

我的CancelRequestDTO实现了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;
  }
}
Run Code Online (Sandbox Code Playgroud)

我的Spring配置如下:

<!-- 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>
Run Code Online (Sandbox Code Playgroud)

有谁知道这可能导致这个反序列化问题?

谢谢

JJ1*_*180 28

我的DTO没有带有setter的默认构造函数!所以看起来像杰克逊的一个不准确的例外

  • 是的,他们的例外可能会误导我. (2认同)
  • 就我而言,我的DTO有一个属性的Map.当我将@JsonIgnore添加到此属性时,它解决了我的问题. (2认同)

小智 11

对于仍然面临此问题的任何人,您不能@JsonBackReference在一个类中有两个,将值添加到其中一个引用中,这样@JsonBackReference(value = "secondParent")也会@JsonManagedReference(value ="secondParent")在父类中添加相同的值.