首页
学习
活动
专区
圈层
工具
发布
社区首页 >专栏 >SpringBoot-Web-Fastjson

SpringBoot-Web-Fastjson

作者头像
崔笑颜
发布2020-06-08 16:14:16
发布2020-06-08 16:14:16
5210
举报

整合模板引擎

由于 jsp 不被 SpringBoot 推荐使用,所以模板引擎主要介绍 Freemarker 和 Thymeleaf。

Freemarker

代码语言:javascript
复制
添加 Freemarker 依赖
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>

添加 Freemarker 模板配置
在 application.properties 中添加如下内容:
spring.freemarker.allow-request-override=false
spring.freemarker.cache=true
spring.freemarker.check-template-location=true
spring.freemarker.charset=UTF-8
spring.freemarker.content-type=text/html
spring.freemarker.expose-request-attributes=false
spring.freemarker.expose-session-attributes=false
spring.freemarker.expose-spring-macro-helpers=false
spring.freemarker.prefix=
spring.freemarker.suffix=.ftl

Freemarker 案例演示
在 controller 包中创建 FreemarkerController:
@Controller
@RequestMapping("freemarker")
public class FreemarkerController {

	@RequestMapping("hello")
	public String hello(Map<String,Object> map) {
		
		map.put("msg", "Hello Freemarker");
		return "hello";
	}
}
在 templates 目录中创建名为 hello.ftl 文件,内容如下:
<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <link href="/css/index.css" rel="stylesheet"/>
</head>
<body>
    <div class="container">
    	<h2>${msg}</h2>
    </div>
</body>
</html>

结果如下:

整合 Thymeleaf

代码语言:javascript
复制
添加 Thymeleaf 依赖
在 pom.xml 文件中添加:
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>

添加 Thymeleaf 模板配置
在 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
上述配置都是默认值。

Thymeleaf 案例演示
在 controller 包中创建 ThymeleafController:
@Controller
@RequestMapping("thymeleaf")
public class ThymeleafController {

	@RequestMapping("hello")
	public String hello(Map<String,Object> map) {
		map.put("msg", "Hello Thymeleaf");
		return "hello";
	}
}

在 template 目录下创建名为 hello.html 的文件,内容如下:
<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <link href="/css/index.css" rel="stylesheet"/>
</head>
<body>
    <div class="container">
    	<h2 th:text="${msg}"></h2>
    </div>
</body>
</html>

结果如下:

整合 Fastjson

代码语言:javascript
复制
添加依赖
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>fastjson</artifactId>
    <version>1.2.35</version>
</dependency>

整合 Fastjson
创建一个配置管理类 WebConfig ,如下:
@Configuration
public class WebConfig {

	@Bean
	public HttpMessageConverters fastJsonHttpMessageConverters() {
		FastJsonHttpMessageConverter fastJsonHttpMessageConverter = new FastJsonHttpMessageConverter();

		FastJsonConfig fastJsonConfig = new FastJsonConfig();
		fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);
		
		fastJsonHttpMessageConverter.setFastJsonConfig(fastJsonConfig);
		
		HttpMessageConverter<?> converter = fastJsonHttpMessageConverter;
		
		return new HttpMessageConverters(converter);

	}
}

演示案例:
创建一个实体类 User: 记得get set
public class User {

	private Integer id;
	
	private String username;
	
	private String password;
	
	@JSONField(format="yyyy-MM-dd")
	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;
	}
}

结果如下图:

日期格式与我们修改的内容格式一致,说明 Fastjson 整合成功。

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2019-09-04,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 整合模板引擎
    • Freemarker
      • 结果如下:
    • 整合 Thymeleaf
      • 结果如下:
    • 整合 Fastjson
      • 结果如下图:
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档