SpringBoot:统一JSON信息返回

2020-08-18 16:20:57 浏览数 (2)

调用后端服务后的返回结果有多种类型,如:StringIntegerBooleanListMap等,在一个项目中,为了保持统一性,我们方法返回结果可以全部使用JSON数据格式,如下:

{
    "code":200,
    "msg":"成功",
    "data":"JSON数据"
}

其中code 是本次请求处理结果对应的状态码,msg是状态码对应的解释信息,data是要返回的数据内容,可以是任意一个对象。

封装响应信息对象

public class ResponseEntity<T> implements Serializable {


    private static final long serialVersionUID = 3595741978061989861L;
    private Integer code;//状态码
    private String msg;//状态码对应信息
    private T data;//要返回的数据


    public Integer getCode() {
        return code;
    }


    public void setCode(Integer code) {
        this.code = code;
    }


    public String getMsg() {
        return msg;
    }


    public void setMsg(String msg) {
        this.msg = msg;
    }


    public T getData() {
        return data;
    }


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

状态码,Http请求状态码有多种,使用枚举列举,如下示例:

public enum ResponseEnum {


    SUCCESS(200, "成功"),
    FAIL(-1, "失败"),
    ERROR_400(400, "错误的请求"),
    ERROR_404(404, "访问资源不存在"),
    ERROR_500(500, "服务器异常");


    private Integer code;
    private String msg;


    ResponseEnum(Integer code, String msg) {
        this.code = code;
        this.msg = msg;
    }


    public Integer getCode() {
        return code;
    }


    public void setCode(Integer code) {
        this.code = code;
    }


    public String getMsg() {
        return msg;
    }


    public void setMsg(String msg) {
        this.msg = msg;
    }
}

创建一个公共类,生成响应对象

public class ResponseUtil {


    /**
     * 成功返回
     * @param object 返回数据
     * @return
     */
    public static ResponseEntity success(Object object){
        ResponseEntity resp = new ResponseEntity();
        resp.setCode(ResponseEnum.SUCCESS.getCode());
        resp.setMsg(ResponseEnum.SUCCESS.getMsg());
        resp.setData(object);
        return resp;
    }


    /**
     * 成功返回  无数据
     * @return
     */
    public static ResponseEntity success(){
        return success(null);
    }


    /**
     * 失败返回
     * @param responseEnum 响应标识
     * @return
     */
    public static ResponseEntity error(ResponseEnum responseEnum){
        ResponseEntity resp = new ResponseEntity();
        resp.setCode(responseEnum.getCode());
        resp.setMsg(responseEnum.getMsg());
        return resp;
    }
}

Spring中的控制器可以用 @Controller@RestController注解来声明,其中@Controller标识当前控制器是SpringMvc的控制器,要返回JSON对象数据,需要和@ResponseBody注解联合使用;@RestController主要用来构建Restful风格接口,返回客户端的请求数据,相当于同时使用@Controller@ResponseBody注解。

(推荐课程:Spring教程

创建Pojo包及对应的实体类

public class DemoEntity {


    private Integer id;
    private String name;


    public Integer getId() {
        return id;
    }


    public void setId(Integer id) {
        this.id = id;
    }


    public String getName() {
        return name;
    }


    public void setName(String name) {
        this.name = name;
    }
}

创建控制器

@Controller

@Controller
public class DemoController {


    @RequestMapping(value = "/users", method= RequestMethod.GET)
    @ResponseBody
    public ResponseEntity users(){
        List<DemoEntity> list = new ArrayList<DemoEntity>();
        DemoEntity demo = new DemoEntity();
        demo.setId(1);
        demo.setName("蜗牛");
        list.add(demo);
        DemoEntity demo1 = new DemoEntity();
        demo1.setId(2);
        demo1.setName("葡萄");
        list.add(demo1);
        return ResponseUtil.success(list);
    }
}

测试:启动服务后,在浏览器地址栏输入http://localhost:8080/users,可以看到页面上的输出信息

{"code":200,"msg":"成功","data":[{"id":1,"name":"蜗牛"},{"id":2,"name":"葡萄"}]}

@RestController

@RestController
public class DemoRestController {


    @RequestMapping(value = "/users1", method= RequestMethod.GET)
    public ResponseEntity users(){
        List<DemoEntity> list = new ArrayList<DemoEntity>();
        DemoEntity demo = new DemoEntity();
        demo.setId(1);
        demo.setName("蜗牛");
        list.add(demo);
        DemoEntity demo1 = new DemoEntity();
        demo1.setId(2);
        demo1.setName("葡萄");
        list.add(demo1);
        return ResponseUtil.success(list);
    }
}

请求后也能看到如上的结果。

(推荐教程:Spring Boot 那些事)

以上就是关于SpringBoot:统一JSON信息返回的相关介绍,希望对大家有所帮助。

0 人点赞