Spring Boot创建一个RESTful Web Service
2017-10-17 17:37
423 查看
简述
这篇文章简述了使用Spring Cloud创建一个简单的”hello world”的Restful Web Service流程。你将构建什么
你将构建一个接收HTTP GET请求的服务:http://localhost:8080/greeting
并且在请求时返回一个JSON格式的字符串:
{"id":1,"content":"Hello, World!"}
你也可以指定
name参数:
http://localhost:8080/greeting?name=User
name参数会覆盖默认的”World”值,并且体现在返回值:
{"id":1,"content":"Hello, User!"}
你需要什么
1、jdk 1.8 及以上2、Gradle 2.3+ or Maven 3.0+
3、你也可以在你的IDE中直接导入代码:
1)、Spring Tool Suite(STS)
2)、IntelliJ IDEA
Maven支持
<?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.7.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>
构建测试工程
Create a resource representation class
package com.zooper.demo; public class Greeting { private long id; private String name; public Greeting(long id, String name) { super(); this.id = id; this.name = name; } public long getId() { return id; } public void setId(long id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
Create a resource controller
在Spring构建RESTful web services中,Http请求被使用为一个controller.这些组件很容易被@RestController注解识别,并且在
GreetingController中使用
GET方式请求
/greeting返
回了一个
Greeting对象:
package com.zooper.demo; 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)); } }
Make the application executable
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标记的class作为一个bean资源在应用环境中定义。
@EnableAutoConfiguration告诉Spring Boot 根据classpath配置、其它beans和各种属性配置去添加beans。
通常你会在一个Spring MVC应用中使用
@EnableWebMvc,但是Spring Boot 在classpath中看到spring-webmvc时会自动使用该注解。
@ComponentScan告诉Spring在当前包中查询其它的部件、配置和服务,并允许它去找到controllers.
在main()方法中调用了SpringApplication.run()方法去开始一个应用。
执行了以上操作后,你就可以访问:
http://localhost:8080/greeting
或者指定name值:
http://localhost:8080/greeting?name=zhangsan
返回的是一个json格式的字符串:
{"id":3,"name":"Hello, zhangsan!"}
相关文章推荐
- 使用IDEA创建一个简单的基于Spring Boot的RESTful Web Service
- 使用spring创建一个RESTful Web Service
- Spring Boot(一) Eclipse创建一个Spring Boot项目,Spring Boot修改端口号
- 使用Java创建RESTful Web Service
- spring3创建RESTFul Web Service
- 运用C#创建一个.Net下的Web Service
- 通过beego快速创建一个Restful风格API项目及API文档自动化
- 使用Restlet创建一个简单的web service(Creating a simple web service with Restlet)
- 2 创建一个 RESTful Web 服务
- 我的WCF4 Rest Service及Entity Framework with POCO之旅(一)——创建一个基本的RESTful Service
- 通过beego快速创建一个Restful风格API项目及API文档自动化(转)
- 运用C#创建一个.Net下的Web Service
- 搭建一个RESTFUL风格的Web Service (Maven版本)
- 通过beego快速创建一个Restful风格API项目及API文档自动化
- 如何创建一个类似 Instagram 的使用 Web Service 作后台的应用
- 用python创建一个简单的restful风格的web服务应用
- Step by Step 创建一个 Web Service
- 使用CXF+spring+restful创建一个web的接口项目
- 使用JAX-RS创建RESTful Web Service