spring-guide之rest-service
来源:互联网 发布:先创网络 编辑:程序博客网 时间:2024/05/21 05:40
spring-guide之rest-service
目标:创建一个spring的符合restful规范的web service项目,并做出hello world。
构建一个简单的符合restful规范的web服务
将构建一个接受http get请求的web服务。
http://localhost:8080/greeting
并且返回一个json格式的响应
{"id":1,"content":"Hello, World!"}
同样,可以在url请求上自定义name参数,用来覆盖 World的值。、
{"id":1,"content":"Hello, User!"}
需要准备的
- 大概15分钟
- 一个友好的文本编辑器或者IDE
- jdk1.8 或者更晚的(我用的1.7)
- gradle2.3+ 或者maven3.0+ (我用的gradle)
也可以直接从github上导入代码
官方:git clone https://github.com/spring-guides/gs-rest-service.git
我的:https://github.com/chenlisong/projects/tree/master/gs-rest-service
gradle的一些配置
gradle的快速学习
官网:https://gradle.org/
快速的使用gradle创建一个java项目:http://spring.io/guides/gs/gradle/创建一个目录
mkdir -p src/main/java/hello;└── src └── main └── java └── hello
创建gradle的配置文件build.gradle
buildscript { repositories { mavenCentral() } dependencies { classpath("org.springframework.boot:spring-boot-gradle-plugin:1.4.0.RELEASE") }}apply plugin: 'java'apply plugin: 'eclipse'apply plugin: 'idea'apply plugin: 'spring-boot'jar { baseName = 'gs-rest-service' version = '0.1.0'}repositories { mavenCentral()}//我在这里将jdk改成1.7,原值1.8sourceCompatibility = 1.7targetCompatibility = 1.7dependencies { compile("org.springframework.boot:spring-boot-starter-web") testCompile('org.springframework.boot:spring-boot-starter-test')}
使用spring-boot启动web服务
- 使用方便简单
- 查找main方法来执行
- 解决依赖的jar包
代码实现
目标:通过项目和build系统,来创建一个简单的web服务。
服务将提供/greeting 类型是http get的接口,参数名是name,类型是string,用来替换world。
返回值如下
{ "id": 1, "content": "Hello, World!"}
id是调用次数。
Greeting.java代码如下:
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的jar包,自动把Greeting类解析成为json格式的字符串
创建一个Controller
src/main/java/hello/GreetingController.java
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)); }}
再创建一个启动器,用spring-boot来启动。
src/main/java/hello/Application.java
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); }}
构建成jar包的方式并执行
//启动服务 ./gradlew bootRun -info | debug //build成为jar包,并放在build/libs下 ./gradlew build //cmd下执行jar并启动服务 java -jar build/libs/gs-rest-service-0.1.0.jar
测试一下
request:http://localhost:8080/greeting response:{"id":1,"content":"Hello, World!"} request:http://localhost:8080/greeting?name=User response:{"id":2,"content":"Hello, User!"}
1 0
- spring-guide之rest-service
- spring-guide之consuming-rest
- REST service 之登录注册
- spring-guide之spring-jdbc
- 利用Spring MVC搭建REST Service
- Spring 初探(十一)(Test REST Service)
- Spring 初探(十二)(HATEOAS REST Service)
- Spring Boot Rest Service 下载文件
- spring security for rest web service
- spring-guide之scheduled-task
- spring-guide之uploading-file
- spring REST 之Hello World
- spring integration之http rest
- Spring REST实践之HATEOAS
- SpringBoot之Spring Data REST
- Web Service 实践之 REST vs RPC
- Web Service实践之REST vs RPC
- Web Service实践之REST vs RPC
- 256_判断字符串是否包含字母或数字
- CSS/H5适配手机屏幕
- hdu 5876 - Sparse Graph(2016大连网络赛) bfs
- android拍照保存全尺寸图片
- MyBatis 一次执行多条SQL语句
- spring-guide之rest-service
- 257_属性动画xml使用
- java设计模式之命令模式
- 内存优化的解决方案(最全面的总结!如何合理的使用内存)
- (SOJ) Is it a word ladder? (词梯)
- qt基础总结
- 简单理解设计模式之外观模式
- 如何将tomcat注册成windows系统服务方法
- [NOI 2014] 随机数生成器:模拟,贪心