SpringBoot
springboot JSON
springboot 默认json解析器使用的是jackson,如果jackson不满足使用,可以整合其它的json解析器
springboot 整合fastjson
第一种方法:
1.引入 fastjson 依赖
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.15</version>
</dependency>
2.启动类继承 WebMvcConfigurerAdapter
覆盖方法 configureMessageConverters
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
super.configureMessageConverters(converters);
FastJsonHttpMessageConverter fastConverter = newFastJsonHttpMessageConverter();
FastJsonConfig fastJsonConfig = new FastJsonConfig();
fastJsonConfig.setSerializerFeatures(
SerializerFeature.PrettyFormat
);
fastConverter.setFastJsonConfig(fastJsonConfig);
converters.add(fastConverter);
}
第二种方法:
1.在App.java启动类中,注入Bean : HttpMessageConverters
@Bean
public HttpMessageConverters fastJsonHttpMessageConverters() {
FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();
FastJsonConfig fastJsonConfig = new FastJsonConfig();
fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);
fastConverter.setFastJsonConfig(fastJsonConfig);
HttpMessageConverter<?> converter = fastConverter;
return new HttpMessageConverters(converter);
}
HTML
在springboot中,默认静态资源位置是在resources下的static文件中,我们在resources文件下创建两个文件夹 templates 和 static
static下面用来存放 JS 等静态资源 templates下面用来存放 html等静态界面
在springboot中使用html的一种方式 ,引入html相关依赖
1.依赖
<dependency> //需要关注标签闭合或者字符转义
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency> //避免Thymeleaf对html语法检查过严 不用再去关注
<groupId>net.sourceforge.nekohtml</groupId>
<artifactId>nekohtml</artifactId>
<version>1.9.22</version>
</dependency>
2.配置文件 application.properties
//thymelea模板配置
spring.thymeleaf.prefix=classpath:/templates/user/
spring.thymeleaf.suffix=.html
//避免Thymeleaf对html语法检查过严 不用再去关注
spring.thymeleaf.mode=LEGACYHTML5
spring.thymeleaf.encoding=UTF-8
//热部署文件,页面不产生缓存,及时更新
spring.thymeleaf.cache=false
spring.resources.chain.strategy.content.enabled=true
spring.resources.chain.strategy.content.paths=/**
// 静态文件请求匹配方式
spring.mvc.static-path-pattern=/**
// 修改默认的静态寻址资源目录
spring.resources.static-locations=classpath:/templates/,classpath:/META-INF/resources/,\
classpath:/resources/,classpath:/static/,classpath:/public/
3.引入资源路径要使用 th:
<script th:src="@{/js/user.js}"></script>