1.rest api
创建maven工程,在pom文件中导入依赖
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.9.RELEASE</version>
</parent>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
然后创建DemoController类,运行即可
@Controller
@EnableAutoConfiguration
public class DemoController {
@RequestMapping("/")
@ResponseBody
String home() {
return "hello world!";
}
public static void main(String[] args) throws Exception{
SpringApplication.run(DemoController.class, args);
}
}
在浏览器输入localhost:8080即可看到hello world!字样。
对结果集进行封装,对于成功结果,我们只需要知道data数据,对于失败结果,我们需要对具体的code 和 msg进行封装。
public class Result<T> {
private int code;
private String msg;
private T data;
// 将构造函数私有化,不允许其他人通过new对象获得对象
private Result(T data) {
this.code = 0;
this.msg = "success";
this.data = data;
}
private Result(CodeMsg codeMsg) {
if (codeMsg == null) {
return;
}
this.code = codeMsg.getCode();
this.msg = codeMsg.getMsg();
}
/**
* 成功时候的调用
*
* @param data
* @return
*/
public static <T> Result<T> success(T data) {
return new Result<T>(data);
}
/**
* 失败时候的调用
*
* @param data
* @return
*/
public static <T> Result<T> error(CodeMsg codeMsg) {
return new Result<T>(codeMsg);
}
}
public class CodeMsg {
private int code;
private String msg;
//通用异常
public static CodeMsg SUCCESS = new CodeMsg(0,"success");
public static CodeMsg SEVER_ERROR = new CodeMsg(500100,"服务端异常");
//登录模块 5002xx
//登录模块 5003xx
//订单模块 5004xx
//秒杀模块 5005xx
private CodeMsg(int code, String msg) {
this.code = code;
this.msg = msg;
}
public int getCode() {
return code;
}
public String getMsg() {
return msg;
}
}
除了对构造方法私有化外,只提供get方法,可以防止他人通过set方法改变我们的值。
经过这样封装后,我们在DemoController里写结果返回,就会变得特别优雅,没有多余代码
@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");
// return new Result(0,"success","hello imooc");
}
@RequestMapping("/helloError")
@ResponseBody
public Result<String> helloError() {
return Result.error(CodeMsg.SEVER_ERROR);
// return new Result(500100,"session失效");
}
}
2.thymeleaf模版
在pom文件中添加如下依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
在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
编写一个可以从服务端获得值得hello.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>
接下来,可以通过
@RequestMapping("/thymeleaf")
public String thymeleaf(Model model) {
model.addAttribute("name","Joshua");
return "hello";
}
对hello.html进行访问,输出
最后附上工程目录