Jackson-Mapper null zurückgeben mit spring rest

Ich bin immer Json aus diesem link

Bin ich mit folgenden zwei pojo-Klassen für die Zuordnung .

1)NewsSource.class

@JsonIgnoreProperties(ignoreUnknown = true)
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({ "status", "sources" })
public class NewsSource {

    @JsonProperty("status")
    private String status;
    @JsonProperty("sources")
    private List<Source> sources = null;

    @JsonProperty("status")
    public String getStatus() {
        return status;
    }

    @JsonProperty("status")
    public void setStatus(String status) {
        this.status = status;
    }

    @JsonProperty("sources")
    public List<Source> getSources() {
        return sources;
    }

    @JsonProperty("sources")
    public void setSources(List<Source> sources) {
        this.sources = sources;
    }

}

2)Source.class

    @JsonIgnoreProperties(ignoreUnknown = true)
    @JsonInclude(JsonInclude.Include.NON_NULL)
    @JsonPropertyOrder({ "id", "name", "description", "url", "category", "language", "country", "sortBysAvailable" })

    public class Source {

        @JsonProperty("id")
        private String id;
        @JsonProperty("name")
        private String name;
        @JsonProperty("description")
        private String description;
        @JsonProperty("url")
        private String url;
        @JsonProperty("category")
        private String category;
        @JsonProperty("language")
        private String language;
        @JsonProperty("country")
        private String country;
        @JsonProperty("sortBysAvailable")
        private List<String> sortBysAvailable = null;
//getter setters generated with IDE , and annotated with @JsonProperty too

Ich bin mit diesem code mapping von json zu pojo .

 public NewsSource getNewsSource() {
        String URL = source_url; //above mentioned url to get json
ObjectMapper omapper= new ObjectMapper();
        omapper.setVisibilityChecker(omapper.getSerializationConfig().getDefaultVisibilityChecker()
                .withFieldVisibility(JsonAutoDetect.Visibility.ANY)
                .withGetterVisibility(JsonAutoDetect.Visibility.NONE)
                .withSetterVisibility(JsonAutoDetect.Visibility.NONE)
                .withCreatorVisibility(JsonAutoDetect.Visibility.NONE));

   NewsSource newsSource =null;
        try {
        String result = restTemplate.getForObject(URL, String.class);
//result is returnning data properly
            omapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
            newsSource = omapper.readValue(result, NewsSource.class);
//newsSource returnning null
}catch (Exception ex){

System.out.println("oooh exception ");
}

Ich habe versucht, viele vorgeschlagenen tricks, die erwähnt werden, auf diese link aber niemand für mich gearbeitet 🙂

Hinweis: ich bin mit dem folgenden maven dependency für Jackson .

        <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
    </dependency>

Jeder Vorschlag würde geschätzt werden. vielen Dank im Voraus

Update
Ich habe folgende code .

    private static Object getJsonObject(String str) {
        Object obj = null;
        try {
            ObjectMapper mapper = new ObjectMapper();
            mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
            if (str != null) {
                obj = mapper.readValue(str, NewsSource.class);

            }
        } catch (Exception exc) {
            exc.printStackTrace();
        }
        System.out.println("json obj:===>" + obj);
        return obj;
    }

    public NewsSource getNewsSource() {
        String URL = source_url;

        NewsSource newsSource =null;
        try {
            String result = restTemplate.getForObject(URL, String.class);
            //String jsonStr=getJsonString(result);
            ObjectMapper mapper = new ObjectMapper();
        //   newsSource = mapper.readValue(result, NewsSource.class);

            newsSource= (NewsSource) getJsonObject(result);
            System.out.println("done:" +newsSource);
}catch (Exception ex){

System.out.println("oooh exception ");
}

        return newsSource;
    }

StackTrace

    com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of java.util.ArrayList out of VALUE_STRING token
 at [Source: {"status":"ok","sources":[{"id":"abc-news-au","name":"ABC News (AU)","description":"Australia's most trusted source of local, national and world news. Comprehensive, independent, in-depth analysis, the latest business, sport, weather and more.","url":"http://www.abc.net.au/news","category":"general","language":"en","country":"au","urlsToLogos":{"small":"","medium":"","large":""},"sortBysAvailable":"top"},{"id":"al-jazeera-english","name":"Al Jazeera English","description":"News, analysis from the Middle East and worldwide, multimedia and interactives, opinions, documentaries, podcasts, long reads and broadcast schedule.","url":"http://www.aljazeera.com","category":"general","language":"en","country":"us","urlsToLogos":{"small":"","medium":"","large":""},"sortBysAvailable":"top"},{"id":"ars-technica","name":"Ars Technica","description":"The PC enthusiast's resource. Power users and the tools they love, without computing religion.","url":"http://arstechnica.com","category":"technology","language":"en","country":"us","urlsToLogos":{"small":"","medium":"","large":""},"sortBysAvailable":"top"},{"id":"associated-press","name":"Associated Press","description":"The AP delivers in-depth coverage on the international, politics, lifestyle, business, and entertainment news.","url":"https://apnews.com/","category":"general","language":"en","country":"us","urlsToLogos":{"small":"","medium":"","large":""},"sortBysAvailable":"top"},{"id":"bbc-news","name":"BBC News","description":"Use BBC News for up-to-the-minute news, breaking news, video, audio and feature stories. BBC News provides trusted World and UK news as well as local and regional perspectives. Also entertainment, business, science, technology and health news.","url":"http://www.bbc.co.uk/news","category":"general","language":"en","country":"gb","urlsToLogos":{"small":"","medium":"","large":""},"sortBysAvailable":"top"},{"id":"bbc-sport","name":"BBC Sport","description":"The home of BBC Sport online. Includes live sports coverage, breaking news, results, video, audio and analysis on Football, F1, Cricket, Rugby Union, Rugby League, Golf, Tennis and all the main world sports, plus major events such as the Olympic Games.","url":"http://www.bbc.co.uk/sport","category":"sport","language":"en","country":"gb","urlsToLogos":{"small":"","medium":"","large":""},"sortBysAvailable":"top"},{"id":"time","name":"Time","description":"Breaking news and analysis from TIME.com. Politics, world news, photos, video, tech reviews, health, science and entertainment news.","url":"http://time.com","category":"general","language":"en","country":"us","urlsToLogos":{"small":"","medium":"","large":""},"sortBysAvailable":"top"},{"id":"usa-today","name":"USA Today","description":"Get the latest national, international, and political news at USATODAY.com.","url":"http://www.usatoday.com/news","category":"general","language":"en","country":"us","urlsToLogos":{"small":"","medium":"","large":""},"sortBysAvailable":"top"}]}; line: 1, column: 402] (through reference chain: com.example.demo.dto.NewsSource["sources"]->java.util.ArrayList[0]->com.example.demo.dto.Source["sortBysAvailable"])
    at com.fasterxml.jackson.databind.JsonMappingException.from(JsonMappingException.java:270)
    at com.fasterxml.jackson.databind.DeserializationContext.reportMappingException(DeserializationContext.java:1234)
    at com.fasterxml.jackson.databind.DeserializationContext.handleUnexpectedToken(DeserializationContext.java:1122)
    at com.fasterxml.jackson.databind.DeserializationContext.handleUnexpectedToken(DeserializationContext.java:1075)
    at com.fasterxml.jackson.databind.deser.std.StringCollectionDeserializer.handleNonArray(StringCollectionDeserializer.java:260)
    at com.fasterxml.jackson.databind.deser.std.StringCollectionDeserializer.deserialize(StringCollectionDeserializer.java:187)
    at com.fasterxml.jackson.databind.deser.std.StringCollectionDeserializer.deserialize(StringCollectionDeserializer.java:177)
    at com.fasterxml.jackson.databind.deser.std.StringCollectionDeserializer.deserialize(StringCollectionDeserializer.java:20)
    at com.fasterxml.jackson.databind.deser.SettableBeanProperty.deserialize(SettableBeanProperty.java:504)
    at com.fasterxml.jackson.databind.deser.impl.MethodProperty.deserializeAndSet(MethodProperty.java:104)
    at com.fasterxml.jackson.databind.deser.BeanDeserializer.vanillaDeserialize(BeanDeserializer.java:276)
    at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:140)
    at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:287)
    at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:259)
    at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:26)
    at com.fasterxml.jackson.databind.deser.SettableBeanProperty.deserialize(SettableBeanProperty.java:504)
    at com.fasterxml.jackson.databind.deser.impl.MethodProperty.deserializeAndSet(MethodProperty.java:104)
    at com.fasterxml.jackson.databind.deser.BeanDeserializer.vanillaDeserialize(BeanDeserializer.java:276)
    at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:140)
    at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:3814)
    at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:2858)
    at com.example.demo.Utill.RuleUtill.getJsonObject(RuleUtill.java:80)
    at com.example.demo.Utill.RuleUtill.getNewsSource(RuleUtill.java:100)
    at com.example.demo.Service.NewsApiService.getSource(NewsApiService.java:32)
    at com.example.demo.Controller.Home.somespecific(Home.java:56)
    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:498)
    at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:205)
    at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:133)
    at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:97)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:827)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:738)
    at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85)
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:967)
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:901)
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970)
    at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:861)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:635)
    at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:742)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:99)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.springframework.web.filter.HttpPutFormContentFilter.doFilterInternal(HttpPutFormContentFilter.java:108)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:81)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:197)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:199)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:478)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:81)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:342)
    at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:803)
    at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
    at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:868)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1459)
    at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Thread.java:745)
json obj:===>null
done:null
  • Bitte osting die volle Ausnahme-stack
  • nur null-Zeiger-Ausnahme , denn die Ausnahme aufgetreten ist, im mapping .
InformationsquelleAutor naila naseem | 2017-11-17



2 Replies
  1. 1

    Modifizierte ich Ihr source-code wie folgt aus irgendwelchen Gründen:

    • RestTemplete wird automatisch konvertieren Sie die Antwort Json-string in die angegebene POJO, wie Sie definiert in NewsSource und Source.
    • Wie ich oben sagte, die Methode getJsonObject redundant ist.
    • Ihre Ausnahmebehandlung in der Methode getNewsSource nicht drucken die wahre Ursache.

    getNewsSource

    public NewsSource getNewsSource() {
        String URL = source_url;
    
        NewsSource newsSource =null;
        try {
            /*
            String result = restTemplate.getForObject(URL, String.class);
            ObjectMapper mapper = new ObjectMapper();
    
            newsSource= (NewsSource) getJsonObject(result);
            System.out.println("done:" +newsSource);
            */
    
            newsSource = restTemplate.getForObject(URL, NewsSource.class);
            System.out.println("done:" + newsSource.toString());
        } catch (Exception ex){
            System.out.println("oooh exception \n" + ex);
        }
    
        return newsSource;
    }

    Hoffe, dies hilft Ihnen! 🙂

    • o großer . es geklappt hat . so getForObject alles funktioniert , keine Notwendigkeit der mapper .Gut
    • Ja, Sie haben es! Das Spring-framework bietet viele APIs zu reduzieren unseren Aufwand beim Programmieren.
  2. 0

    Den code korrekt arbeitet. Sieht aus wie Sie mit einer älteren version von jackson. Getestet habe ich mit 2.9.0 und unten code funktioniert wie erwartet, obwohl mit version 2.9.0 setVisibilityChecker(…..) ist veraltet.

    public static void main(String[] args) {
    
        NewsSource nSource=new NewsSource();
    
        nSource.setStatus("status");
    
        Source source=new Source();
    
        source.setCategory("category");
    
        List<Source> sList=new ArrayList<Source>();
        sList.add(source);
    
        nSource.setSources(sList);
    
        String jsonStr=getJsonString(nSource);
        //{"status":"status","sources":[{"category":"category"}]}
        System.out.println(jsonStr);
    
        NewsSource newsSource =(NewsSource)getJsonObject(jsonStr);
        //[email protected]
        System.out.println(newsSource);
    
    }
    
    private static String getJsonString(Object object) {
        String jsonString = "";
        try {
            ObjectMapper mapper = new ObjectMapper();
            if (object != null) {
                jsonString = mapper.writeValueAsString(object);
            }
        } catch (Exception exc) {
            exc.printStackTrace();
        }
        System.out.println("jsonString:===>" + jsonString);
        return jsonString;
    }
    
    private static Object getJsonObject(String str) {
        Object obj = null;
        try {
            ObjectMapper mapper = new ObjectMapper();
            mapper.setVisibilityChecker(mapper.getSerializationConfig().getDefaultVisibilityChecker()
                    .withFieldVisibility(JsonAutoDetect.Visibility.ANY)
                    .withGetterVisibility(JsonAutoDetect.Visibility.NONE)
                    .withSetterVisibility(JsonAutoDetect.Visibility.NONE)
                    .withCreatorVisibility(JsonAutoDetect.Visibility.NONE));
    
            mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
            if (str != null) {
                 obj = mapper.readValue(str, NewsSource.class);
    
            }
        } catch (Exception exc) {
            exc.printStackTrace();
        }
        System.out.println("json obj:===>" + obj);
        return obj;
    }
    • lassen Sie mich überprüfen Sie, dass
    • Sie müssen Jackson Abhängigkeit nur und es wird funktionieren
    • Ich bin mit jackson Abhängigkeit , erwähnt in Frage
    • <Abhängigkeit> <groupId>com.fasterxml.jackson.Kern</groupId> <artifactId>jackson-annotations</artifactId> <version>2.8.0</version> </Abhängigkeit> <Abhängigkeit> <groupId>com.fasterxml.jackson.Kern</groupId> <artifactId>jackson-core</artifactId> <version>2.8.7</version> </Abhängigkeit> <Abhängigkeit> <groupId>com.fasterxml.jackson.Kern</groupId> <artifactId>jackson-databind-Methode</artifactId> <version>2.8.0</version> <Anwendungsbereich>kompilieren</scope> </Abhängigkeit>
    • Verwenden Sie keine anderen Abhängigkeiten außer den 3 oben erwähnten, und versuchen Sie zum erstellen der json die Art und Weise habe ich mit den Jackson und dann deserialisiert das Objekt aus den generierten json-string. Es sollte funktionieren. Ob das der Fall ist, dann kann das problem mit dem json-string, den Sie erstellt haben.
    • Ich bin eine Aktualisierung in Frage . vielen Dank für Ihre Mühe .

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert.