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

21

我遇到了上述错误,是由于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));
}

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

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

我的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>

有人知道是什么导致了这个反序列化问题吗?

谢谢


你的DTO/POJO出现错误的完整代码是什么? - bh5k
1
刚刚添加的,可能与缺少设置器有关? - DJ180
3个回答

30

由于我的DTO没有默认构造函数和setter方法所致!这看起来像是Jackson的一个不准确的异常。


2
是的,它们的异常可能会误导。 - bh5k
2
在我的情况下,我的DTO有一个Map作为其属性之一。当我在这个属性上添加@JsonIgnore时,它解决了我的问题。 - Adam
在我的情况下,我为属性“compiled”创建了方法getCompiledisCompiled,并且错误来自于Jackson。 - Edgard Leal

11

对于仍在遇到此问题的任何人,您不能在单个类中有两个 @JsonBackReference,请像这样向其中一个引用添加值 @JsonBackReference(value = "secondParent"),并且在父类中的 @JsonManagedReference(value ="secondParent") 中添加相同的值。


0

我一直都是使用ContentNegotiatingViewResolver来做这个。似乎它没有理解你传递的内容类型。下面是我通常用来实现你想要实现的配置:

<bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
    <property name="order" value="1" />
    <property name="contentNegotiationManager">
        <bean class="org.springframework.web.accept.ContentNegotiationManager">
            <constructor-arg>
                <bean class="org.springframework.web.accept.PathExtensionContentNegotiationStrategy">
                    <constructor-arg>
                        <map>
                            <entry key="json" value="application/json" />
                            <entry key="xml" value="application/xml" />
                        </map>
                    </constructor-arg>
                </bean>
            </constructor-arg>
        </bean>
    </property>

    <property name="defaultViews">
        <list>
            <bean class="org.springframework.web.servlet.view.json.MappingJacksonJsonView" />
            <bean class="org.springframework.web.servlet.view.xml.MarshallingView">
                <constructor-arg>
                    <bean class="org.springframework.oxm.xstream.XStreamMarshaller">
                        <property name="autodetectAnnotations" value="true" />
                    </bean>
                </constructor-arg>
            </bean>
        </list>
    </property>
</bean>

这个视频演示了如何通过jQuery在UI中消费服务,正是你正在尝试做的事情:

http://pluralsight.com/training/Courses/TableOfContents/springmvc-intro


依然是同样的问题,尽管错误与字符集有关,但它与ObjectMapper中的序列化有关。 - DJ180

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接