title: How to ignore certain field from response in REST API Java
body: <p>I've written two microservices, Microservice A is calling Microservice B and returning the response to Microservice A. The requirement is very simple to validate the credit card number and return success or failure in the response. Please find my code below:</p>
  <p><strong>Microservice A:</strong></p>
  <pre><code>@RestController
  public class TestController {
  
      @PostMapping(&quot;/users&quot;)
      public CreditCard createUser(@RequestBody CreditCard creditCard) {
  
          Map&lt;String, String&gt; ccDetails = new HashMap&lt;&gt;();
          ccDetails.put(&quot;ccNumber&quot;, creditCard.getCcNumber());
  
          ResponseEntity&lt;CreditCard&gt; responseEntity = new RestTemplate()
                  .getForEntity(&quot;http://localhost:8081/validation/cc/{ccNumber}&quot;, CreditCard.class, ccDetails);
  
          return responseEntity.getBody();
      }
  }
  </code></pre>
  <p><strong>CreditCard.java</strong></p>
  <pre><code>public class CreditCard {
  
      private String status;
      private String ccNumber;
  
      public CreditCard() {
      }
  
      public String getStatus() {
          return status;
      }
      public String getCcNumber() {
          return ccNumber;
      }
      public void setCcNumber(String ccNumber) {
          this.ccNumber = ccNumber;
      }
  
  }
  </code></pre>
  <p><strong>Microservice B:</strong></p>
  <p><strong>ValidateCCResource.java</strong></p>
  <pre><code>@RestController
  @RequestMapping(&quot;/validation&quot;)
  public class ValidateCCResource {
  
      @GetMapping(&quot;/cc/{ccNumber}&quot;)
      public CreditCardValidation validateCreditCardApplication(@PathVariable String ccNumber) {
  
          CreditCardValidation response = new CreditCardValidation();
          if (ccNumber.length() == 16) {
              System.out.println(&quot;valid credit card - success&quot;);
              response.setStatus(&quot;Credit Card Number is Valid&quot;);
          } else {
              response.setStatus(&quot;Credit Card Number is Not Valid&quot;);
          }
          return response;
      }
  }
  </code></pre>
  <p><strong>CreditCardValidation.java</strong></p>
  <pre><code>public class CreditCardValidation {
  
      private String status;
  
      public CreditCardValidation() {
      }
  
  public void setStatus(String status) {
      this.status = status;
  }
  
      public String getStatus() {
          return status;
      }
  }
  </code></pre>
  <p><strong>Request Body:</strong></p>
  <pre><code>{
      &quot;ccNumber&quot;: &quot;1234432232232233&quot;
  }
  </code></pre>
  <p><strong>Actual Response:</strong></p>
  <pre><code>{
      &quot;status&quot;: &quot;Credit Card Number is Valid&quot;,
      &quot;ccNumber&quot;: null
  }
  </code></pre>
  <p><strong>Expected Response:</strong></p>
  <pre><code>{
      &quot;status&quot;: &quot;Credit Card Number is Valid&quot;
  }
  </code></pre>
  <p>I would like to ignore <strong>ccNumber</strong> in the response, I tried using <code>@JsonIgnore</code> but its not working- can someone please help how to get rid of the ccNumber from my response.</p>