App.java
package com.kfit; import java.util.List; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.web.HttpMessageConverters; import org.springframework.context.annotation.Bean; import org.springframework.http.converter.HttpMessageConverter; import com.alibaba.fastjson.serializer.SerializerFeature; import com.alibaba.fastjson.support.config.FastJsonConfig; import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter; /** * 在这里我们使用@SpringBootApplication指定这是一个 spring boot的应用程序.入口文件。 * @date 2016年12月10日 */ //extends WebMvcConfigurerAdapter @SpringBootApplication public class App{ /* 配置fastjon(支持两种方法) 第一种方法就是: (1)启动类继承extends WebMvcConfigurerAdapter (2)覆盖方法configureMessageConverters 第二种方法 (1)在App.java启动类中, 注入Bean : HttpMessageConverters */ /* public void configureMessageConverters(List<HttpMessageConverter<?>> converters) { // super.configureMessageConverters(converters); * 1、需要先定义一个 convert 转换消息的对象; * 2、添加fastJson 的配置信息,比如:是否要格式化返回的json数据; * 3、在convert中添加配置信息. * 4、将convert添加到converters当中. * // 1、需要先定义一个 convert 转换消息的对象; FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter(); //2、添加fastJson 的配置信息,比如:是否要格式化返回的json数据; FastJsonConfig fastJsonConfig = new FastJsonConfig(); fastJsonConfig.setSerializerFeatures( SerializerFeature.PrettyFormat ); //3、在convert中添加配置信息. fastConverter.setFastJsonConfig(fastJsonConfig); //4、将convert添加到converters当中. converters.add(fastConverter); }*/ /** * 在这里我们使用 @Bean注入 fastJsonHttpMessageConvert * @return */ @Bean public HttpMessageConverters fastJsonHttpMessageConverters() { // 1、需要先定义一个 convert 转换消息的对象; FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter(); //2、添加fastJson 的配置信息,比如:是否要格式化返回的json数据; FastJsonConfig fastJsonConfig = new FastJsonConfig(); fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat); //3、在convert中添加配置信息. fastConverter.setFastJsonConfig(fastJsonConfig); HttpMessageConverter<?> converter = fastConverter; return new HttpMessageConverters(converter); } /** * 这是springloader的配置方式:-javaagent:.libspringloaded-1.2.4.RELEASE.jar -noverify * @param args */ public static void main(String[] args) { /* * 在main方法进行启动我们的应用程序. */ SpringApplication.run(App.class, args); } }
Demo.java
package com.kfit; import java.util.Date; import com.alibaba.fastjson.annotation.JSONField; /** * 这是一个测试实体类. * @date 2016年12月10日 */ public class Demo { private int id; private String name; //com.alibaba.fastjson.annotation.JSONField @JSONField(format="yyyy-MM-dd HH:mm:ss",serialize=true) //返回给浏览器的时候,json的时候以指定的格式返回。 private Date createTime;//创建时间. /* * 我们不想返回remarks? * serialize:是否需要序列化属性. 为false时候就不会返回给浏览器 */ @JSONField(serialize=false) private String remarks;//备注信息. public String getRemarks() { return remarks; } public void setRemarks(String remarks) { this.remarks = remarks; } public Date getCreateTime() { return createTime; } public void setCreateTime(Date createTime) { this.createTime = createTime; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
HelloController.java
package com.kfit; import java.util.Date; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; /** * 在这里我们使用RestController (等待于 @Controller 和 @RequestBody) * @date 2016年12月10日 */ @RestController public class HelloController { /** * 在这里我们使用@RequestMapping 建立请求映射: * http://127.0.0.1:8080/hello */ @RequestMapping("/hello") public String hello(){ return "hello-2016-12-11.v.0"; } @RequestMapping("/hello2") public String hello2(){ return "hello2-2016"; } @RequestMapping("/hello3") public String hello3(){ return "hello3"; } /** * Spring Boot默认使用的json解析框架是jackson,默认就支持返回json */ @RequestMapping("/getDemo")//请求的映射 public Demo getDemo(){ Demo demo = new Demo(); demo.setId(1); demo.setName("张三"); demo.setCreateTime(new Date()); demo.setRemarks("这是备注信息aaaaa"); return demo; } }
总结.txt
我们要使用第三方的json解析框架的话: 1、我们需要在pom.xml中引入相应的依赖; 2、需要在App.java中继承WebMvcConfigurerAdapter重写方法:configureMessageConverters 添加我们自己定义的json解析框架; 2.1 @Bean注入第三方的json解析框架: @Bean public HttpMessageConverters fastJsonHttpMessageConverters() { // 1、需要先定义一个 convert 转换消息的对象; FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter(); //2、添加fastJson 的配置信息,比如:是否要格式化返回的json数据; FastJsonConfig fastJsonConfig = new FastJsonConfig(); fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat); //3、在convert中添加配置信息. fastConverter.setFastJsonConfig(fastJsonConfig); HttpMessageConverter<?> converter = fastConverter; return new HttpMessageConverters(converter); } 1、第1节 Spring Boot之Hello World,mavenproject, 1、新建一个Controller类HelloController;路由作用, 2、编写Spring Boot 启动类;@SpringBootApplication,新建启动类(App – Main方法) 3、进行访问测试,默认的端口号是8080;
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.kfit</groupId> <artifactId>spring-boot-hello</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>spring-boot-hello</name> <url>http://maven.apache.org</url> <!-- spring boot 父节点依赖,引入这个之后相关的引入就不需要添加version配置, spring boot会自动选择最合适的版本进行添加。 --> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.4.1.RELEASE</version> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <!-- 指定一下jdk的版本 ,这里我们使用jdk 1.8 ,默认是1.6 --> <java.version>1.8</java.version> </properties> <dependencies> <!-- spring-boot-starter-web: MVC,AOP的依赖包.... --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <!-- <version></version> 由于我们在上面指定了 parent(spring boot)这里就不指定版本号了。 --> </dependency> <!-- 个人使用比较习惯的json框架是fastjson,所以spring boot默认的jackson使用起来比较不习惯, 所以很自然我就想我能不能使用fastjson进行json解析呢? 这里要说下很重要的话,官方文档说的1.2.10以后, 会有两个方法支持HttpMessageconvert,一个是FastJsonHttpMessageConverter, 支持4.2以下的版本,一个是FastJsonHttpMessageConverter4支持4.2以上的版本, 具体有什么区别暂时没有深入研究。这里也就是说:低版本的就不支持了, 所以这里最低要求就是1.2.10+。 --> <!-- 添加fastjson 依赖包. --> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.15</version> </dependency> <!-- spring boot devtools 依赖包. --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <optional>true</optional> <scope>true</scope> </dependency> </dependencies> <!-- 编译节点。构建节点. --> <build> <plugins> <!-- 在这里添加springloader plugin:那么如何解决热部署的问题呢?那就是springloaded. 如果使用的run as – java application的话,那么还需要做一些处理。 把spring-loader-1.2.4.RELEASE.jar下载下来,放到项目的lib目录中, 然后把IDEA的run参数里VM参数设置为(run as >>> run configuration >>> Arguments >>> VM arguments): -javaagent:.libspringloaded-1.2.4.RELEASE.jar -noverify 然后启动就可以了,这样在run as的时候,也能进行热部署 <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin </artifactId> <dependencies> //springloaded hot deploy <dependency> <groupId>org.springframework</groupId> <artifactId>springloaded</artifactId> <version>1.2.4.RELEASE</version> </dependency> </dependencies> <executions> <execution> <goals> <goal>repackage</goal> </goals> <configuration> <classifier>exec</classifier> </configuration> </execution> </executions> </plugin> --> <!-- 这是spring boot devtool plugin springboot + devtools(热部署):通过使用springloaded进行热部署, 但是些代码修改了,并不会进行热部署。 spring-boot-devtools 是一个为开发者服务的一个模块,其中最重要的功能就是自动应 用代码更改到最新的App上面去。原理是在发现代码有更改之后,重新启动应用,但是速度 比手动停止后再启动还要更快,更快指的不是节省出来的手工操作的时间。 其深层原理是使用了两个ClassLoader,一个Classloader加载那些不会改变的类(第三方Jar包), 另一个ClassLoader加载会更改的类,称为 restart ClassLoader,这样在有代码更改的时候, 原来的restart ClassLoader 被丢弃,重新创建一个restart ClassLoader,由于需要加载的类相比较少, 所以实现了较快的重启时间(5秒以内)。 添加依赖包: <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <optional>true</optional> <scope>true</scope> </dependency> 使用方法: 添加spring-boot-maven-plugin: <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <fork>true</fork> </configuration> </plugin> </plugins> </build> 修改类>>>保存:应用会重启 修改配置文件>>>保存:应用会重启 修改页面>>>保存:应用会重启,页面会刷新(原理是将spring.thymeleaf.cache设为false) 1、当我们修改了方法的返回值,是能够进行热部署的; 2、当我们重新创建了一个方法,是能够进行热部署的; 3、当我们重新创建了一个Class,是能够进行热部署的; 1. devtools会监听classpath下的文件变动,并且会立即重启应用(发生在保存时机), 注意:因为其采用的虚拟机机制,该项重启是很快的。 2. devtools可以实现页面热部署(即页面修改后会立即生效,这个可以直接在 application.properties文件中配置spring.thymeleaf.cache=false来实现 (这里注意不同的模板配置不一样)。 不能使用分析: 对应的spring-boot版本是否正确,这里使用的是1.4.1版本; 是否加入plugin以及属性<fork>true</fork> Eclipse Project 是否开启了Build Automatically (我自己就在这里栽了坑,不知道为什么我的工具什么时候关闭了自动编译的功能)。 如果设置SpringApplication.setRegisterShutdownHook(false),则自动重启将不起作用。 --> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <!--fork : 如果没有该项配置,肯呢个devtools不会起作用,即应用不会restart --> <fork>true</fork> </configuration> </plugin> </plugins> </build> </project>