title: How to include null values to JSON responses in spring boot REST API
body: <p>I'm in the process of developing a RESTful API. I want to get user details by calling API. But my expected response not included null values.</p>
  <p><strong>Expected Response</strong></p>
  <pre><code>{
      &quot;id&quot;: 1,
      &quot;username&quot;: &quot;admin&quot;,
      &quot;fullName&quot;: &quot;Geeth Gamage&quot;,
      &quot;userRole&quot;: &quot;ADMIN&quot;,
      &quot;empNo&quot;: null
  } 
  </code></pre>
  <p><strong>Actual Response</strong></p>
  <pre><code>  {
          &quot;id&quot;: 1,
          &quot;username&quot;: &quot;admin&quot;,
          &quot;fullName&quot;: &quot;Geeth Gamage&quot;,
          &quot;userRole&quot;: &quot;ADMIN&quot;
      } 
  </code></pre>
  <p>My Spring boot rest API code is below. why not include null parameters for my response?</p>
  <pre><code>@GetMapping(value = &quot;/{userCode}&quot;, produces = MediaType.APPLICATION_JSON_VALUE)
  public ResponseEntity&lt;Object&gt; findUser(@PathVariable String userCode)
  {
      return ResponseEntity.status(HttpStatus.OK).body(userService.getUserByUserName(userCode));
  }
  
  
  @Override
  @Transactional
  public Object getUserByUserName(String username)
  {
      try {
          User user = Optional.ofNullable(userRepository.findByUsername(username))
                     .orElseThrow(() -&gt; new ObjectNotFoundException(&quot;User Not Found&quot;));
              
              return new ModelMapper().map(user, UserDTO.class);
  
      } catch (ObjectNotFoundException ex) {
          log.error(&quot;Exception  :  &quot;, ex);
          throw ex;
      } catch (Exception ex) {
          log.error(&quot;Exception  :  &quot;, ex);
          throw ex;
      }
  }
  </code></pre>
  <p><code>User</code> entity class and <code>UserDTO</code> object class as below</p>
  <p><strong>User.class</strong></p>
  <pre><code>@Data
  @Entity
  @Table(name = &quot;user&quot;)
  public class User{
      @Id
      @GeneratedValue(strategy = IDENTITY)
      @Column(name = &quot;ID&quot;, unique = true, nullable = false)
      private Long id;
      @Column(name = &quot;USERNAME&quot;, nullable = false, length = 64)
      private String username;
      @Column(name = &quot;USER_ROLE&quot;, nullable = false)
      private String userRole;
      @Column(name = &quot;EMP_NO&quot;)
      private String empNo;
  }
  </code></pre>
  <p><strong>UserDTO.class</strong></p>
  <pre><code>@Data
  public class  UserDTO {
      private Long id;
      private String username;
      private String fullName;
      private String userRole;
      private String empNo;
  }
  </code></pre>