一 整合Thymeleaf
在pom.xml中添加
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
在application.properties中添加
spring.thymeleaf.cache=true
spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.suffix=.html
spring.thymeleaf.mode=HTML5
spring.thymeleaf.encoding=UTF-8
spring.thymeleaf.content-type=text/html
验证是否配置成功:在controller中创建ThymeleafController:
("thymeleaf")
public class ThymeleafController {
("hello")
public String hello(Map<String,Object> map) {
map.put("msg", "Hello Thymeleaf");
return "hello";
}
}
在 template 下创建 hello.html :
<body>
<div class="container">
<h2 th:text="${msg}"></h2>
</div>
</body>
结果如下
二 整合FastJson
在pom.xml中添加依赖:
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.35</version></dependency>
在入口类即Demo1Application.java中添加一个方法:
@Bean
public HttpMessageConverters fastJsonHttpMessageConverters() {
FastJsonHttpMessageConverter converter = new FastJsonHttpMessageConverter();
FastJsonConfig config = new FastJsonConfig();
config.setSerializerFeatures(SerializerFeature.PrettyFormat);
converter.setFastJsonConfig(config);
return new HttpMessageConverters(converter);
}
验证配置是否成功:先创建一个JavaBean User,属性如下:
private Integer id;
private String username;
private String password;
@JSONField(serialize=false)
private Date birthday;
创建一个控制器FastjsonController:
@Controller
@RequestMapping("fastjson")
public class FastJsonController {
@RequestMapping("test")
@ResponseBody
public User test() {
User user = new User();
user.setId(1);
user.setUsername("jack");
user.setPassword("jack123");
user.setBirthday(new Date());
return user;
}
}
打开浏览器访问http://localhost:8080/fastjson/test,结果如下:
配置完成