使用spring boot快速构建一个RESTful Web Service
2017-02-23 19:07
531 查看
IDE:Eclipse
勾选Create a simple project(skip archetype selection)
创建资源控制器
创建应用执行器
@Configuration 定义这个类为bean
@EnableAutoConfiguration 告诉Spring Boot 根据classpath设置、其他beans和各种设置加载bean
@ComponentScan 告诉Spring在
一般来讲,你要加上
访问http://localhost:8080/greeting
访问http://localhost:8080/greeting?name=Jzh
新建maven工程
勾选Create a simple project(skip archetype selection)
配置POM
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.0.RELEASE</version> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>com.jayway.jsonpath</groupId> <artifactId>json-path</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build>
创建资源
package hello; public class Greeting { private final long id; private final String content; public Greeting(long id, String content) { this.id = id; this.content = content; } public long getId() { return id; } public String getContent() { return content; } }
创建资源控制器
package hello; import java.util.concurrent.atomic.AtomicLong; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; @RestController public class GreetingController { private static final String template = "Hello, %s!"; private final AtomicLong counter = new AtomicLong(); @RequestMapping("/greeting") public Greeting greeting(@RequestParam(value="name", defaultValue="World") String name) { return new Greeting(counter.incrementAndGet(), String.format(template, name)); } }
@RequestMapping确保到
/greeing的HTTP请求映射到
greeting()方法。
@RequestMapping默认映射所有的HTTP操作。可以使用
@RequestMapping(method=GET)限定映射
@RequestParam将请求参数
name绑定到
greeting()方法的
name参数。如果该值为空,则显示默认值
World。
@RestController是
@Controller和
@ResponseBody的简写
Greeting必须要转成JSON对象。这不需要你手动去转换。因为
Jackson 2在classpath里,Spring会自动将
Greeting转换为JSON。
创建应用执行器
package hello; 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); } }
@SpringBootApplication有下面几个意思:
@Configuration 定义这个类为bean
@EnableAutoConfiguration 告诉Spring Boot 根据classpath设置、其他beans和各种设置加载bean
@ComponentScan 告诉Spring在
hello包中查找其他组件、配置、服务
一般来讲,你要加上
@EnableWebMvc,但当classpath中有spring-webmvc时,Spring Boot会自动添加
测试服务
像启动普通工程那样启动。访问http://localhost:8080/greeting
访问http://localhost:8080/greeting?name=Jzh
相关文章推荐
- 快速构建一个基于Spring的RESTful Web Service
- 用springBoot构建一个RESTful web service
- Spring Boot - 快速构建一个基本的Restful程序
- 使用IDEA创建一个简单的基于Spring Boot的RESTful Web Service
- 《Springboot极简教程》使用Spring Boot, JPA, Mysql, ThymeLeaf,gradle, Kotlin快速构建一个CRUD Web App
- 使用 Spring Boot 快速构建 Spring 框架应用
- 使用SpringBoot快速构建应用程序
- 使用Spring Boot快速构建应用
- 使用spring创建一个RESTful Web Service
- Spring最新教程(译自Spring官网)——构建一个RESTful Web Service
- 使用 Spring Boot 快速构建 Spring 框架应用
- 使用 Spring Boot 快速构建 Spring 框架应用
- 使用 Spring Boot 快速构建 Spring 框架应用
- 使用 Spring Boot 快速构建 Spring 框架应用
- spring boot demo( 获取一个RESTful web service)
- 使用Spring Boot快速构建应用
- 使用Spring Boot快速构建应用
- 使用Spring Boot快速构建应用
- 使用 Spring Boot 快速构建应用服务
- 使用 Spring Boot 快速构建 Spring 框架应用