[英]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:-如果我正确地回答了您的问题,您可以执行以下操作:-
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)
注释。
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.