使用maven创建项目,工具用的eclipse
application
测试类
贴上pom文件
pom文件
<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.zhinanche</groupId>
<artifactId>manage_spring_boot</artifactId>
<version>0.0.1-SNAPSHOT</version>
<!--从Spring Boot继承默认值 -->
<parent>
<groupId> org.springframework.boot </groupId>
<artifactId> spring-boot-starter-parent </artifactId>
<version> 2.0.0.RELEASE </version>
</parent>
<!-- 为Web应用程序添加典型的依赖关系 -->
<dependencies>
<dependency>
<groupId> org.springframework.boot </groupId>
<artifactId> spring-boot-starter-web </artifactId>
</dependency>
<!--fastjson数据配置start -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.29</version>
</dependency>
<!--fastjson数据配置 end -->
</dependencies>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<!-- Package as a executable jar -->
<build>
<plugins>
<plugin>
<groupId> org.springframework.boot </groupId>
<artifactId> spring-boot-maven-plugin </artifactId>
</plugin>
</plugins>
</build>
</project>
</project>
注:贴上以后下载完jar包update一下maven项目
创建application启动类
application
package cn.springboot.controller;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.http.HttpMessageConverters;
import org.springframework.context.annotation.Bean;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
/**
* spring_boot 启动类
* @author Administrator
*
*/
@SpringBootApplication
public class App{
/** 配置spring_boot返回json数据
* 使用 @Bean 注入 FastJsonHttpMessageConverter
* @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);
//4、
HttpMessageConverter<?> converter = fastConverter;
return new HttpMessageConverters(converter);
}
/**主方法
*
* @param args
*/
public static void main(String[] args) {
SpringApplication.run(App.class,args);
}
}
注:启动类加
@SpringBootApplication注解
测试类
package cn.springboot.controller;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import cn.springboot.pojo.User;
/**
* spring-boot domo controller
* @author Administrator
*
*/
@RestController
public class HelloWordController {
@RequestMapping("hello")
public User hello(){
User user = new User();
user.setId(1);
user.setName("hello");
user.setCreateTime(new Date());
return user;
}
}
App类main方法启动项目
访问 http://localhost:8080/hello
整个项目结构
githup地址 忘记添加忽略了 https://github.com/yinyutaoyyt/spring-boot-demo.git