无法构造“ com.test.FilterModel”的实例。没有可从字符串值反序列化的字符串参数构造函数/工厂方法

时间:2019-01-21 07:18:38

标签: java jackson

我使用Jackson对象映射器实现如下:

public void getFilterTest(){
    String filterJSON = "{\"SelectOptions\":[{\"key\":\"status\",\"options\":[{\"sign\":\"eq\",\"value\":\"FAILED\"},{\"sign\":\"eq\",\"value\":\"ESCALATED\"}]}]}";
    ObjectMapper objectMapper = new ObjectMapper();
    FilterModel filters = null;
    try {
        filters = objectMapper.convertValue(filterJSON, FilterModel.class);
    }catch(Exception e) {
        e.printStackTrace();
    }
    assertNotNull(filters);}

执行convertValue时出现以下错误:

  

java.lang.IllegalArgumentException:无法构造的实例   com.Test.FilterModel(尽管位于   至少存在一个创建者):没有字符串参数构造函数/工厂   从字符串值反序列化的方法   ('{“ SelectOptions”:[{“ key”:“ status”,“ options”:[{“ sign”:“ eq”,“ value”:“ FAILED”},{“ sign”:“ eq”,“ value“:” ESCALATED“}]}}}'')   在[资料来源:未知;行:-1,列:-1]在   com.fasterxml.jackson.databind.ObjectMapper._convert(ObjectMapper.java:3750)     在   com.fasterxml.jackson.databind.ObjectMapper.convertValue(ObjectMapper.java:3668)     在   com.test.util.TestUnitCPIODataUtil.getFilterTest(TestUnitCPIODataUtil.java:71)     在sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法)处   sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)     在   sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)     在java.lang.reflect.Method.invoke(Method.java:498)在   org.junit.runners.model.FrameworkMethod $ 1.runReflectiveCall(FrameworkMethod.java:50)     在   org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)     在   org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)     在   org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)     在   org.springframework.test.context.junit4.statements.RunBeforeTestExecutionCallbacks.evaluate(RunBeforeTestExecutionCallbacks.java:74)     在   org.springframework.test.context.junit4.statements.RunAfterTestExecutionCallbacks.evaluate(RunAfterTestExecutionCallbacks.java:84)     在   org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)     在   org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:75)     在   org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:86)     在   org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:84)     在org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)处   org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:251)     在   org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:97)     在org.junit.runners.ParentRunner处$ 3.run(ParentRunner.java:290)在   org.junit.runners.ParentRunner $ 1.schedule(ParentRunner.java:71)在   org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)在   org.junit.runners.ParentRunner.access $ 000(ParentRunner.java:58)在   org.junit.runners.ParentRunner $ 2.evaluate(ParentRunner.java:268)在   org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)     在   org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)     在org.junit.runners.ParentRunner.run(ParentRunner.java:363)处   org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:190)     在   org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)     在   org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)     在   org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:538)     在   org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:760)     在   org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:460)     在   org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:206)   造成原因:   com.fasterxml.jackson.databind.exc.MismatchedInputException:无法   构造的实例   com.test.FilterModel(尽管位于   至少存在一个创建者):没有字符串参数构造函数/工厂   从字符串值反序列化的方法   ('{“ SelectOptions”:[{“ key”:“ status”,“ options”:[{“ sign”:“ eq”,“ value”:“ FAILED”},{“ sign”:“ eq”,“ value“:” ESCALATED“}]}}}'')   在[资料来源:未知;行:-1,列:-1]在   com.fasterxml.jackson.databind.exc.MismatchedInputException.from(MismatchedInputException.java:63)     在   com.fasterxml.jackson.databind.DeserializationContext.reportInputMismatch(DeserializationContext.java:1343)     在   com.fasterxml.jackson.databind.DeserializationContext.handleMissingInstantiator(DeserializationContext.java:1032)     在   com.fasterxml.jackson.databind.deser.ValueInstantiator._createFromStringFallbacks(ValueInstantiator.java:371)     在   com.fasterxml.jackson.databind.deser.std.StdValueInstantiator.createFromString(StdValueInstantiator.java:323)     在   com.fasterxml.jackson.databind.deser.BeanDeserializerBase.deserializeFromString(BeanDeserializerBase.java:1373)     在   com.fasterxml.jackson.databind.deser.BeanDeserializer._deserializeOther(BeanDeserializer.java:171)     在   com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:161)     在   com.fasterxml.jackson.databind.ObjectMapper._convert(ObjectMapper.java:3745)     ...另外34个

这是我的模型课: 过滤器型号:

@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({
"SelectOptions"
})
public class FilterModel {

@JsonProperty("SelectOptions")
private List<SelectOption> selectOptions = null;
@JsonIgnore
private Map<String, Object> additionalProperties = new HashMap<String, Object>();

@JsonProperty("SelectOptions")
public List<SelectOption> getSelectOptions() {
return selectOptions;
}

@JsonProperty("SelectOptions")
public void setSelectOptions(List<SelectOption> selectOptions) {
this.selectOptions = selectOptions;
}

@JsonAnyGetter
public Map<String, Object> getAdditionalProperties() {
return this.additionalProperties;
}

@JsonAnySetter
public void setAdditionalProperty(String name, Object value) {
this.additionalProperties.put(name, value);
}

}

SelectOptions类:

@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({
"key",
"options"
})
public class SelectOption {

@JsonProperty("key")
private String key;
@JsonProperty("options")
private List<Option> options = null;
@JsonIgnore
private Map<String, Object> additionalProperties = new HashMap<String, Object>();

@JsonProperty("key")
public String getKey() {
return key;
}

@JsonProperty("key")
public void setKey(String key) {
this.key = key;
}

@JsonProperty("options")
public List<Option> getOptions() {
return options;
}

@JsonProperty("options")
public void setOptions(List<Option> options) {
this.options = options;
}

@JsonAnyGetter
public Map<String, Object> getAdditionalProperties() {
return this.additionalProperties;
}

@JsonAnySetter
public void setAdditionalProperty(String name, Object value) {
this.additionalProperties.put(name, value);
}

}

选项模型类:

@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({
"sign",
"value"
})
public class Option {

@JsonProperty("sign")
private String sign;
@JsonProperty("value")
private String value;
@JsonIgnore
private Map<String, Object> additionalProperties = new HashMap<String, Object>();

@JsonProperty("sign")
public String getSign() {
return sign;
}

@JsonProperty("sign")
public void setSign(String sign) {
this.sign = sign;
}

@JsonProperty("value")
public String getValue() {
return value;
}

@JsonProperty("value")
public void setValue(String value) {
this.value = value;
}

@JsonAnyGetter
public Map<String, Object> getAdditionalProperties() {
return this.additionalProperties;
}

@JsonAnySetter
public void setAdditionalProperty(String name, Object value) {
this.additionalProperties.put(name, value);
}

}

我在其他问题上尝试了几种解决方案,但找不到解决方案。似乎整个JSON字符串都被视为普通字符串。但是,我不确定该如何解决。我需要FilterModel类,因为我希望有1个以上的参数,这与上述测试用例中的字符串不同。

2 个答案:

答案 0 :(得分:3)

您应该调用 readValue 方法。

filters = objectMapper.readValue(filterJSON, FilterModel.class);

答案 1 :(得分:0)

final List<Object> providers = new ArrayList<Object>();

JacksonJaxbJsonProvider jacksonJsonProvider = new JacksonJaxbJsonProvider();
        providers.add( jacksonJsonProvider );

WebClient client =  WebClient.create(node + updateCacheNodeUrl, providers);
                      client.type("application/json").accept("application/json")
try {
                            ObjectMapper objectMapper = new ObjectMapper();
                            
                            objectMapper.enable(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY);
                            String serialisedJson = objectMapper.writeValueAsString(replicateCacheDto);
                            ReplicateCacheDto objReplicateCacheDto = objectMapper.readValue(serialisedJson, ReplicateCacheDto.class);
                            System.out.println("objReplicateCacheDto.toString = " + objReplicateCacheDto.toString());
                            
                            client.put(objReplicateCacheDto);
                        } catch (JsonParseException e) {
                            e.printStackTrace();
                        }
                      catch (Exception e) {
                            e.printStackTrace();
                        }