1.简单程序
spring-boot的pom.xml
<!--引入父依赖,在下面的依赖可以不写版本号,会自动选择最合适的版本-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.2.5.RELEASE</version>
<relativePath/>
</parent>
<!--指定字符集和jdk版本-->
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
</properties>
<!--引入springboot子依赖-->
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
</dependencies>
<!--引入maven插件-->
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
spring-boot 启动类
@SpringBootApplication
public Class Main{
public static void main(String[] args) {
SpringApplication.run(Main.class,args);
}
}
spring-boot Controller
/**
* @RestController=@Controller+@ResposeBody
* Created by zxdong on 2017/3/30.
*/
@RestController
public class Say {
@RequestMapping("/wwjd")
public String sayWwjd(){
return "万物皆导";
}
@RequestMapping("/getUser")
public List<User> getUser(){
List<User> users=new ArrayList<User>();
for(int i=0;i<10;i++){
users.add(new User(getRandom(4,true),
getRandom(6,false),
getRandom(8,false),
Integer.valueOf(getRandom(3,true)),
new Date()));
}
return users;
}
private String getRandom(int n,boolean flag){
Random random = new Random();
String[] code={"0","1","2","3","4","5","6","7","8","9",
"a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"};
String rs=new String();
for(int i=0;i<n;i++){
if(flag){
rs+=code[random.nextInt(10)];
}else{
rs+=code[random.nextInt(36)];
}
}
return rs;
}
}
2.FastJSON转换消息对象
实现步骤都是一样,如下:
1.需要定义一个convert转换消息对象
2.添加fastJson的配置信息,比如:是否需要格式化返回的json数据
3.在convert中添加配置信息
4.将convert添加到converts中
继承WebMvcConfigurerAdapter类
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
super.configureMessageConverters(converters);
//1.需要定义一个convert转换消息对象
FastJsonHttpMessageConverter fastJsonHttpMessageConverter=new FastJsonHttpMessageConverter();
//2.添加fastJson的配置信息,比如:是否需要格式化返回的json数据
FastJsonConfig fastJsonConfig=new FastJsonConfig();
fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);
//3.在convert中添加配置信息
fastJsonHttpMessageConverter.setFastJsonConfig(fastJsonConfig);
//4.将convert添加到converts中
converters.add(fastJsonHttpMessageConverter);
}
通过@Bean注解注入fastJsonHttpMessageConverter
@Bean
public HttpMessageConverters fastJsonHttpMessageConverters(){
// 1.需要定义一个convert转换消息对象
FastJsonHttpMessageConverter fastJsonHttpMessageConverter=new FastJsonHttpMessageConverter();
// 2.添加fastJson的配置信息,比如:是否需要格式化返回的json数据
FastJsonConfig fastJsonConfig=new FastJsonConfig();
fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);
// 3.在convert中添加配置信息
fastJsonHttpMessageConverter.setFastJsonConfig(fastJsonConfig);
// 4.将convert添加到converts中
HttpMessageConverter<?> converters=fastJsonHttpMessageConverter;
return new HttpMessageConverters(converters);
}
备注,fastJson一些对bean的注解
//不让这个bean的属性序列化返回到前端
@JSONField(serialize = false)
private String password;
//返回指定格式的时间
@JSONField(format="yyyy-MM-dd HH:mm:ss")
private Date date;
2.Spring-Boot 热部署
在构建节点(build)添加springloader plugin
特别提醒:在intellij IDEA 中可能通过CTRL+SHIFT+F9配合使用
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<!--解决热部署引起的中文乱码问题-->
<configuration>
<jvmArguments>-Dfile.encoding=UTF-8</jvmArguments>
</configuration>
<dependencies>
<!--springloaded hot deploy-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>springloaded</artifactId>
<version>1.2.7.RELEASE</version>
</dependency>
</dependencies>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
测试热部署的两种方法
1.利用springboot:run命令进行程序的启动
2. 使用run as -java application
一些处理 :把springloaded-1.2.7.RELEASE.jar的jar下载下来,放到项目的目录lib下,然后将IDEA 的run参数里面的VM参数设置为
-javaagent:.\lib\springloaded-1.2.7.RELEASE.jar -noverify,然后启动程序就行了,当然只要springloaded的路径正确即可,尽量避免中文路径。
阿导未成功实现的方案,使用spring-boot-devtools进行热部署:http://www.cnblogs.com/zhangjianbin/p/6439006.html
需要云服务器的不要错过优惠 [阿里云低价购买云服务,值得一看]