title: Trying to Validate JSON using Jackson through Spring Boot Rest
body: <p>Am trying to create a RESTful Web Service using Spring Boot which will take in a JSON and validate it using Jackson.</p>
  
  <p>Here's the RESTful Web Service:</p>
  
  <pre><code>import java.util.Map;
  
  import org.springframework.web.bind.annotation.RequestBody;
  import org.springframework.web.bind.annotation.RequestMapping;
  import org.springframework.web.bind.annotation.RequestMethod;
  import org.springframework.web.bind.annotation.RestController;
  
  import com.fasterxml.jackson.core.JsonFactory;
  import com.fasterxml.jackson.core.JsonParser;
  import com.google.gson.Gson;
  
  @RestController
  @RequestMapping("/myservice")
  public class ValidationService {    
  
      @RequestMapping(value="/validate", method = RequestMethod.POST)
      public void validate(@RequestBody Map&lt;String, Object&gt; payload) throws Exception {
          Gson gson = new Gson();
          String json = gson.toJson(payload); 
          System.out.println(json);
          boolean retValue = false;
  
          try {
              retValue = Validator.isValid(json);
          } 
          catch(Throwable t) {
              t.printStackTrace();
          }
          System.out.println(retValue);
  
      }
  }
  </code></pre>
  
  <p>Here's the code to the Validator:</p>
  
  <pre><code>import java.io.IOException;
  
  import com.fasterxml.jackson.core.JsonParseException;
  import com.fasterxml.jackson.core.JsonParser;
  import com.fasterxml.jackson.databind.DeserializationFeature;
  import com.fasterxml.jackson.databind.ObjectMapper;
  
  public class Validator {
  
      public static boolean isValid(String json) {
          boolean retValue = false;
          try {
              ObjectMapper objectMapper = new ObjectMapper();
              objectMapper.enable(DeserializationFeature.FAIL_ON_READING_DUP_TREE_KEY);
              JsonParser parser = objectMapper.getFactory().createParser(json);
              while (parser.nextToken() != null) {}
              retValue = true;
              objectMapper.readTree(json);
          }catch(JsonParseException jpe) {
              jpe.printStackTrace();
          }
          catch(IOException ioe) {
  
          }
          return retValue;
      }
  }
  </code></pre>
  
  <p>So, when I use a curl to send a valid JSON:</p>
  
  <pre><code>curl -H "Accept: application/json" -H "Content-type: application/json" \ 
  -X POST -d '{"name":"value"}' http://localhost:8080/myservice/validate
  </code></pre>
  
  <p>I receive the following to stdout:</p>
  
  <pre><code>{"name":"value"}
  true
  </code></pre>
  
  <p>But when use the following curl command for invalid JSON (deliberately removed the closing curly brace):</p>
  
  <pre><code>curl -H "Accept: application/json" -H "Content-type: application/json" \
   -X POST -d '{"name":"value"' http://localhost:8080/myservice/validate
  </code></pre>
  
  <p>I receive the following inside stdout:</p>
  
  <pre><code>{"timestamp":1427698779063,
   "status":400,"error":
   "Bad Request",
   "exception":"org.springframework.http.converter.HttpMessageNotReadableException",
   "message":"Could not read JSON: 
   Unexpected end-of-input: expected close marker for OBJECT 
   (from [Source: java.io.PushbackInputStream@1edeb3e; line: 1, column: 0])\n
   at [Source: java.io.PushbackInputStream@1edeb3e; line: 1, column: 31]; 
   nested exception is
   com.fasterxml.jackson.core.JsonParseException: 
   Unexpected end-of-input: expected close marker for OBJECT 
   (from [Source: java.io.PushbackInputStream@1edeb3e; line: 1, column: 0])\n 
   at [Source: java.io.PushbackInputStream@1edeb3e; line: 1, column: 31]",
   "path":"/myservice/validate"
  </code></pre>
  
  <p>Is there a way to ensure exception is handled on the server side but not thrown in stdout and then just have my code respond with:</p>
  
  <pre><code>false
  </code></pre>
  
  <p>Thanks for taking the time to read this...</p>