用springBoot构建一个RESTful web service
2017-04-21 00:00
651 查看
摘要: 从spring官网学习搭建一个RESTful web service
WebService有两种方式,一是SOAP方式,二是REST方式。SOAP是基于XML的交互,WSDL也是一个XML文档,
可以使用WSDL作为SOAP的描述文件;REST是基于HTTP协议的交互,支持JSON、XML等交互,不需要WSDL。
RESTful就是http的本质,是一个资源URI,和Get,Post,Put,Delete四种操作。相当于一个接口。
1.用idea构建一个项目,然后填充pom.xml文件
pom.xml文件
2.Application.java启动springboot项目
3.model层文件Greeting.java
4.controller层GreetingController.java
5.返回结果
6.整个项目布局
WebService有两种方式,一是SOAP方式,二是REST方式。SOAP是基于XML的交互,WSDL也是一个XML文档,
可以使用WSDL作为SOAP的描述文件;REST是基于HTTP协议的交互,支持JSON、XML等交互,不需要WSDL。
RESTful就是http的本质,是一个资源URI,和Get,Post,Put,Delete四种操作。相当于一个接口。
1.用idea构建一个项目,然后填充pom.xml文件
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>com.spring</groupId> <artifactId>rest</artifactId> <version>1.0-SNAPSHOT</version> <!--spring-boot-starter是SpringBoot的核心启动器,包含了自动配置,日志和yaml--> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.2.RELEASE</version> </parent> <dependencies> <!--支持全栈式web开发,包括tomcat和spring-web mvc--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!--支持常见的测试依赖,包括junit,hamcrest,spring-test模块--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</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> <!--maven仓库--> <repositories> <repository> <id>spring-releases</id> <url>http://repo.spring.io/libs-release</url> </repository> </repositories> <pluginRepositories> <pluginRepository> <id>spring-releases</id> <url>http://repo.spring.io/libs-release</url> </pluginRepository> </pluginRepositories> </project>
2.Application.java启动springboot项目
package com.spring.rest; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; /** * Created by guanguan on 17/4/21. */ @SpringBootApplication public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } }
3.model层文件Greeting.java
package com.spring.rest; /** * Created by guanguan on 17/4/21. */ 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; } }
4.controller层GreetingController.java
import org.springframework.web.bind.annotation.RestController; import java.util.concurrent.atomic.AtomicLong; /** * Created by guanguan on 17/4/21. */ @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)); } }
5.返回结果
6.整个项目布局
相关文章推荐
- 使用spring boot快速构建一个RESTful Web Service
- 构建一个基于 Spring 的 RESTful Web Service
- Spring Boot 构建一个RESTful Web服务
- [译]Spring Boot 构建一个RESTful Web服务
- Spring Boot:建立一个RESTful Web Service
- 快速构建一个基于Spring的RESTful Web Service
- SpringBoot学习系列 | 构建一个较为复杂的RESTful API以及单元测试
- spring boot demo( 获取一个RESTful web service)
- Spring Boot-构建一个复杂的RESTful API及单元测试
- 使用IDEA创建一个简单的基于Spring Boot的RESTful Web Service
- Spring最新教程(译自Spring官网)——构建一个RESTful Web Service
- Spring Boot 构建一个较为复杂的RESTful API以及单元测试
- Spring Boot - 快速构建一个基本的Restful程序
- 学习怎么用Spring写一个RESTful Web Service
- Spring Boot 创建RESTful Web Service
- Jersey2 Spring4 构建RESTful web service
- Spring Boot构建RESTful API与单元测试
- 一个Springboot 热部署的方法(基于gradle构建的项目) 3ff8
- Spring Boot实战:Restful API的构建
- Springboot快速构建restful接口