@JsonProperty @JsonIgnoreProperties

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了@JsonProperty @JsonIgnoreProperties相关的知识,希望对你有一定的参考价值。

我无法将这个新的文件添加到我的构造函数中,而json中没有这个文件,并且在下面抛出错误可能有人请帮助我。我已经尝试了两个注释@JsonIgnoreProperties和@JsonIgnore以及@JsonIgnoreProperties(ignoreUnknown = true)以及类级别和构造函数级别

以下是我的代码和错误详细信息

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 com.fasterxml.jackson.annotation.JsonIgnoreProperties=@com.fasterxml.jackson.annotation.JsonIgnoreProperties(ignoreUnknown=true, allowGetters=false, value=[], allow
Setters=false), interface com.fasterxml.jackson.annotation.JsonCreator=@com.fasterxml.jackson.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的主要内容,如果未能解决你的问题,请参考以下文章

更新类中 Json 属性的名称

SpringBoot序列化与反序列化

用Jackson进行Json序列化时的常用注解

jackson 常用注解,比如忽略某些属性,驼峰和下划线互转

@JsonIgnore

@JsonIgnore 和 @Getter 注解