由于没有单一的int-arg构造函数/工厂方法,Jackson @JsonCreator无法正常工作

时间:2015-12-09 08:42:06

标签: java jackson

我有两个类都没有默认构造函数。我想用Jackson序列化/反序列化它们。但是,我收到错误:

com.fasterxml.jackson.databind.JsonMappingException: Can not instantiate value of type [simple type, class com.example.JacksonCustomizationsTest$TestEntityId] from Integral number (1); no single-int-arg constructor/factory method
 at [Source: {"id":1,"mutableProperty":"blabla"}; line: 1, column: 2] (through reference chain: com.example.TestEntity["id"])

    at com.fasterxml.jackson.databind.JsonMappingException.from(JsonMappingException.java:148)
    at com.fasterxml.jackson.databind.DeserializationContext.mappingException(DeserializationContext.java:878)
    at com.fasterxml.jackson.databind.deser.std.StdValueInstantiator.createFromInt(StdValueInstantiator.java:304)
    at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.deserializeFromNumber(BeanDeserializerBase.java:1133)
    at com.fasterxml.jackson.databind.deser.BeanDeserializer._deserializeOther(BeanDeserializer.java:147)
    at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:136)
    at com.fasterxml.jackson.databind.deser.SettableBeanProperty.deserialize(SettableBeanProperty.java:520)
    at com.fasterxml.jackson.databind.deser.BeanDeserializer._deserializeWithErrorWrapping(BeanDeserializer.java:463)
    at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserializeUsingPropertyBasedWithUnwrapped(BeanDeserializer.java:638)
    at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserializeWithUnwrapped(BeanDeserializer.java:523)
    at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserializeFromObject(BeanDeserializer.java:291)
    at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:133)
    at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:3736)
    at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:2726)
    at com.example.JacksonCustomizationsTest.testImmutableEntityIdFromJson(JacksonCustomizationsTest.java:44)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:117)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:234)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:74)

这是显示问题的单元测试:

package com.example;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonUnwrapped;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.jayway.jsonpath.JsonPath;
import com.example.ImmutableEntityId;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;

import static org.hamcrest.CoreMatchers.notNullValue;
import static org.hamcrest.core.Is.is;
import static org.junit.Assert.assertThat;

public class JacksonCustomizationsTest {

    private ObjectMapper objectMapper = new ObjectMapper();



    @Test
    public void testImmutableEntityIdToJson() throws JsonProcessingException {

        TestEntity entity = new TestEntity(new TestEntityId(1L));
        entity.setMutableProperty("blabla");
        String json = objectMapper.writeValueAsString(entity);
        System.out.println("json = " + json);
        assertThat(JsonPath.read(json, "$.id"), is(1));
        assertThat(JsonPath.read(json, "$.mutableProperty"), is("blabla"));
    }

    @Test
    public void testImmutableEntityIdFromJson() throws IOException {
        String json = "{\"id\":1,\"mutableProperty\":\"blabla\"}";
        TestEntity testEntity = objectMapper.readValue(json, TestEntity.class);
        assertThat( testEntity, is(notNullValue()) );
    }

// -------------------------- INNER CLASSES --------------------------

    private static class TestEntity {
        @JsonUnwrapped
        private TestEntityId id;
        private String mutableProperty;

        @JsonCreator
        public TestEntity(@JsonProperty("id") TestEntityId id) {
            this.id = id;
        }

        public TestEntityId getId() {
            return id;
        }

        public String getMutableProperty() {
            return mutableProperty;
        }

        public void setMutableProperty(String mutableProperty) {
            this.mutableProperty = mutableProperty;
        }
    }

    private static class TestEntityId extends ImmutableEntityId<Long> {
        @JsonCreator
        public TestEntityId(@JsonProperty("id") Long id) {
            super(id);
        }
    }
}

更新:

使其工作的一种方法是删除@JsonUnwrapped注释,但随后JSON更改为:

String json = "{\"id\":{\"id\":1},\"mutableProperty\":\"blabla\"}";

我不想要。

0 个答案:

没有答案