gpt4 book ai didi

java - 如何在 Java Spring 中格式化 JSON 响应?

转载 作者:行者123 更新时间:2023-12-02 10:09:41 25 4
gpt4 key购买 nike

我有一个带有一组私有(private)字段的响应类。前端开发人员要求我以这种 JSON 格式发送服务的响应。迄今为止响应应采用 JSON 格式,如下所示

{
"status": "SUCCESS",
"message": {
"code": "040",
"description": "verified"
},
"qrContent": "aaa | bbb"

}



QrCodePaymentResponse response = new QrCodePaymentResponse();
if (firstThree.equalsIgnoreCase(QRType.EZDYNAMIC.getDescription())) {
axiPayQrCodePaymentService.ezCashDynamicQR(axiPayQrCodePayment,serviceContext);
response.setStatus(RequestStatus.SUCCESS.getStatus());
response.setMessage(----------------);
response.setQrContent(returnValue.getQrContent);

}

如何修改上面的代码以发送请求的格式?谢谢。

最佳答案

使用 spring RestController。它的方法以 JSON 格式返回数据

QrCodePaymentResponse.java

public class QrCodePaymentResponse{
private String response;
private String qrContent;
private Message message;

//set getters and setters
}

消息.java

public class Message{
private String code;
private String description;

//set getters and setters
}

AppController.java

@RestController
public class AppController {

@RequestMapping(value="/get", method=RequestMethod.GET)
public QrCodePaymentResponse getPaymentResponse(){
QrCodePaymentResponse response = new QrCodePaymentResponse();
Message message = new Message();
//set values to message
if (firstThree.equalsIgnoreCase(QRType.EZDYNAMIC.getDescription())) {
axiPayQrCodePaymentService.ezCashDynamicQR(axiPayQrCodePayment,serviceContext);
response.setStatus(RequestStatus.SUCCESS.getStatus());
response.setMessage(message);
response.setQrContent(returnValue.getQrContent);
}
return response;
}
}

关于java - 如何在 Java Spring 中格式化 JSON 响应?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55089422/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com