Spring Boot 创建RESTful Web Service
2016-11-13 21:08
381 查看
1. 介绍
本篇将使用Spring Boot创建一个简单restful风格web服务,接受HTTP GET请求:
响应体(respond)为一个JSON字符串
2. 环境要求
IED 或 文本编辑器
JDK1.8+
Maven 3.0+
3. 项目工程
1) pom.xml
Spring Boot Maven Plugin 的作用为:
将本项目classpath里面的所有jar包打包成一个可执行的jar文件;
找到包含public static void main() 方法的类,作为可执行jar的入口
提供內建的依赖分析器,为Spring boot dependencies设置默认的版本号(可以被自己指定的版本号覆盖)
2) 创建资源表示类(resource representation,即RESTful中的R,简单理解就是实体)
注:spring使用Jackson JSON库将实体类的对象自动转换成JSON。
3) 资源控制类
控制器代码很简洁,但是却做了很多事情。
@RestController将该类标注为一个spring bean,并标记其中的每个方法返回一个实体对象。相当于@Controller+@ResponsBody。
@RequestMapping提供url和方法的映射,当接收到一个/greeting的请求时,能执行greeting()方法。
注:上面的例子没有指定http动作(GET, PUT, POST, DELETE),所以会与所有的动作匹配。更好的方法是使用@RequestMapping(method=GET),这样只会接收到GET请求。
@RequestParam通过指定的value绑定url中的参数。本例中,url参数是可选的,因为提供了默认值“World”。
方法体创建Greeting()对象并返回。
注:传统的MVC 控制器和RESTful 控制器的区别在于http响应是怎样被创建的。
传统的MVC 控制器会通过视图转换将Greeting对象渲染成html返回到前端。
而RESTful 控制器直接返回Greeting对象,通过JSON转换器变成一个JSON字符串。
4) 执行类
@SpringBootApplication = @Configuration+@EnableAutoConfiguration+@ComponentScan
@Configuration将该类标注为一个spring bean
@EnableAutoConfiguration告诉Spring Boot装载bean的方式可以基于classpath设置、其他bean或属性设置。
正常情况下我们会为Spring MCV应用加上@EnableWebMvc,但是这里Spring Boot已经为我们做好了,它检测到了工程classpath下有spring-mvc jar,就会自动加上这个注解来激活web应用的关键动作,如创建DispatcherServlet。
@ComponentScan 扫描当前包里面所有带@Component、@Service、@Controller等注解的类,加入spring容器注册成bean。
main方法里用Spring Boot的SpringApplication.run()方法启动web应用。整个项目没有一个xml配置文件,包括web.xml,这就是Spring
Boot的强大所在,让用户只关注业务逻辑的java代码,不用去处理项目配置和结构。
5) 构建可执行JAR
命令行进入项目根目录
输入./mvnw spring-boot:run
或./mvnw clean package
会发现项目目录下生成一个target文件夹,里面的jar文件即为可执行JAR。
运行JAR java -jar target/gs-rest-service-0.1.0.jar
6) 测试
浏览器输入 http://localhost:8080/greeting
返回结果
带参数的请求 http://localhost:8080/greeting?name=User
返回结果
原文: http://spring.io/guides/gs/rest-service/,翻译省去了关于Gradle的部分。
本篇将使用Spring Boot创建一个简单restful风格web服务,接受HTTP GET请求:
http://localhost:8080/greeting
响应体(respond)为一个JSON字符串
{"id":1,"content":"Hello, World!"}
2. 环境要求
IED 或 文本编辑器
JDK1.8+
Maven 3.0+
3. 项目工程
1) pom.xml
<?xml version="1.0" encoding="UTF-8"?> <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>org.springframework</groupId> <artifactId>gs-rest-service</artifactId> <version>0.1.0</version> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.4.2.RELEASE</version> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>com.jayway.jsonpath</groupId> <artifactId>json-path</artifactId> <scope>test</scope> </dependency> </dependencies> <properties> <java.version>1.8</java.version> </properties> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> <repositories> <repository> <id>spring-releases</id> <url>https://repo.spring.io/libs-release</url> </repository> </repositories> <pluginRepositories> <pluginRepository> <id>spring-releases</id> <url>https://repo.spring.io/libs-release</url> </pluginRepository> </pluginRepositories> </project>
Spring Boot Maven Plugin 的作用为:
将本项目classpath里面的所有jar包打包成一个可执行的jar文件;
找到包含public static void main() 方法的类,作为可执行jar的入口
提供內建的依赖分析器,为Spring boot dependencies设置默认的版本号(可以被自己指定的版本号覆盖)
2) 创建资源表示类(resource representation,即RESTful中的R,简单理解就是实体)
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; } }
注:spring使用Jackson JSON库将实体类的对象自动转换成JSON。
3) 资源控制类
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)); } }
控制器代码很简洁,但是却做了很多事情。
@RestController将该类标注为一个spring bean,并标记其中的每个方法返回一个实体对象。相当于@Controller+@ResponsBody。
@RequestMapping提供url和方法的映射,当接收到一个/greeting的请求时,能执行greeting()方法。
注:上面的例子没有指定http动作(GET, PUT, POST, DELETE),所以会与所有的动作匹配。更好的方法是使用@RequestMapping(method=GET),这样只会接收到GET请求。
@RequestParam通过指定的value绑定url中的参数。本例中,url参数是可选的,因为提供了默认值“World”。
方法体创建Greeting()对象并返回。
注:传统的MVC 控制器和RESTful 控制器的区别在于http响应是怎样被创建的。
传统的MVC 控制器会通过视图转换将Greeting对象渲染成html返回到前端。
而RESTful 控制器直接返回Greeting对象,通过JSON转换器变成一个JSON字符串。
4) 执行类
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+@EnableAutoConfiguration+@ComponentScan
@Configuration将该类标注为一个spring bean
@EnableAutoConfiguration告诉Spring Boot装载bean的方式可以基于classpath设置、其他bean或属性设置。
正常情况下我们会为Spring MCV应用加上@EnableWebMvc,但是这里Spring Boot已经为我们做好了,它检测到了工程classpath下有spring-mvc jar,就会自动加上这个注解来激活web应用的关键动作,如创建DispatcherServlet。
@ComponentScan 扫描当前包里面所有带@Component、@Service、@Controller等注解的类,加入spring容器注册成bean。
main方法里用Spring Boot的SpringApplication.run()方法启动web应用。整个项目没有一个xml配置文件,包括web.xml,这就是Spring
Boot的强大所在,让用户只关注业务逻辑的java代码,不用去处理项目配置和结构。
5) 构建可执行JAR
命令行进入项目根目录
输入./mvnw spring-boot:run
或./mvnw clean package
会发现项目目录下生成一个target文件夹,里面的jar文件即为可执行JAR。
运行JAR java -jar target/gs-rest-service-0.1.0.jar
6) 测试
浏览器输入 http://localhost:8080/greeting
返回结果
{"id":1,"content":"Hello, World!"}
带参数的请求 http://localhost:8080/greeting?name=User
返回结果
{"id":2,"content":"Hello, User!"}
原文: http://spring.io/guides/gs/rest-service/,翻译省去了关于Gradle的部分。
相关文章推荐
- 使用IDEA创建一个简单的基于Spring Boot的RESTful Web Service
- 创建maven spring boot的RESTful Web Service工程(使用IntelliJ IDEA以及无集成开发环境)
- 使用spring创建一个RESTful Web Service
- 使用spring boot快速构建一个RESTful Web Service
- Spring Boot:建立一个RESTful Web Service
- 使用Spring创建满足RESTful规范的Web Service
- Spring基础:快速入门spring boot(3):创建Restful的Web服务
- 快速搭建RESTful Web Service(Spring Boot)
- spring3创建RESTFul Web Service
- 用spring boot快速创建 Restful Web Service
- SpringBoot-创建RESTful风格的 http接口访问jpa 来操作数据库
- Spring Boot 实践1 --创建WEB restful项目,集成jpa
- SpringCloud微服务系列(2): 创建一个基于Springboot的RESTFul服务
- 用springBoot构建一个RESTful web service
- SpringBoot IntelliJ创建简单的Restful接口
- Spring Boot、Mybatis框架整合开发Java RESTful Web Service
- spring boot demo( 获取一个RESTful web service)
- 使用Apache CXF和Spring集成创建Web Service
- 使用 Spring 3 来创建 RESTful Web Services
- 使用 Spring 3 来创建 RESTful Web Services