简体   繁体   English

为所有响应定义公共基础 Java Model 的最佳方法是什么?

[英]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.

相关问题 用Java定义泛型方法的最佳方法是什么? - What is the best way to define Generic Method in Java? 在 Java 中存储字符串的公共部分的最佳方法是什么? - What is the best way to store a common part of Strings in Java? 在Java中支持嵌套对象模型的最佳方法是什么? - What is the best way to support nested object model in java? 使用 Java Hibernate 和 Z9CE3D1BD8890F916A0C44809 获得 model 友谊的最佳方式是什么 - What's the best way to model Friendship using Java Hibernate and JPA 在 Java 应用程序中使用经过训练的 LibSVM 模型的最佳方法是什么? - What is the best way to use a trained LibSVM model inside a Java application? 使用页面对象模型(java)处理导航的最佳方法是什么 - What is the best way to handle navigation using page object model (java) 原子替换Java中List中所有内容的最佳方法是什么? - What is the best way to atomic-replace all contents in List in Java? 在Java类中打印字段的所有值的最佳方法是什么 - what is the best way to print all the values of the fields in a java class 解决所有Java字节都已签名这一事实的最佳方法是什么? - What is the best way to work around the fact that ALL Java bytes are signed? 在Cucumber中存储REST API响应的最佳方法是什么 - What's the best way to store REST API responses in Cucumber
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM