请求参数对象中有这么个属性:
private String MESSAGE_ID;
public void setMESSAGE_ID(String MESSAGE_ID){
this.MESSAGE_ID = MESSAGE_ID;
}
public String getMESSAGE_ID(){
return this.MESSAGE_ID;
}
controller的接受方法是这样的:
@ResponseBody
@RequestMapping( value = "/change-subscriber-status" )
public BaseResp changeSubscriberStatus(@RequestBody StatusDTO statusDTO) {
logger.info("change-subscriber-status para: "+JSON.toJSONString(statusDTO));
。。。}
这么写其实spring mvc的MappingJackson2HttpMessageConverter消息转换器会认为接受参数项是message_ID,因为setter是setMESSAGE_ID(默认按照spring的java bean处理规范)
后台debug日志能清楚看出来:
2022-04-02 16:43:11,942 DEBUG [org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver] Resolving exception from handler [public com.BaseResp com.AcceptAppService.changeSubscriberStatus(com.StatusDTO)]: org.springframework.http.converter.HttpMessageNotReadableException: Could not read JSON: Unrecognized field "MESSAGE_ID" (class com.StatusDTO), not marked as ignorable (known properties: "message_ID"])
【解决方式】
属性定义时引入@com.fasterxml.jackson.annotation.JsonProperty.JsonProperty
@JsonProperty(value = "MESSAGE_ID")
private String MESSAGE_ID;
如此fasterxml.jackson反序列化的时候就能按这个名称解析json串