简体   繁体   English

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

[英]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. 由于杰克逊试图反序列化我的POJO,我收到了上述错误。

I've debugged the code and it returns false within Jackson's ObjectMapper: 我调试了代码,它在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) returns false which causes the error this.objectMapper.canDeserialize(javaType)返回false,导致错误

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

My Spring configuration is as follow: 我的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>

Anyone know what might be causing this deserialization issue? 有谁知道这可能导致这个反序列化问题?

Thanks 谢谢

Caused by my DTO not having a default constructor with setters! 我的DTO没有带有setter的默认构造函数! So looks like an inaccurate Exception from Jackson 所以看起来像杰克逊的一个不准确的例外

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

I have always done this using the ContentNegotiatingViewResolver. 我总是使用ContentNegotiatingViewResolver完成此操作。 It seems that it is not understanding the content type that you are passing it. 它似乎无法理解您传递的内容类型。 This is the configuration that I typically use for doing what you are trying to do: 这是我通常用于执行您尝试执行的操作的配置:

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

This video goes through doing exactly what you are trying to do with consuming the service through jQuery in the UI: 这个视频完成了你正在尝试通过UI中的jQuery消费服务所做的事情:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 org.springframework.web.HttpMediaTypeNotSupportedException:内容类型&#39;application / json; charset = UTF-8&#39;不支持 - org.springframework.web.HttpMediaTypeNotSupportedException: Content type 'application/json;charset=UTF-8' not supported 内容类型&#39;application / json; charset = utf-8&#39;不是预期的类型&#39;text / xml; 字符集= UTF-8&#39; - the content type 'application/json; charset=utf-8' was not the expected type 'text/xml; charset=utf-8' Content-Type 'application/json;charset=UTF-8' 不支持 SpringBoot Rest - Content-Type 'application/json;charset=UTF-8' is not supported SpringBoot Rest Express:从 Content-Type &quot;application/json; charset=utf-8&quot; 中删除 charset=utf-8 - Express: Remove charset=utf-8 from Content-Type "application/json; charset=utf-8" 不支持内容类型 'application/json;charset=UTF-8' - Content type 'application/json;charset=UTF-8' not supported 内容类型为application / json; 响应消息的charset = utf-8与绑定的内容类型不匹配(text / xml; charset = utf-8) - The content type application/json; charset=utf-8 of the response message does not match the content type of the binding (text/xml; charset=utf-8) Rest API 以纯字符串形式发送响应,但内容类型为“application/json;charset=UTF-8”。 我如何阅读回复 - Rest API sends response as plain string however Content Type is "application/json;charset=UTF-8". How can I readthe response 为什么内容类型标头用于json? “应用程序/ JSON; charset = utf-8“或”application / json“? - Why content type header to use for json? “application/json; charset=utf-8 ” or “application/json”? 如何将mappingJacksonHttpMessageConverter的内容类型从application / json; charset = UTF-8更改为application / json - How to change content type of MappingJacksonHttpMessageConverter from application/json;charset=UTF-8 to application/json 从响应中提取 JSON 作为 ResponseEntityProxy{[Content-Type: application/json;charset=UTF-8,Chunked: true]} - Extracting JSON from response as ResponseEntityProxy{[Content-Type: application/json;charset=UTF-8,Chunked: true]}
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM