首页 > 其他分享 >Http ResponseEntity 返回包装

Http ResponseEntity 返回包装

时间:2023-03-07 15:34:12浏览次数:52  
标签:status Http 包装 ResponseStatus ResponseEntity message null data public

public class ResponseEntityFactory {

    private enum ResponseStatus {
        SUCCESS, FAIL, ERROR
    }

    public static ResponseEntity success (String message, Object data) {
        return getResponseEntity(HttpStatus.OK, ResponseStatus.SUCCESS, message, data, null);
    }

    /* ADDITIONAL SUCCESS, FAIL, ERROR FACTORY METHODS */

    private static ResponseEntity getResponseEntity(HttpStatus httpStatus, ResponseStatus responseStatus, String message, Object data, HttpHeaders headers) {

        if (httpStatus == null) throw new IllegalArgumentException("HttpStatus cannot be null");
        if (responseStatus == null) throw new IllegalArgumentException("ResponseStatus cannot be null");

        JsonResponse jsonResponse = new JsonResponse(responseStatus, message, data);
        return new ResponseEntity(jsonResponse, headers, httpStatus);
    }

    private static class JsonResponse {

        private ResponseStatus status;
        private String message;
        private Object data;

        public JsonResponse(ResponseStatus status, String message, Object data) {
            if (status == null) throw new IllegalArgumentException("ResponseStatus cannot be null");
            this.status = status;
            this.message = message;
            this.data = data;
        }

        public ResponseStatus getStatus() {
            return status;
        }

        public void setStatus(ResponseStatus status) {
            this.status = status;
        }

        public String getMessage() {
            return message;
        }

        public void setMessage(String message) {
            this.message = message;
        }

        public Object getData() {
            return data;
        }

        public void setData(Object data) {
            this.data = data;
        }

    }

}

 

示例控制器。java

@Controller
@RequestMapping(value = "/example")
public class ExampleController {

    @Autowire
    private UserService userService;        

    @RequestMapping(method = RequestMethod.GET)
    public ResponseEntity getUsers() {
        List<User> users = userService.getAllUsers();
        return AjaxResponseFactory.success("Found users", userList);
    }

    @RequestMapping(value = "/active/{id}" method = RequestMethod.GET)
    public ResponseEntity isUserActive(long userId) {
        boolean = userService.isUserActive(userId);
        return AjaxResponseFactory.success(null, boolean);
    }
}

标签:status,Http,包装,ResponseStatus,ResponseEntity,message,null,data,public
From: https://www.cnblogs.com/yhc-love-cl/p/17188266.html

相关文章

  • 《渗透测试》抓包技术&HTTPS协议&APP&小程序&PC应用&WEB&转发联动 2023 day6&7
      准备工作:1、浏览器安装证书:解决本地抓HTTPS1.1打开burpsuite的Proxy模块   1.2点击下方的import/exportCAcertificate选择输出的第一个按钮 1.3......
  • http需要掌握的知识
    http协议的报文格式:请求报文:响应报文:状态行响应头sever服务器类型实现抓fiddlerfiddler抓包请求和响应:抓出来的内容东西有哪些http请求头和响应头中的常用字段User-Agent......
  • 了解HTTP请求和响应
    介绍HTTP(超文本传输协议)是一种用于Web服务器和客户端之间通信的协议。它是万维网上数据通信的基础。HTTP请求和响应是Web通信的重要组成部分,允许客户端向服务器请求资......
  • Https工作流程
      1.Client 发起一个 HTTPS 的请求,连接 443 端口,这个过程可以理解成是请求公钥的过程2.Server 端收到请求后,通过第三方机构私钥加密,会把数字证书(也可以认为......
  • 如何使用HTTPS加密保护网站?
    加密Web 内容并不是什么新鲜事:自发布通过SSL/TLS协议来加密Web 内容的规范以来,已经过去了近20 年。然而,近年来,运行安全的HTTPS加密Web 服务器已经从一种选择变成了......
  • edge禁止自动https
    问题描述http网站自动被https,搞的莫名其妙,经过查询得知被自动http->https(如果可行的话)微软为Edge浏览器引入自动HTTPS切换功能问题解决edge://flags/#edge-automati......
  • okhttp Interceptor
    Interceptor介绍okhttp的拦截器就是将整个请求网络的过程的每一步都封装在不同的Interceptor里,这样说可能有点绕,简单点说就是把一个List里的Interceptor都顺序执行一遍,那......
  • HttpClient单例
    publicclassHttpClientFactory{privatestaticHttpClient_httpClient=null;staticHttpClientFactory(){_httpClie......
  • HttpClient长连接
    privatestaticreadonlyHttpClient_httpClient;privateApiLoggerOptions_config;staticApiLogger(){_httpClient=newHttpClient();_httpClient.Timeout=......
  • .NET(C#) HttpClient单例(Singleton)和每次请求new HttpClient对比
    本文主要介绍.NET(C#)中,使用HttpClient执行求时,每次请求都执行newHttpClient创建一个实例和每次请求都使用同一个HttpClient(单例Singleton)分比区别。 1、每次请求创......