主要就是先搭建起来SpringBoot的项目框架(IDEA版)
这下面的是引入thyme leaf模板和对Result结果封装
pom文件(引入了thymeleaf的依赖)
<modelVersion>4.0.0</modelVersion>
<groupId>com.imooc</groupId>
<artifactId>miaosha_1</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>miaosha_1</name>
<url>http://maven.apache.org</url>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.9.RELEASE</version>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
</dependencies>
</project>
application.properties中代码
配置thymeleaf
spring.thymeleaf.cache=false
spring.thymeleaf.content-type=text/html
spring.thymeleaf.enabled=true
spring.thymeleaf.encoding=UTF-8
spring.thymeleaf.mode=HTML5
spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.suffix=.html
要访问的网页
templates中的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>
对Result结果进行封装
我们希望输出的json数据是这样的
默认code=0是成功的
{ “code”:
“msg”:
“data”:json对象或者数组都行
}
result中CodeMsg.java代码
这是对code和msg的封装,方便遇到错误的时候调用
public class CodeMsg {
private int code;
private String msg;
//通用异常
//默认code=0是成功的
public static CodeMsg SUCCESS = new CodeMsg(0, "success");
public static CodeMsg SERVER_ERROR = new CodeMsg(500100, "服务端异常");
//登录模块 5002XX
//商品模块 5003XX
//订单模块 5004XX
//秒杀模块 5005XX
get和set方法
}
result中Result.java代码
public class Result<T> {
private int code;
private String msg;
private T data;
/**
* 成功时候的调用,只需要数据
* */
public static <T> Result<T> success(T data){
return new Result<T>(data);
}
/**
* 失败时候的调用,需要知道code和msg
* */
public static <T> Result<T> error(CodeMsg cm){
return new Result<T>(cm);
}
//success时的构造函数,定义成私有的都应该懂吧
private Result(T data) {
this.code = 0;
this.msg = "success";
this.data = data;
}
//fail时的构造函数
private Result(CodeMsg cm) {
if(cm == null) {
return;
}
this.code = cm.getCode();
this.msg = cm.getMsg();
}
get方法
}
Controller中的代码
@Controller
@RequestMapping("/demo")
public class DemoController {
@RequestMapping("/")
@ResponseBody
String home() {
return "Hello World!";
}
//1.rest api json输出 2.页面
@RequestMapping("/hello")
@ResponseBody
public Result<String> hello() {
return Result.success("hello,imooc");//成功的时候这样写,所以在 result中定义了对应的方法
// return new Result(0, "success", "hello,imooc");这样写是硬编码
}
@RequestMapping("/helloError")
@ResponseBody
public Result<String> helloError() {
return Result.error(CodeMsg.SERVER_ERROR);//异常的时候这样写
//return new Result(500102, "XXX");这样写是硬编码
}
@RequestMapping("/thymeleaf")
public String thymeleaf(Model model) {
model.addAttribute("name", "Joshua");
return "hello";
}
}