[英]What is the best way to define a common Base Java Model for all Responses?
I have a need to format all REST responses as:我需要将所有 REST 响应格式化为:
{
"response": {}
"status": "ERROR"
"error": {
"status": "ERROR",
"errors": [
{
"type": "ERROR",
"code": "E1001",
"message": "This is error 1.",
"reference": "TEST 1"
},
{
"type": "ERROR",
"code": "E1002",
"message": "This is error 2.",
"reference": "TEST 2"
}
]
}
}
Where the response
is an arbitrary data (object, array; with arbitrary fields) different for each REST API endpoint.其中response
是任意数据(对象、数组;具有任意字段)对于每个 REST API 端点都不同。
What is the best way to define different response for each response Model?为每个响应 Model 定义不同响应的最佳方法是什么?
I tried this (and it works, but I'm not sure is it the right thing to do):我试过这个(它有效,但我不确定这样做是否正确):
BaseResponseModel.java BaseResponseModel.java
public class BaseResponseModel {
private String status = "SUCCESS";
private List<ErrorExtendedModel> errors = new ArrayList<>();
// Various constructors, getters and setters
}
and the subclass:和子类:
CustomerResponseModel.java CustomerResponseModel.java
public class CustomerResponseModel extends BaseResponseModel {
public List<Response> response = new ArrayList<>();
public static class Response {
private String customerId;
private String firstName;
private String lastName;
public Response(String customerId, String firstName, String lastName) {
this.customerId = customerId;
this.firstName = firstName;
this.lastName = lastName;
}
// getters and setters of inner class
}
public CustomerResponseModel() {
super();
}
public List<Response> getResponse() {
return response;
}
public void setResponse(List<Response> response) {
this.response = response;
}
}
I used inner static class with fields in every subclass but I'm not sure is it the correct way to do.我在每个子类中都使用了内部 static class字段,但我不确定它是否正确。
In this case I wouldn't go for inheritance but rather go for composition and leverage generics.在这种情况下,我不会 go 用于 inheritance 而是 go 用于组合和利用 Z56B97998B338B5FDFFFFF56B97998B338B93748 The advantage of this approach is that you don't require nested classes, you enforce the base REST model throughout your application while keeping the response type generic.这种方法的优点是您不需要嵌套类,您在整个应用程序中强制执行基本 REST model,同时保持响应类型通用。
BaseResponseModel.java: BaseResponseModel.java:
public class BaseResponseModel<T> {
private T response;
private String status = "SUCCESS";
private List<ErrorExtendedModel> errors = new ArrayList<>();
public BaseResponseModel(T response) {
this.response = response;
}
// ...
}
CustomerResponseModel.java: CustomerResponseModel.java:
public class CustomerResponseModel {
private String customerId;
private String firstName;
private String lastName;
public CustomerResponseModel(String customerId, String firstName, String lastName) {
this.customerId = customerId;
this.firstName = firstName;
this.lastName = lastName;
}
// ...
}
Example of the usage:用法示例:
public class RestApiController {
public BaseResponseModel<CustomerResponseModel> getOneCustomer(String customerId) {
return new BaseResponseModel(new CustomerResponseModel(...));
}
public BaseResponseModel<List<CustomerResponseModel>> getAllCustomers() {
return new BaseResponseModel(List.of(new CustomerResponseModel(...), new CustomerResponseModel(...)));
}
// ...
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.