2017-12-18 176 views
0

我無法將這個新的字段添加到我的構造函數中,這在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] 
+0

什麼是添加newFiled與JsonCreator anntated如果你知道這個領域是不是JSON文件的組成部分構造函數的參數的poijt?只是不要將它添加到構造函數。 –

+0

@JB Nizet如果我不在構造函數中添加該字段,它的拋出錯誤 –

+1

而錯誤是? –

回答

0

您可以嘗試在字段級別添加@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; 
     } 
+0

這也是行不通的 –