@JsonProperty @JsonIgnoreProperties

我无法将这个新的字段添加到我的构造函数中,这在json中不存在,并且抛出下面的错误,有人可以帮我解决这个问题。我已经尝试了两种注释@JsonIgnoreProperties和@JsonIgnore和@JsonIgnoreProperties(ignoreUnknown = TRUE)和一流水平,并构造层次@JsonProperty @JsonIgnoreProperties

下面是我的代码和错误信息按照相同的

public static class Location { 

private String a;

@Wither private int b;

@Wither private int c;

@Wither private int d;

@Wither private int newFiled; //which not there in json

//创建

@JsonCreator 

public Location(@JsonProperty("A") String a,

@JsonProperty("B") int b,

@JsonProperty("C") int b,

@JsonProperty("D") int d,

int newFiled) {

this.a = a;

this.b = b;

this.c = c;

this.d = d;

this.newFiled = newFiled;

}

public int newFiled() {

return a+b+c;

}

Error:

} 2017-12-18 12:53:24,194 WARN o.s.h.c.j.MappingJackson2HttpMessageConverter [I/O dispatcher 3] Failed to evaluate deserialization for type [simple type, class com.gap.plan.allocati

fasterxml.jackson.databind.JsonMappingException: Argument #4 of constructor [constructor for com.gap.plan.allocation.model.SkuInventory$DCLocation, annota

tions: {interface [email protected]rxml.jackson.annotation.JsonIgnoreProperties(ignoreUnknown=true, allowGetters=false, value=[], allow

Setters=false), interface [email protected]son.annotation.JsonCreator(mode=DEFAULT)}] has no property name annotation; must have name w

hen multiple-parameter constructor annotated as Creator

{} 2017-12-18 12:53:24,196 ERROR c.g.p.a.s.ProductAllocationService [ForkJoinPool-2-worker-36] Unknown processing error

org.springframework.web.client.RestClientException: Could not extract response: no suitable HttpMessageConverter found for response type [class com.gap.plan.allocation.model.SkuInven

tory] and content type [application/json;charset=UTF-8]

at org.springframework.web.client.HttpMessageConverterExtractor.extractData(HttpMessageConverterExtractor.java:109) ~[spring-web-4.2.4.RELEASE.jar:4.2.4.RELEASE]

at org.springframework.web.client.RestTemplate$ResponseEntityResponseExtractor.extractData(RestTemplate.java:835) ~[spring-web-4.2.4.RELEASE.jar:4.2.4.RELEASE]

at org.springframework.web.client.RestTemplate$ResponseEntityResponseExtractor.extractData(RestTemplate.java:819) ~[spring-web-4.2.4.RELEASE.jar:4.2.4.RELEASE]

at org.springframework.web.client.AsyncRestTemplate$ResponseExtractorFuture.convertResponse(AsyncRestTemplate.java:680) ~[spring-web-4.2.4.RELEASE.jar:4.2.4.RELEASE]

at org.springframework.web.client.AsyncRestTemplate$ResponseExtractorFuture.adapt(AsyncRestTemplate.java:667) ~[spring-web-4.2.4.RELEASE.jar:4.2.4.RELEASE]

at org.springframework.web.client.AsyncRestTemplate$ResponseExtractorFuture.adapt(AsyncRestTemplate.java:642) ~[spring-web-4.2.4.RELEASE.jar:4.2.4.RELEASE]

at org.springframework.util.concurrent.FutureAdapter.adaptInternal(FutureAdapter.java:100) ~[spring-core-4.2.4.RELEASE.jar:4.2.4.RELEASE]

at org.springframework.util.concurrent.ListenableFutureAdapter$1.onSuccess(ListenableFutureAdapter.java:56) ~[spring-core-4.2.4.RELEASE.jar:4.2.4.RELEASE]

at org.springframework.util.concurrent.ListenableFutureCallbackRegistry.success(ListenableFutureCallbackRegistry.java:118) ~[spring-core-4.2.4.RELEASE.jar:4.2.4.RELEASE]

at org.springframework.http.client.HttpComponentsAsyncClientHttpRequest$HttpResponseFutureCallback.completed(HttpComponentsAsyncClientHttpRequest.java:121) ~[spring-web-4.2.4

.RELEASE.jar:4.2.4.RELEASE]

at org.springframework.http.client.HttpComponentsAsyn c

ClientHttpRequest$HttpResponseFutureCallback.completed(HttpComponentsAsyncClientHttpRequest.java:102) ~[spring-web-4.2.4.RELEASE.jar:4.2.4.RELEASE]

at org.apache.http.concurrent.BasicFuture.completed(BasicFuture.java:119) ~[httpcore-4.4.4.jar:4.4.4]

at org.apache.http.impl.nio.client.DefaultClientExchangeHandlerImpl.responseCompleted(DefaultClientExchangeHandlerImpl.java:177) ~[httpasyncclient-4.1.1.jar:4.1.1]

at org.apache.http.nio.protocol.HttpAsyncRequestExecutor.processResponse(HttpAsyncRequestExecutor.java:432) ~[httpcore-nio-4.4.4.jar:4.4.4]

at org.apache.http.nio.protocol.HttpAsyncRequestExecutor.inputReady(HttpAsyncRequestExecutor.java:325) ~[httpcore-nio-4.4.4.jar:4.4.4]

at org.apache.http.impl.nio.DefaultNHttpClientConnection.consumeInput(DefaultNHttpClientConnection.java:267) ~[httpcore-nio-4.4.4.jar:4.4.4]

at org.apache.http.impl.nio.client.InternalIODispatch.onInputReady(InternalIODispatch.java:81) ~[httpasyncclient-4.1.1.jar:4.1.1]

at org.apache.http.impl.nio.client.InternalIODispatch.onInputReady(InternalIODispatch.java:39) ~[httpasyncclient-4.1.1.jar:4.1.1]

at org.apache.http.impl.nio.reactor.AbstractIODispatch.inputReady(AbstractIODispatch.java:123) ~[httpcore-nio-4.4.4.jar:4.4.4]

at org.apache.http.impl.nio.reactor.BaseIOReactor.readable(BaseIOReactor.java:164) ~[httpcore-nio-4.4.4.jar:4.4.4]

at org.apache.http.impl.nio.reactor.AbstractIOReactor.processEvent(AbstractIOReactor.java:339) ~[httpcore-nio-4.4.4.jar:4.4.4]

at org.apache.http.impl.nio.reactor.AbstractIOReactor.processEvents(AbstractIOReactor.java:317) ~[httpcore-nio-4.4.4.jar:4.4.4]

at org.apache.http.impl.nio.reactor.AbstractIOReactor.execute(AbstractIOReactor.java:278) ~[httpcore-nio-4.4.4.jar:4.4.4]

at org.apache.http.impl.nio.reactor.BaseIOReactor.execute(BaseIOReactor.java:106) ~[httpcore-nio-4.4.4.jar:4.4.4]

at org.apache.http.impl.nio.reactor.AbstractMultiworkerIOReactor$Worker.run(AbstractMultiworkerIOReactor.java:590) ~[httpcore-nio-4.4.4.jar:4.4.4]

at java.lang.Thread.run(Thread.java:748) ~[?:1.8.0_131]

回答:

您可以尝试在字段级别添加@JsonIgnore注释。或者你可以从构造函数中移除它并使用setter方法来赋值。

@JsonCreator 

public Location(@JsonProperty("A") String a,

@JsonProperty("B") int b,

@JsonProperty("C") int b,

@JsonProperty("D") int d) {

this.a = a;

this.b = b;

this.c = c;

this.d = d;

}

public int setNewFiled(int newFiled) {

this.newFiled = newFiled;

}

以上是 @JsonProperty @JsonIgnoreProperties 的全部内容, 来源链接: utcz.com/qa/261490.html

回到顶部