封装返回JSON数据或返回thymeleaf
标签:项目总结

封装返回JSON数据

一般Controller在返回的时候有两种情况,一种是 rest api json输入,一种是页面。

一般与前端做JSON格式约定:

{
    "code": 500205,
    "msg": "密码错误",
    "data": null
}

则可以新建两个文件:

Result

import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
public class Result<T> {
    private int code;
    private String msg;
    private T data;


    private Result(T data) {
        this.code = 0;
        this.msg = "success";
        this.data = data;
    }

    public Result(CodeMsg cm) {
        if (cm == null) {
            return;
        }
        this.code = cm.getCode();
        this.msg = cm.getMsg();
    }

    /**
     * 成功的时候只需要传data数据就行
     *
     * @param data 成功时的数据
     * @param <T>
     * @return
     */
    public static <T> Result<T> success(T data) {
        return new Result<T>(data);
    }

    /**
     * 失败的时候传CodeMsg
     *
     * @param cm  失败的CodeMsg
     * @param <T>
     * @return
     */
    public static <T> Result<T> error(CodeMsg cm) {
        return new Result<T>(cm);
    }
}

CodeMsg

import lombok.Getter;
import lombok.Setter;

@Setter
@Getter
public class CodeMsg {
    private int code;
    private String msg;
    //通用模块
    public static CodeMsg SUCCESS = new CodeMsg(0, "success");
    public static CodeMsg SERVER_ERROR = new CodeMsg(500100, "服务端异常");
    public static CodeMsg BIND_ERROR = new CodeMsg(500101, "参数校验异常:%s");
    //登录模块
    public static CodeMsg USER_ERROR = new CodeMsg(500200, "用户输入异常");
    public static CodeMsg PASSWORD_EMPTY = new CodeMsg(500201, "登录密码不能为空");
    public static CodeMsg MOBILE_EMPTY = new CodeMsg(500202, "手机号不能为空");
    public static CodeMsg MOBILE_ERROR = new CodeMsg(500203, "手机号格式错误");
    public static CodeMsg MOBILE_NO_EXIST = new CodeMsg(500204, "用户不存在");
    public static CodeMsg PASSWORD_ERROR = new CodeMsg(500205, "密码错误");

    private CodeMsg(int code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    /**
     * 变长的错误参数
     *
     * @param args
     * @return
     */
    public CodeMsg fillArgs(Object... args) {
        int code = this.code;
        String message = String.format(this.msg, args);
        return new CodeMsg(code, message);
    }
}

返回thymeleaf

SpringBoot使用thymeleaf:

导入maven依赖:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-thymeleaf</artifactId>
    <version>2.0.3.RELEASE</version>
</dependency>

在resources新建application.properties填入配置:(更多配置:打开搜thymeleaf

#thymeleaf
spring.thymeleaf.cache=false
spring.thymeleaf.enabled=true
spring.thymeleaf.encoding=UTF-8
spring.thymeleaf.mode=HTML5
spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.suffix=.html

上面是在 /templates目录下寻找以html为后缀的文件:

新建html:

<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <title>hello</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
</head>
<body>
<p th:text="'hello:'+${name}"></p>
</body>
</html>

上面主要是引入了 <html xmlns:th="http://www.thymeleaf.org">

更多的查看:

  1. thymeleaf快速入门教程
  2. Spring Boot (三):Thymeleaf 的使用
  3. 官方文档
  • 3 min read

CONTRIBUTORS


  • 3 min read