0

I am new to JAVA/Jackson world; I am trying to map incoming json to pojo's using jackson library. I dont have control on the incoming JSON. Below is hte json structure.

{
 "kind": "html",
 "responseCode": 200,
 "DisplayRules": {
  "Age": {
   "Max": 40
  },
  "Weight": {
   "Max": 100
  }
 }

Below are my springboot based app class.

@JsonIgnoreProperties(ignoreUnknown = true)
@JsonInclude(Include.NON_NULL)
public class JSONResults {
private String kind;
private DisplayRules displayRules;
//Getters & Setters
}

@JsonIgnoreProperties(ignoreUnknown = true)
public class DisplayRules {
private Age age;
private Weight weight;
//Getters & Setters
@Override
    public String toString(){
        return  " - " + Weight.toString();
    }
    }


@JsonIgnoreProperties(ignoreUnknown = true)
public class Age {
    private int Max;
//Getters & Setters
}
@JsonIgnoreProperties(ignoreUnknown = true)
public class Weight {
    private int Max;
//Getters & Setters
@Override
        public String toString(){
            return  "Weight=  "+ weight;
        }
}

Sometimes Age or Weight will be missing from the JSON. So if the entire object is missing(show below). How to handle it in my class? I tried using @JsonInclude(Include.NON_NULL) based on this thread, but its not working. Any help is highly appreciated.

{
 "kind": "html",
 "responseCode": 200,
 "DisplayRules": {
  "Age": {
   "Max": 40
  }//missing weight
 }

Adding to this, I was able to address this scenario using "Jackson Tree Model" .has method; but my json has more than 100 "objects"; so its hard to check each and every object for availability.

==================stack trace ========================= java.lang.NullPointerException at com.cards.jsondata.DisplayRules.toString(RuleGroups.java:44) at java.lang.String.valueOf(String.java:2982) at java.lang.StringBuilder.append(StringBuilder.java:131) at com.cards.jsondata.JSONResults.JSONString(JSONResults.java:54) at com.cards.service.GetResultsImpl.getJSONResults(GetResultsImpl.java:61) at com.cards.controller.RunReports.generateReports(RunReports.java:48) 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:497) 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:105) 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:198) 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:80) 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:799) 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:1455) 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)

Jack
  • 1
  • 1
  • If Age or Weight is missing in the JSON, they will be null in the POJOs. As simple as that. – JB Nizet Sep 10 '17 at 13:53
  • @JBNizet Yes, I am getting a null pointer exception. what is the best/right approach to handle that? Add logic in setter? – Jack Sep 10 '17 at 13:56
  • The best approache is to understand why you get that exception, by reading its stack trace. Then to fix the code causing that exception (for example, it that's the right fix, by checking for null). Post the code causing the exception, and the complete and exact exception stack trace. – JB Nizet Sep 10 '17 at 13:59
  • Now read the stack trace. Look at its very first line: `NullPointerException at com.cards.jsondata.DisplayRules.toString(RuleGroups.java:44) `. This line is full of useful information. You know it's a NullPointerException (which means you're calling a method or accessing a field on a null reference). You know it happens in the file RuleGroups.java, in the method toString of the class DisplayRules, at line 44. So, what's this line of code? `return " - " + Weight.toString()` (that's not the actual line of code, BTW, because that wouldn't even compile, but let's imagine it comiles). – JB Nizet Sep 10 '17 at 14:19
  • You're calling toString() on the variable Weight, and you **know** that Weight can be null. So you need to decide what to do when Weight is null. A simple fix would be, for example, to use `return " - " + Weight;`. You should also avoid posting code that doesn't compile, and respect the Java naming conventions. – JB Nizet Sep 10 '17 at 14:20
  • Thanks, I will handle the null values in toString() method. Basic question, "and respect the Java naming conventions", could you please clarify; it would be learning and helpful for me to correct myself... – Jack Sep 10 '17 at 14:37
  • Variables in Java start with a lowercase letter. So Weight and Max are violating these conventions. Although JSON doesn't have official naming conventions, mixing two different conventions in the same document is also not a good idea (responseCode vs. DisplayRules, for example). – JB Nizet Sep 10 '17 at 14:44

0 Answers0