【Spring官方指南学习】Spring构建一个 restful web service
来源:互联网 发布:日语网络用语2017 编辑:程序博客网 时间:2024/06/12 19:55
【Spring学习笔记,稍作记录,主要参考Spring官网 http://spring.io/guides】
构建一个restful web service
理解如何用spring去构建一个简单的 “hello world” Restful web service.
【即将创建】
创建HTTP GET的request请求:
http://localhost:8080/greeting返回一个JSON格式的响应:
{“id”: 1, “content”: “Hello, World!”}定制响应的内容,在请求地址里面加入一个可选的name参数:
http://localhost:8080/greeting?name=User在返回的响应内容中,name参数将会替代掉默认的”World”:
{“id”:1, “content”: “Hello, User!”}
【环境需要】
- 大约15min
- JDK1.8+
- Maven 3.0+
- IDE:eclipse或者idea, 此处推荐STS(Spring Tool Suite)
【实现步骤】
构建工程
<?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.5.1.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>
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;@RestControllerpublic 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)); }}
在完成这一步之后,其实这个restful service就可以运行了,按照老的方式就是打成war 包,发布 到服务器上就可以了。
但是对月开发来说,发布步骤太繁琐了,下面Spring boot里面提供了一个更便捷的方式,非常适合 开发的时候调试接口使用,个人比较喜欢O(∩_∩)O~
package hello;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplicationpublic class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); }}
@SpringBootApplication是
@Configuration
@EnableAutoConfiguration
@ComponentScan
的集合,功能集大成者~
main()方法中直接使用Spring boot的SpringApplication.run()方法就可以直接运行整个工程 了。
运行工程,三种方法
1. 打成jar,命令行运行 java -jar jar名字
2. 打成war,发布到服务器
3. Spring boot
这里使用Spring boot,main()方法直接运行。
发布之后,可访问如下,
- 【Spring官方指南学习】Spring构建一个 restful web service
- 构建一个基于 Spring 的 RESTful Web Service
- 快速构建一个基于Spring的RESTful Web Service
- 使用spring boot快速构建一个RESTful Web Service
- Spring学习笔记之一:建立一个RESTful Web Service
- Spring最新教程(译自Spring官网)——构建一个RESTful Web Service
- Spring Boot 构建一个RESTful Web服务
- 使用spring创建一个RESTful Web Service
- Spring Boot创建一个RESTful Web Service
- Jersey 2 和 Spring 4 构建 RESTful web service
- 搭建spring RESTful Web Service
- spring-cloud 构建一个restful的web服务
- spring boot demo( 获取一个RESTful web service)
- Spring Boot示例 - 1. 使用Spring Boot Actuator构建RESTful web service
- Spring Web MVC实现Restful Web Service
- Spring Web MVC实现Restful Web Service
- 构建RESTful Web Service
- Jersey+Spring构建RESTful Web服务
- 使用Visual Studio 2010制作安装包
- python安装pywin32clipboard的方法
- 杭电 hdu 2063. 过山车
- IO流之ByteArrayOutputStream
- 数据库之主键、外键、索引
- 【Spring官方指南学习】Spring构建一个 restful web service
- zigbee
- MongoDB学习之NoSQL数据库
- [LeetCode]31. Next Permutation
- Go项目目录管理
- 7款开源Java反编译工具
- 洛谷1198 最大值 线段树
- iOS开发之App主题切换完整解决方案(Swift版)
- 线段树详解