Java Jackson@JsonCreator由于没有单个int-arg构造函数/工厂方法而无法工作

Java Jackson@JsonCreator由于没有单个int-arg构造函数/工厂方法而无法工作,java,jackson,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

我有两个类,它们都没有默认构造函数。我想使用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);
        }
    }
}

这是我不想要的。

信息看起来很清楚
TestEntityId
需要一个接受
int
的构造函数。添加一个。这也是我的第一个想法,但这没有帮助。它仍然给出相同的错误。
String json = "{\"id\":{\"id\":1},\"mutableProperty\":\"blabla\"}";