简体   繁体   中英

How to ignore certain field from response in REST API Java

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:

Microservice A:

@RestController
public class TestController {

    @PostMapping("/users")
    public CreditCard createUser(@RequestBody CreditCard creditCard) {

        Map<String, String> ccDetails = new HashMap<>();
        ccDetails.put("ccNumber", creditCard.getCcNumber());

        ResponseEntity<CreditCard> responseEntity = new RestTemplate()
                .getForEntity("http://localhost:8081/validation/cc/{ccNumber}", CreditCard.class, ccDetails);

        return responseEntity.getBody();
    }
}

CreditCard.java

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;
    }

}

Microservice B:

ValidateCCResource.java

@RestController
@RequestMapping("/validation")
public class ValidateCCResource {

    @GetMapping("/cc/{ccNumber}")
    public CreditCardValidation validateCreditCardApplication(@PathVariable String ccNumber) {

        CreditCardValidation response = new CreditCardValidation();
        if (ccNumber.length() == 16) {
            System.out.println("valid credit card - success");
            response.setStatus("Credit Card Number is Valid");
        } else {
            response.setStatus("Credit Card Number is Not Valid");
        }
        return response;
    }
}

CreditCardValidation.java

public class CreditCardValidation {

    private String status;

    public CreditCardValidation() {
    }

public void setStatus(String status) {
    this.status = status;
}

    public String getStatus() {
        return status;
    }
}

Request Body:

{
    "ccNumber": "1234432232232233"
}

Actual Response:

{
    "status": "Credit Card Number is Valid",
    "ccNumber": null
}

Expected Response:

{
    "status": "Credit Card Number is Valid"
}

I would like to ignore ccNumber in the response, I tried using @JsonIgnore but its not working- can someone please help how to get rid of the ccNumber from my response.

If I get your question correctly, here is what you can do:-

  1. If you don't want to include null objects to be part of your response, then you can use @JsonInclude(Include.NON_NULL) annotation on the property of you model/data/dto class.

  2. If you want not to include any specific field at all, then you can annotate that property with @Transient in the model/data/dto class.

How's it working:

So, all your model/data/dto classes get serialized when they are converted into JSON from Object. While this serialization takes place, you can decide your customized logic to determine what the final JSON response would be. This is achieved simply by the available annotations in Java (and or whatever library you are using for Serialization).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM