一 框架的基本搭建
1 导包--springboot 有一个父包进行版本控制
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.1.RELEASE</version>
</parent>
2 父包导入之后 再导入springboot相关Jar包就无需再关心版本号
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
3 导入包之后 和以前一样 编写 Controller 需要加 RestController注解
4 编写启动类 启动类必须在Controller同包或者父包下 同级包不行
启动类必须加一个注解 @SpringBootApplication
在main方法中 编写 SpringApplication.run即可启动
5 默认端口号为8080 如果想修改为80 那么需要编写配置文件 application.properties (只能叫这个名字)
文件中添加端口号的配置 server.port=80
二 返回对象
1 很简单 只要把Controller的返回值类型更改为pojo的类型 直接返回pojo对象 springboot会自动把该对象解析为Json
2 通常Fastjson 解析会比Springboot自带的Jackon更快 所以我们也可以植入FastJson
首先引入fastJson包
(1) 先创建一个配置类 其实该类的作用和 applicationContext.xml 的作用一样 只是我们不需要再用配置文件了
(2) 配置类必须加一个注解 @Configuration 加了注解后 我们也不用再配置bean节点了 该类中 只要在方法上加@Bean注解 并且返回一个JavaBean 则会被加载到IOC容器
@Bean
public HttpMessageConverters fastJsonMessageConverters(){
FastJsonHttpMessageConverter fastConverter=new FastJsonHttpMessageConverter();
FastJsonConfig config=new FastJsonConfig();
config.setSerializerFeatures(SerializerFeature.PrettyFormat,SerializerFeature.WriteMapNullValue,SerializerFeature.DisableCircularReferenceDetect);
//配置日期类型
config.setDateFormat("yyyy/MM/dd");
//解决中文乱码
List<MediaType> fastMediaTypes = new ArrayList<>();
fastMediaTypes.add(MediaType.APPLICATION_JSON_UTF8);
fastConverter.setSupportedMediaTypes(fastMediaTypes);
fastConverter.setFastJsonConfig(config);
HttpMessageConverter converter=fastConverter;
return new HttpMessageConverters(converter);
}
其实这样的配置 就相当于植入了bean节点 把我们返回值用Fastjson进行解析
三 解析ajax跨域问题
ajax 如果跨域是不允许的,但是前后端分离的时候又不得不跨域,通常的解决方式是用JSONP,但是使用JSONP无端会多一个CallBack参数来
也可以修改response来解决
在springboot中 我们只要再配一个bean就可以解决这个问题了
private CorsConfiguration buildConfig() {
CorsConfiguration corsConfiguration = new CorsConfiguration();
corsConfiguration.addAllowedOrigin("*");
corsConfiguration.addAllowedHeader("*");
corsConfiguration.addAllowedMethod("*");
return corsConfiguration;
}
@Bean
public CorsFilter corsFilter() {
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
source.registerCorsConfiguration("/**", buildConfig());
return new CorsFilter(source);
}
四 springboot热部署
springboot热部署 devtools 方式
植入了两个ClassLoader 一个加载固定包
另一个加载你自己编写的代码 一旦编码有所改变 该ClassLoader会重新加载类 所以该方法会比手动启动更快
(1) 引入依赖包
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
<scope>true</scope>
</dependency>
(2) 引入插件
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<fork>true</fork>
</configuration>
</plugin>
1 导包--springboot 有一个父包进行版本控制
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.1.RELEASE</version>
</parent>
2 父包导入之后 再导入springboot相关Jar包就无需再关心版本号
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
3 导入包之后 和以前一样 编写 Controller 需要加 RestController注解
4 编写启动类 启动类必须在Controller同包或者父包下 同级包不行
启动类必须加一个注解 @SpringBootApplication
在main方法中 编写 SpringApplication.run即可启动
5 默认端口号为8080 如果想修改为80 那么需要编写配置文件 application.properties (只能叫这个名字)
文件中添加端口号的配置 server.port=80
二 返回对象
1 很简单 只要把Controller的返回值类型更改为pojo的类型 直接返回pojo对象 springboot会自动把该对象解析为Json
2 通常Fastjson 解析会比Springboot自带的Jackon更快 所以我们也可以植入FastJson
首先引入fastJson包
(1) 先创建一个配置类 其实该类的作用和 applicationContext.xml 的作用一样 只是我们不需要再用配置文件了
(2) 配置类必须加一个注解 @Configuration 加了注解后 我们也不用再配置bean节点了 该类中 只要在方法上加@Bean注解 并且返回一个JavaBean 则会被加载到IOC容器
@Bean
public HttpMessageConverters fastJsonMessageConverters(){
FastJsonHttpMessageConverter fastConverter=new FastJsonHttpMessageConverter();
FastJsonConfig config=new FastJsonConfig();
config.setSerializerFeatures(SerializerFeature.PrettyFormat,SerializerFeature.WriteMapNullValue,SerializerFeature.DisableCircularReferenceDetect);
//配置日期类型
config.setDateFormat("yyyy/MM/dd");
//解决中文乱码
List<MediaType> fastMediaTypes = new ArrayList<>();
fastMediaTypes.add(MediaType.APPLICATION_JSON_UTF8);
fastConverter.setSupportedMediaTypes(fastMediaTypes);
fastConverter.setFastJsonConfig(config);
HttpMessageConverter converter=fastConverter;
return new HttpMessageConverters(converter);
}
其实这样的配置 就相当于植入了bean节点 把我们返回值用Fastjson进行解析
三 解析ajax跨域问题
ajax 如果跨域是不允许的,但是前后端分离的时候又不得不跨域,通常的解决方式是用JSONP,但是使用JSONP无端会多一个CallBack参数来
也可以修改response来解决
在springboot中 我们只要再配一个bean就可以解决这个问题了
private CorsConfiguration buildConfig() {
CorsConfiguration corsConfiguration = new CorsConfiguration();
corsConfiguration.addAllowedOrigin("*");
corsConfiguration.addAllowedHeader("*");
corsConfiguration.addAllowedMethod("*");
return corsConfiguration;
}
@Bean
public CorsFilter corsFilter() {
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
source.registerCorsConfiguration("/**", buildConfig());
return new CorsFilter(source);
}
四 springboot热部署
springboot热部署 devtools 方式
植入了两个ClassLoader 一个加载固定包
另一个加载你自己编写的代码 一旦编码有所改变 该ClassLoader会重新加载类 所以该方法会比手动启动更快
(1) 引入依赖包
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
<scope>true</scope>
</dependency>
(2) 引入插件
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<fork>true</fork>
</configuration>
</plugin>