SpringBoot构建RESTful service完成Get和Post
一个基本的RESTfule service最进场向外提供的请求Method就是Get和Post。
在Get中,常用的都会在请求上带上参数,或者是路径参数。响应Json。
在Post中,常用的会提交form data或者json data作为参数,响应Json。
1. Get请求,url传参,返回json。
先准备一个请求后,响应的对象。
package com.example.demo;
public class Echo {
private final long id;
private final String content;
public Echo(long id, String content) {
this.id = id;
this.content = content;
}
public long getId() {
return this.id;
}
public String getContent() {
return this.content;
}
}
准备一个用来接收请求的EchoController(名字可以根据实际情况写),为它增加@RestController注解,表示这是一个处理RESTful请求的响处理类。
增加@RequestMapping,为本Controller提供一个根url,当然可以不写,写这个是为了更好的将同一种处理的url归在一类,本Controller其他的处理方法对应的url中就不需要重复写。
package com.example.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;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.ModelAttribute; @RestController
@RequestMapping("/echo")
public class EchoController {
private static final String echoTemplate1 = "received %s!";
private static final String echoTemplate2 = "%s speak to %s \'%s\'";
private final AtomicLong counter = new AtomicLong(); @RequestMapping(value="/getter/pattern1", method=RequestMethod.GET)
public Echo getterPattern1(String content) {
return new Echo(counter.incrementAndGet(), String.format(echoTemplate1, content));
} @RequestMapping(value="/getter/pattern2", method=RequestMethod.GET)
public Echo getterPattern2(@RequestParam(value="content", required=false) String alias) {
return new Echo(counter.incrementAndGet(), String.format(echoTemplate1, alias));
}
}
getterPattern1的上面增加了@RequestMapping注解,将指定的url和处理的方法进行映射,对应这个url的请求就由该方法来处理,method可以省略,省略后就是对应所有的Http Metho,gtterPatten1方法的参数默认就和url中的content参数进行映射。
再看getterPattern2,跟上面的方法效果一致,他们的区别就在于参数定义用了@RequestParam注解将url参数和方法参数进行了映射说明,@RequesteParam中value的值就是url中实际的参数,required说明该参数是否必须,如果是true,而实际上url中并没有带上该参数,那么会报异常,为防止异常可以增加defaultValue指定参数的默认值即可。我们会发现这里的方法参数是alias,这里当然是可以和url的参数名不同,只要RequestParam注解映射了他们的关系就没问题。
运行后,可以在浏览器中访问对应的url来看看结果,我这里是用curl来访问。
curl http://localhost:8080/echo/getter/pattern1?content=hello
curl http://localhost:8080/echo/getter/pattern2?content=hello
上面两个url的访问得到的结果除了id会自增外,其他是一致的:
{"id":6,"content":"received hello!"}
2. Get请求,传递url路径参数,返回json。
在EchoController中增加一个响应方法。
@RequestMapping(value="/getter/pattern3/{content}", method=RequestMethod.GET)
public Echo getterPattern3(@PathVariable String content) {
return new Echo(counter.incrementAndGet(), String.format(echoTemplate1, content));
}
可以看到,在@RequestMapping的url定义中的末尾有“{content}”,表明这里是一个路径参数,在getterPattern3的参数content增加了@PathVariable注解,将方法参数与路径参数进行了映射。
运行后,访问url。
curl http://localhost:8080/echo/getter/pattern3/123456
结果:
{"id":8,"content":"received 123456!"}
3.Post请求,参数以Http body的途径提交Json数据。
先定义一个提交的Json对应的对象,这里把它定义为Message。
package com.example.demo;
public class Message {
private String from;
private String to;
private String content;
public Message() {}
public String getFrom() {
return this.from;
}
public String getTo() {
return this.to;
}
public String getContent() {
return this.content;
}
public void setFrom(String value) {
this.from = value;
}
public void setTo(String value) {
this.to = value;
}
public void setContent(String value) {
this.content = value;
}
}
在EchoController增加响应的方法,并完成映射。
@RequestMapping(value="/setter/message1", method=RequestMethod.POST)
public Echo setterMessage1(@RequestBody Message message) {
return new Echo(counter.incrementAndGet(), String.format(echoTemplate2, message.getFrom(), message.getTo(), message.getContent()));
}
在setterMessage1方法的参数中用@RequestBody将请求的Http Body和参数messge进行了映射。
运行后,使用curl向服务端提交json数据。提交的请求头部要带上"Content-Type:application/json",表明请求体Json。
curl -i -H "Content-Type:application/json" -d "{\"from\":\"Tom\",\"to\":\"Sandy\",\"content\":\"hello buddy\"}" http://localhost:8080/echo/setter/message1
结果:
{"id":9,"content":"Tom speak to Sandy 'hello buddy'"}
4.Post请求,参数以Http body的途径提交表单数据。
在EchoController增加响应的方法,并完成映射。
@RequestMapping(value="/setter/message2", method=RequestMethod.POST)
public Echo setterMessage2(@ModelAttribute Message message) {
return new Echo(counter.incrementAndGet(), String.format(echoTemplate2, message.getFrom(), message.getTo(), message.getContent()));
}
在setterMessage2方法的参数中用@ModelAttribute将请求的Http Body中的表单数据和参数messge进行了映射。
运行后,使用curl向服务端提交表单数据。提交的请求头部要带上"Content-Type:application/x-www-form-urlencoded",表明请求体是表单数据,格式是"key1=value1&key2=value2&key3=value3"。
curl -i -H "Content-Type:application/x-www-form-urlencoded" -d "from=sandy&to=aissen&content=go to" http://localhost:8080/echo/setter/message2
结果:
{"id":11,"content":"sandy speak to aissen 'go to'"}
End
SpringBoot构建RESTful service完成Get和Post的更多相关文章
- SpringBoot 构建RestFul API 含单元测试
相关博文: 从消费者角度评估RestFul的意义 SpringBoot 构建RestFul API 含单元测试 首先,回顾并详细说明一下在快速入门中使用的 @Controller . @RestC ...
- 【快学springboot】2.Restful简介,SpringBoot构建Restful接口
Restful简介 Restful一种软件架构风格.设计风格,而不是标准,只是提供了一组设计原则和约束条件.它主要用于客户端和服务器交互类的软件.基于这个风格设计的软件可以更简洁,更有层次,更易于实现 ...
- 使用spring boot+mybatis+mysql 构建RESTful Service
开发目标 开发两个RESTful Service Method Url Description GET /article/findAll POST /article/insert 主要使用到的技术 j ...
- [SpringBoot guides系列翻译]SpringBoot构建RESTful程序入门
原文地址 构建一个RESTful的WebService 这个指南将带你用Spring创建一个RESTful的helloworld程序. 你将完成 在下面地址上创建一个接收http get请求的服务 h ...
- SpringBoot构建RESTful API
1.RESTful介绍 RESTful是一种软件架构风格! RESTful架构风格规定,数据的元操作,即CRUD(create, read, update和delete,即数据的增删查改)操作,分别对 ...
- 使用 SpringBoot 构建一个RESTful API
目录 背景 创建 SpringBoot 项目/模块 SpringBoot pom.xml api pom.xml 创建 RESTful API 应用 @SpringBootApplication @C ...
- Springboot & Mybatis 构建restful 服务五
Springboot & Mybatis 构建restful 服务五 1 前置条件 成功执行完Springboot & Mybatis 构建restful 服务四 2 restful ...
- Springboot & Mybatis 构建restful 服务四
Springboot & Mybatis 构建restful 服务四 1 前置条件 成功执行完Springboot & Mybatis 构建restful 服务三 2 restful ...
- Springboot & Mybatis 构建restful 服务三
Springboot & Mybatis 构建restful 服务三 1 前置条件 成功执行完Springboot & Mybatis 构建restful 服务二 2 restful ...
随机推荐
- 信息安全的核心:CIA三元组 | 安全千字文系列1
我们总是在说信息安全管理,那么信息安全管理到底是在管什么?我们要如何定义信息安全? 这里就要引出信息安全最基本的概念:CIA三元组. 这里的 C,指的是Confidentiality机密性 这里的 I ...
- OpenShitf 使用教程
OpenShitf https://www.openshift.com 1.注册openshitf账号 https://www.openshift.com/app/account/new 2.gett ...
- axis1.4开发webservice服务端(快速入门)-基于jdk1.4
写在前面: 现在有很多开发webservice的方法以及框架,什么cxf等,但是这些在你编写服务类的时候都要用到注解这个功能.如果现在的jdk是1.4的,那么就不能使用注解这个功能了.所以这里可以用到 ...
- 为实施了IFD的Dynamics 365更换自签名的SSL证书以符合Chrome的要求
关注本人微信和易信公众号: 微软动态CRM专家罗勇 ,回复259或者20170704可方便获取本文,同时可以在第一间得到我发布的最新的博文信息,follow me!我的网站是 www.luoyong. ...
- 在linux环境下tomcat 指定 jdk或jre版本
最近在服务器上部署的服务出了点问题,后来查到是因为JDK版本太高了,程序识别不了,需要把JDK降级. 但是服务器上面跑的程序很多,又不能直接把环境变量改了,所以只能想着怎么把这个出问题的工程服务指定j ...
- ReactiveSwift源码解析(八) SignalProducer的代码的基本实现
在前面几篇博客中我们详细的聊了ReactiveSwift中的Bag.Event.Observer以及Signal的使用方式和代码实现.那么在接下来的这几篇博客中,我们就依附于之前博客的基础上来聊一聊S ...
- MVC MVC+EF快速搭建
MVC+EF快速搭建 一.准备: vs2017(个人用的) 二.开始MVC+EF之旅吧: 1.创建mvc项目: Web-ASP.NET Web Application(.NET Framework) ...
- [C++]智能指针的实现与使用
智能指针 智能指针是当我们在使用对象时,有时会把对象的内存分配在堆上忘记释放,导致内存泄露,并且当多个指针共享同一个对象的内存时,容易出现重复释放内存,导致错误. 我们针对所需要共享的对象,手动完成一 ...
- javascript王国的一次旅行,一个没有类的世界怎么玩转面向对象?
1. 前言 作为Java 帝国的未来继承人,Java小王子受到了严格的教育, 不但精通Java语言.Java虚拟机.java类库和框架,还对各种官方的Java规范了如指掌. 近日他听说一个叫做Java ...
- SpringMVC源码情操陶冶-ViewResolver视图解析
简单分析springmvc是如何解析view视图,并返回页面给前端 SpringMVC配置视图解析器 <bean id="viewResolver" class=" ...