简体   繁体   English

如何忽略 REST API Java 中的响应中的某些字段

[英]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.我写了两个微服务,微服务 A 调用微服务 B 并将响应返回给微服务 A。需求很简单,验证信用卡号并在响应中返回成功或失败。 Please find my code below:请在下面找到我的代码:

Microservice A:微服务 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 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:微服务 B:

ValidateCCResource.java验证CCResource.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 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.我想在响应中忽略ccNumber ,我尝试使用@JsonIgnore但它不起作用 - 有人可以帮助如何从我的响应中删除 ccNumber。

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.如果您不想在响应中包含null对象,则可以在模型/数据/dto class 的属性上使用@JsonInclude(Include.NON_NULL)注释。

  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.如果您根本不想包含任何特定字段,则可以在模型/数据/dto class 中使用@Transient注释该属性。

How's it working:它是如何工作的:

So, all your model/data/dto classes get serialized when they are converted into JSON from Object.因此,当您的所有模型/数据/dto 类从 Object 转换为 JSON 时,它们都会被序列化。 While this serialization takes place, you can decide your customized logic to determine what the final JSON response would be.在发生此序列化时,您可以决定自定义逻辑以确定最终的 JSON 响应是什么。 This is achieved simply by the available annotations in Java (and or whatever library you are using for Serialization).这可以简单地通过 Java(和或您用于序列化的任何库)中的可用注释来实现。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 忽略来自Rest API Response Java的Null值字段 - Ignore Null Value Fields From Rest API Response Java 解析其余API的json响应并删除某些jsonObjects-JAVA - Parse json response of rest API and delete certain jsonObjects - JAVA 我如何忽略 JSON 响应中列表中的特定字段 - How do i ignore a specific field from an list in JSON response 如何从android(java)中的json响应中获取内部索引对象,该对象对rest api调用使用改造 - How to get inner indexed object from json response in android (java) which use retrofit for rest api calls java - 当响应代码为 4xx 时,如何从 REST API 解析响应正文 - How to parse reponse body from REST API when response code is 4xx in java 我想从java中的REST API返回XML格式的响应 - I want to return response in XML form from REST API in java 如何固定响应时间以在Java中调用REST API? - How to fixed response time to call REST API in java? 如何在java代码中获取Spring rest API响应? - How to get Spring rest API response in java code? 如何在json响应中获取数组的大小? 放心API、JAVA - How get size of an array in json response? rest assured API, JAVA 如何在java中进行rest api调用并映射响应对象? - How to make a rest api call in java and map the response object?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM