Wrong POST body JSON serialization when using postForEntity in Spring

时间:2015-09-30 23:08:11

标签: java json spring jackson

I'm trying to send an instance of the object EGiftCreationRequest as JSON via POST body in Spring:

    final BigDecimal amount = new BigDecimal(100.00);
    final String configurationId = "test_configuration_id";
    final String referenceNumber = "12345";

    EGiftCreationRequest giftCreationRequest = new EGiftCreationRequest() {{
        giftAmount(amount);
        productConfigurationId(configurationId);
        retrievalReferenceNumber(referenceNumber);
    }};

    HttpHeaders headers = new HttpHeaders();
    headers.setContentType(MediaType.APPLICATION_JSON);

    HttpEntity<EGiftCreationRequest> httpEntity = new HttpEntity<EGiftCreationRequest>(giftCreationRequest, headers);

    ResponseEntity<EGift> entity = new TestRestTemplate().postForEntity(
            "http://localhost:" + this.port + "/eGiftProcessing/v1/generateEGift",
            httpEntity,
            EGift.class
    );

However, for some reason the object is being serialized into the following String:

{"headerParams":{}}

Obviously this has nothing to do with my EGiftCreationRequest, which is actually:

public class EGiftCreationRequest extends RequestBase<EGiftCreationRequest> {
    private BigDecimal giftAmount;
    private String productConfigurationId;
    private String retrievalReferenceNumber;

    public BigDecimal giftAmount() {
        return this.giftAmount;
    }

    public String productConfigurationId() {
        return this.productConfigurationId;
    }

    public String retrievalReferenceNumber() {
        return this.retrievalReferenceNumber;
    }

    public EGiftCreationRequest giftAmount(final BigDecimal giftAmount) {
        this.giftAmount = giftAmount;
        return this;
    }

    public EGiftCreationRequest productConfigurationId(final String productConfigurationId) {
        this.productConfigurationId = productConfigurationId;
        return this;
    }

    public EGiftCreationRequest retrievalReferenceNumber(final String retrievalReferenceNumber) {
        this.retrievalReferenceNumber = retrievalReferenceNumber;
        return this;
    }
}

What can possibly be going on?

1 个答案:

答案 0 :(得分:3)

这是由错误配置的Jackson映射器引起的。默认情况下,Jackson正在寻找以JavaBeans方式命名的访问器(get *(),set *())来检索和设置值。由于模型使用不同的命名约定(字段名称本身),杰克逊无法序列化对象。

以下映射器配置使一切正常:

    ObjectMapper mapper = new ObjectMapper();

    mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
    mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);

    mapper.setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY);
    mapper.setVisibility(PropertyAccessor.GETTER, JsonAutoDetect.Visibility.NONE);
    mapper.setVisibility(PropertyAccessor.SETTER, JsonAutoDetect.Visibility.NONE);
    mapper.setVisibility(PropertyAccessor.CREATOR, JsonAutoDetect.Visibility.NONE);

    TestRestTemplate testRestTemplate = new TestRestTemplate();
    List<HttpMessageConverter<?>> messageConverters = new ArrayList<HttpMessageConverter<?>>();
    messageConverters.add(new MappingJackson2HttpMessageConverter(mapper));

    testRestTemplate.setMessageConverters(messageConverters);