springboot
项目构建
采用maven多模块化方式搭建项目
1.建立maven项目
2.导入springboot依赖
<!-- SpringBoot项目必须导入的parent依赖 -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<!-- 定义常用的属性 -->
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<spring-cloud.version>Greenwich.SR3</spring-cloud.version>
</properties>
<!-- 声明了spring-boot的版本信息 -->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<scope>import</scope>
<type>pom</type>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<!-- SpringBoot 对Web支持 SpringMVC相关功能,json转换的功能等等 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
3.编写配置文件
application.yml 或 application.properties
server:
port: 9001
spring: #服务命令暂时没有用
application:
name: microservice-user
4.创建启动类
@SpringBootApplication
public class UserAppliction {
public static void main(String[] args) {
SpringApplication.run(UserAppliction.class,args);
}
}
5.创建测试类
@RequestMapping("/user")
@RestController
public class UserController {
/**
* 查询所有用户
*/
@RequestMapping(method = RequestMethod.GET)
public List<User> findAll(){
List<User> list = new ArrayList<>();
list.add(new User(1,"张三",21,199.0));
list.add(new User(2,"李四",23,199.0));
list.add(new User(3,"王五",22,199.0));
return list;
}
}
引入打包插件
<build>
<finalName>XXXX</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>