title: Getting rid of default values in spring rest
body: <p>I'm using Spring rest with below code base:</p>
  
  <p>When I invoke /info by passing string value in request body, I'm expecting the below response if this value is not present in my backend database.</p>
  
  <pre><code>{"output":-10} 
  </code></pre>
  
  <p>but instead it returns me below response:</p>
  
  <pre><code>{"id": 0, "output":-10} 
  </code></pre>
  
  <p>Can any one tell me how to get rid of this id default value? If there is a boolean variable in JSON mapper, then that would also get returned as </p>
  
  <pre><code>{"id": 0, "booleanVar": false, "output":-10} 
  </code></pre>
  
  <p>Can any one tell me how to get rid of this default value?</p>
  
  <p>Controller.java</p>
  
  <pre><code>@RequestMapping(value = "heartbeat", method = RequestMethod.GET, consumes="application/json")
  public ResponseEntity&lt;String&gt; getHeartBeat() throws Exception {
      String curr_time = myService.getCurrentTime();      
      return MyServiceUtil.getResponse(curr_time, HttpStatus.OK);
  }
  
  @RequestMapping(value = "info", method = RequestMethod.POST, consumes="application/json")
  public ResponseEntity&lt;String&gt; getData(@RequestBody String body) throws Exception {
      ....
      myInfo = myService.getMyInfo(myServiceJson);
      return MyServiceUtil.getResponse(myInfo, responseHeader, HttpStatus.OK);
  }
  </code></pre>
  
  <p>MyService.java</p>
  
  <pre><code>@Override
  public String getCurrentTime() throws Exception {
      String currentDateTime = null;
      MyServiceJson json = new MyServiceJson();
      ObjectMapper mapper = new ObjectMapper().configure(SerializationConfig.Feature.DEFAULT_VIEW_INCLUSION, false);
  
      try {           
          Date currDate = new Date(System.currentTimeMillis());
          currentDateTime = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss").format(currDate);           
          json.setCurrentDateTime(currentDateTime);
  
          ObjectWriter writer = mapper.writerWithView(Views.HeartBeatAPI.class);
          return writer.writeValueAsString(json);
  
      } catch (Exception e) {
          throw new Exception("Excpetion in getCurrentTime: ", HttpStatus.BAD_REQUEST);           
      }
  }
  
  @Override
  public String getMyInfo(MyServiceJson myServiceJson) throws Exception {             
      MyServiceJson json = new MyServiceJson();
      json.setFirstName("hhh");
      json.setLastName("abc");
  
      ObjectMapper mapper = new ObjectMapper();
      return mapper.writeValueAsString(json);
  }
  </code></pre>
  
  <p>Views.java</p>
  
  <pre><code>public class Views {
      public static class HeartBeatAPI {  }
  }
  </code></pre>
  
  <p>MyServiceJson.java</p>
  
  <pre><code>@JsonSerialize(include = Inclusion.NON_NULL)
  public class MyServiceJson {
      private int id;
      private String firstName;   
      private String lastName;
  
      @JsonView(Views.HeartBeatAPI.class) 
      private String currentDateTime;
  
      // Getter/Setter for the above variables here
      .....
  
  }
  </code></pre>