使用 spring boot 快速搭建 基于 Restful 风格的微服务,
无spring 配置文件,纯java 工程,可以快速发布,调试项目
1.创建一个maven 工程
2. 导入如下配置
<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.iraid.test</groupId> <artifactId>spring-boot</artifactId> <version>0.0.1-SNAPSHOT</version> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.3.5.RELEASE</version> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies> </project>
3. 创建 RestController
@RestController @EnableAutoConfiguration public class GreetController { @Autowired private UserService userService; @RequestMapping("/greet") User greet() { return userService.getUser(1L); } }
4. 创建启动类 ,这个可以省略,直接把mian 方法放到其他 controller 类里面运行即可
package com.iraid.test.boot; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } }
5. 打包
mvn install
6. 运行
java -jar spring-boot-0.0.1-SNAPSHOT.jar
7. 测试