我有一个POJO类和Mixin,如下所示。
在调用Jackson时,我希望{'enquiry_id':'1234'}
可以工作,但是不会调用setter方法,并且getter返回null
。当我将其作为{'enquiryId':'1234'}
输入时,它会正确反序列化。
在使用Mixin之前,我直接在类中使用了相同的注释,并且没有遇到这个问题。
public class Party {
private String enquiryId;
public String getEnquiryId() {
return enquiryId;
}
public String setEnquiryId(String enquiryId) {
this.enquiryId = enquiryId;
}
}
public abstract class PartyMixIn {
@JsonProperty("enquiry_id") String enquiryId;
}
任何人都可以详细说明为何Jackson Mixin Deserializer不调用Party Class的setEnquiryId(String enquiryId)
方法。
示例用法
import io.swagger.annotations.ApiOperation;
import io.swagger.annotation.Api;
@Api(value = "Party Api")
@RestController
public class MainController {
@ApiOperation(value = "Process Party", notes = "Example usage {'enquiry_id':'1234'}")
@RequestMapping(value = "process", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public Party process(@RequestBody Party party) throws IOException, HttpException {
// Here i get party.getEnquiryId() as null
return party;
}
}
@Configuration
@EnableWebMvc
@EnableCaching
public class SpringConfig extends WebMvcConfigurerAdapter {
@Override
public void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
for (HttpMessageConverter<?> mc : converters) {
if (mc instanceof MappingJackson2HttpMessageConverter || mc instanceof MappingJackson2XmlHttpMessageConverter) {
((AbstractJackson2HttpMessageConverter) mc).getObjectMapper().addMixIn(Party.class, PartyMixIn.class);
}
}
return;
}
}