首页 > 其他分享 >springboot返回结果包装统一返回格式

springboot返回结果包装统一返回格式

时间:2024-09-07 16:35:51浏览次数:5  
标签:返回 code springboot springframework import msg 格式 String

 

统一返回结果拦截处理类

import com.itcoder.test.utils.JsonUtils;
import com.sun.istack.internal.NotNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.MethodParameter;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.http.server.ServletServerHttpRequest;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice;

import java.util.List;

/**
 * 对返回结果统一进行处理,包括返回结果格式统一包装,返回异常统一处理
 *
 */
@Slf4j
@RestControllerAdvice
public class RestResponseAdvice implements ResponseBodyAdvice<Object> {
    @Override
    public boolean supports(MethodParameter returnType, Class<? extends HttpMessageConverter<?>> converterType) {
        return true;
    }
    @Value("ignoreUris")
    private List<String> ignoreUris;
    /**
     * 返回结果包装统一返回格式
     * @return 包装后的返回结果
     */
    @Override
    public Object beforeBodyWrite(Object body, MethodParameter returnType, MediaType selectedContentType,
                                  Class<? extends HttpMessageConverter<?>> selectedConverterType,
                                  ServerHttpRequest request, ServerHttpResponse response) {
        if (ignoreUris.contains(request.getURI().getPath())){
            return body;
        }
        // 指定返回的结果为application/json格式
        // 不指定,String类型转json后返回Content-Type是text/plain;charset=UTF-8
        response.getHeaders().setContentType(MediaType.APPLICATION_JSON);
        ResultJson result = new ResultJson(body);
        // 若返回类型是ResultJson,则不进行修改
        if (body == null) {
            if (returnType.getParameterType().isAssignableFrom(String.class)) {
                return JsonUtils.toJsonString(result);
            }
        }
        if (body instanceof ResultJson) {
            return body;
        }
        if (body instanceof String) {
            return  JsonUtils.toJsonString(result);
        }
        return result;
    }
}

 

统一信息包装类

import lombok.Data;

/**
 * 统一返回信息包装类
 *
 * @date 2022/07/15
 */
@Data
public class ResultJson {

    private static final String SUCCESS_CODE = "0000";

    /**
     * 成功失败的状态值,true:成功;false:失败
     * 这里返回编码为:“0000”,系统就认为接口成功;其他值,代表失败
     */
    private Boolean status;

    /**
     * 状态码 正确为0000
     */
    private String code;

    /**
     * 返回提示信息
     */
    private String msg;

    /**
     * 返回数据
     */
    private Object data;

    public ResultJson() {
        this.status = true;
        this.code = SUCCESS_CODE;
        this.msg = "";
    }

    public ResultJson(Object data) {
        this.status = true;
        this.code = SUCCESS_CODE;
        this.msg = "";
        this.data = data;
    }

    public ResultJson(String code, String msg) {
        this.status = SUCCESS_CODE.equals(code);
        this.code = code;
        this.msg = msg;
    }

    /**
     * 如果返回状态码非0000,且接口状态为成功,请使用这个
     * @param status 接口请求状态
     * @param code 返回code值
     * @param msg 返回消息
     * @param data 返回数据
     */
    public ResultJson(Boolean status, String code, String msg, Object data) {
        this.status = status;
        this.code = code;
        this.msg = msg;
        this.data = data;
    }
}

 

标签:返回,code,springboot,springframework,import,msg,格式,String
From: https://www.cnblogs.com/fyiyy/p/18401860

相关文章

  • LeetCodeTest算法测试 传递一个数组和一个特定的目标整型数字,返回的两个数组元素相加
    1importjava.util.ArrayList;2importjava.util.List;34publicclassLeetCodeTest{5publicstaticvoidmain(String[]args){67int[]intArr=newint[]{2,7,11,15};8List<CustomerIntIndex>customerIntIndexL......
  • springboot流浪天使乐园管理系统
    基于springboot+vue实现的流浪天使乐园管理系统(源码+L文+ppt)4-039第4章系统设计   4.1总体功能设计一般个人用户和管理者都需要登录才能进入流浪天使乐园管理系统,使用者登录时会在后台判断使用的权限类型,包括一般使用者和管理者,一般使用者只能对流浪动物提供查阅......
  • springboot小儿推拿培训系统
     基于springboot+vue实现的小儿推拿培训系统 (源码+L文+ppt)4-50                             3系统设计    3.1系统功能结构系统结构图可以把杂乱无章的模块按照设计者的思维方式进行调整排序,可以让设计者在之后的......
  • springboot流浪天使乐园管理系统
    基于springboot+vue实现的流浪天使乐园管理系统(源码+L文+ppt)4-039第4章系统设计   4.1总体功能设计一般个人用户和管理者都需要登录才能进入流浪天使乐园管理系统,使用者登录时会在后台判断使用的权限类型,包括一般使用者和管理者,一般使用者只能对流浪动物提供查阅......
  • springboot流浪天使乐园管理系统
    基于springboot+vue实现的流浪天使乐园管理系统(源码+L文+ppt)4-039第4章系统设计   4.1总体功能设计一般个人用户和管理者都需要登录才能进入流浪天使乐园管理系统,使用者登录时会在后台判断使用的权限类型,包括一般使用者和管理者,一般使用者只能对流浪动物提供查阅......
  • springboot流浪天使乐园管理系统
    基于springboot+vue实现的流浪天使乐园管理系统(源码+L文+ppt)4-039第4章系统设计   4.1总体功能设计一般个人用户和管理者都需要登录才能进入流浪天使乐园管理系统,使用者登录时会在后台判断使用的权限类型,包括一般使用者和管理者,一般使用者只能对流浪动物提供查阅......
  • json字符串转义格式化后再转换处理demo StringEscapeUtils.unescapeJava
    json字符串转义格式化后再转换处理demoStringEscapeUtils.unescapeJava报错关键字:illegalidentifierExpectedBEGIN_OBJECTbutExpectednameatpackagecom.example.core.mydemo;importcom.alibaba.fastjson.JSON;importcom.fasterxml.jackson.core.JsonProcessingE......
  • 返回结果的HTTP状态码
    2XX状态码(246)    表明请求被正常处理了。    200OK            表示从客户端发来的请求在服务器端被正常处理了。    204NoContent            表示请求处理成功,但是没有资源可以返回。    206P......
  • 280java jsp SSM Springboot旅游推荐系统旅游景点路线管理(源码+文档+开题+PPT+运行视
    项目技术:Springboot+Maven+Vue等等组成,B/S模式+Maven管理等等。环境需要1.运行环境:最好是javajdk1.8,我们在这个平台上运行的。其他版本理论上也可以。2.IDE环境:IDEA,Eclipse,Myeclipse都可以。推荐IDEA;3.tomcat环境:Tomcat7.x,8.x,9.x版本均可4.硬件环境:windows......
  • springboot整合es
    1.引入依赖在pom.xml中添加SpringBoot和Elasticsearch相关的依赖:xml复制代码<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-elasticsearch</artifactId></dependency><depe......