SpringBoot(二)整合Thymeleaf及FastJson

一 整合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:

@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 :

<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,结果如下:


配置完成

        

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值