gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 06:44:04 25 4
gpt4 key购买 nike

由于 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 如下:

@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 实现了可序列化:

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 没有带有 setter 的默认构造函数!所以看起来像是 jackson 的一个不准确的异常

关于json - Spring 休息: HttpMediaTypeNotSupportedException: Content type 'application/json;charset=UTF-8' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16548182/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com