为所有响应定义通用基础 Java 模型的最佳方法是什么?
What is the best way to define a common Base Java Model for all Responses?
我需要将所有 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"
}
]
}
}
其中 response
是任意数据(对象、数组;具有任意字段)不同 每个 REST API 端点。
为每个响应模型定义不同响应的最佳方法是什么?
我试过了(而且它有效,但我不确定这样做是否正确):
BaseResponseModel.java
public class BaseResponseModel {
private String status = "SUCCESS";
private List<ErrorExtendedModel> errors = new ArrayList<>();
// Various constructors, getters and setters
}
和子class:
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;
}
}
我使用 inner static class 每个子class 中的字段,但我不确定这是正确的方法。
在这种情况下,我不会选择继承,而是选择组合并利用泛型。这种方法的优点是您不需要嵌套 类,您在整个应用程序中强制执行基本 REST 模型,同时保持响应类型通用。
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:
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;
}
// ...
}
用法示例:
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(...)));
}
// ...
}
我需要将所有 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"
}
]
}
}
其中 response
是任意数据(对象、数组;具有任意字段)不同 每个 REST API 端点。
为每个响应模型定义不同响应的最佳方法是什么?
我试过了(而且它有效,但我不确定这样做是否正确):
BaseResponseModel.java
public class BaseResponseModel {
private String status = "SUCCESS";
private List<ErrorExtendedModel> errors = new ArrayList<>();
// Various constructors, getters and setters
}
和子class:
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;
}
}
我使用 inner static class 每个子class 中的字段,但我不确定这是正确的方法。
在这种情况下,我不会选择继承,而是选择组合并利用泛型。这种方法的优点是您不需要嵌套 类,您在整个应用程序中强制执行基本 REST 模型,同时保持响应类型通用。
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:
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;
}
// ...
}
用法示例:
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(...)));
}
// ...
}