2_1.项目环境搭建

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进行访问,输出

最后附上工程目录

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值