SpringBoot构建Restful service完成Get和Post请求

 一个基本的RESTful service经常向外提供的请求Method就是Get和Post。

在Get中,常用的都会在请求上带上参数或者是路径参数响应Json。

请求带参

路径参数

在Post中,常用的会提交form data或者json data作为参数,响应Json。

1.    Get请求,url传参,返回json。

准备一个请求后,响应的对象。

package com.example.demo;

publicclass Echo{

privatefinallongid;

privatefinalStringcontent;

publicEcho(longid,String content) {

this.id= id;

this.content= content;

}

publiclonggetId(){

returnthis.id;

}

publicStringgetContent() {

returnthis.content;

}

}

准备一个用来接收请求的EchoController(名字可以根据实际情况写),为它增加@RestController注解,表示这是一个处理RESTful请求的响处理类。

增加@RequestMapping,为本Controller提供一个根url,当然可以不写,写这个是为了更好的将同一种处理的url归在一类,本Controller其他的处理方法对应的url中就不需要重复写。

@RestController

@RequestMapping("/echo")

publicclassEchoController{

privatestaticfinalStringechoTemplate1 = "received %s!";

privatestaticfinalStringechoTemplate2 = "%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)

publicEchogetterPattern2(@RequestParam(value="content", required=false) String alias) {

return new Echo(counter.incrementAndGet(),String.format(echoTemplate1, alias));

}

}

getterPattern1的上面增加了@RequestMapping注解,将指定的url和处理的方法进行映射,对应这个url的请求就由该方法来处理,method可以省略,省略后就是对应所有的Http Method,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":"receivedhello!"}

 2. Get请求,传递url路径参数,返回json。

在EchoController中增加一个响应方法。

@RequestMapping(value="/getter/pattern3/{content}",method=RequestMethod.GET)

publicEchogetterPattern3(@PathVariableString 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":"received123456!"}

3.Post请求,参数以Http body的途径提交Json数据。 @RequestBody

先定义一个提交的Json对应的对象,这里把它定义为Message。

packagecom.example.demo;

publicclassMessage{

privateStringfrom;

privateStringto;

privateStringcontent;

publicMessage(){}

publicStringgetFrom() {

returnthis.from;

}

publicStringgetTo() {

returnthis.to;

}

publicStringgetContent() {

returnthis.content;

}

publicvoidsetFrom(Stringvalue) {

this.from =value;

}

publicvoidsetTo(Stringvalue) {

this.to = value;

}

publicvoidsetContent(Stringvalue) {

this.content =value;

}

}

在EchoController增加响应的方法,并完成映射。

@RequestMapping(value="/setter/message1",method=RequestMethod.POST)

publicEchosetterMessage1(@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\":\"hellobuddy\"}" http://localhost:8080/echo/setter/message1

结果:

{"id":9,"content":"Tom speak toSandy 'hello buddy'"}

4.Post请求,参数以Http body的途径提交表单数据(根据属性)。@ModelAttribute

在EchoController增加响应的方法,并完成映射。

@RequestMapping(value="/setter/message2",method=RequestMethod.POST)

public Echo setterMessage2(@ModelAttribute Message message) {

  return newEcho(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=value

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":"sandyspeak to aissen 'go to'"}

框架:SpringBoot构建Restful service完成Get和Post请求相关推荐

  1. springboot构建RESTful 风格应用

    Spring Boot 构建 RESTful 风格应用 1.Web开发的两种模式: 前后端不分离: 以前没有移动互联网时,我们做的大部分应用都是前后端不分的,比如jsp,或者thymeleaf等后端分 ...

  2. 使用 SpringBoot 构建一个RESTful API

    文章目录 背景 创建 SpringBoot 项目/模块 SpringBoot pom.xml api pom.xml 创建 RESTful API 应用 @SpringBootApplication ...

  3. 构建 RESTful Web 服务

    from: https://www.ibm.com/developerworks/cn/education/java/j-rest/j-rest.html 开始之前 关于本教程 REST 是一种思维方 ...

  4. [转]构建基于WCF Restful Service的服务

    本文转自:http://www.cnblogs.com/scy251147/p/3566638.html 前言 传统的Asmx服务,由于遵循SOAP协议,所以返回内容以xml方式组织.并且客户端需要添 ...

  5. 企业级SpringBoot教程(十一)springboot集成swagger2,构建Restful API

    swagger,中文"拽"的意思.它是一个功能强大的api框架,它的集成非常简单,不仅提供了在线文档的查阅,而且还提供了在线文档的测试.另外swagger很容易构建restful风 ...

  6. SpringBoot——JPA的使用、构建restful风格的JPA

    1.JPA概述 JPA:Java持久化规范.JPA(Java Persistence API)是Sun官方提出的Java持久化规范.为Java开发人员提供了一种对象/关联映射工具来管理Java应用中的 ...

  7. 手把手0基础项目实战(一)——教你搭建一套可自动化构建的微服务框架(SpringBoot+Dubbo+Docker+Jenkins)......

    手把手0基础项目实战(一)--教你搭建一套可自动化构建的微服务框架(SpringBoot+Dubbo+Docker+Jenkins)... 原文: 手把手0基础项目实战(一)--教你搭建一套可自动化构 ...

  8. springboot集成swagger2构建RESTful API文档

    在开发过程中,有时候我们需要不停的测试接口,自测,或者交由测试测试接口,我们需要构建一个文档,都是单独写,太麻烦了,现在使用springboot集成swagger2来构建RESTful API文档,可 ...

  9. Guzzle – 构建 RESTful Web 服务的 PHP HTTP 框架

    Guzzle 减轻了发送 HTTP 请求和创建 Web 服务客户端的痛苦.它包含建立一个强大的网络服务客户端的工具,包括:服务描述定义的输入和输出的 API,资源迭代器遍历分页资源,尽可能有效地发送大 ...

最新文章

  1. optee中spinlock的实现原理详解
  2. jqgrid的实用方法集合
  3. 使用gparted live分区工具对VMware及ESXI(vsphere)虚拟机进行根目录扩容(可视化界面操作)
  4. bugku-杂项 convert
  5. cocos2d-x初探学习笔记(14)--菜单项
  6. Moodle: Remove customise this page button from profile?
  7. 智能手机上最没有用的功能是什么?
  8. 2021浙江高考成绩排名查询,2021年浙江高考成绩排名查询,第一批分数线23日公布...
  9. .obj格式的文件怎么显示在html_怎么才能把安卓手机备忘录便签里的文件转到苹果上?...
  10. python中的进制,python中的十进制到十六进制
  11. Kotlin — 适用于数据科学
  12. 操作系统及IIS版本选择参考
  13. matlab方差分析盒型图
  14. 计算机在服装生产中的应用情况,【服装设计论文】服装设计中计算机信息技术的运用(共1730字)...
  15. matlab取值只能为整数,运行时提示Size 输入必须为整数
  16. python3 练习题 day02
  17. 【财富空间】像先知一样思考,如拳手般战斗
  18. MYSQL导入数据出现ERROR 1290 (HY000): The MySQL server is running with the --secure-file-priv option so it
  19. 计算机图形学之机器人
  20. 魔数湖南大学程序设计作业

热门文章

  1. Highcharts在IE中不能一次性正常显示的一种解决办法
  2. 关于 屏幕阅读器 和 sr-only
  3. maven 工程启动找不到 Spring ContextLoaderListener 的解决办法
  4. 差分约束 4416 FFF 团卧底的后宫
  5. 【腾讯第二届校园编程马拉松】HDU-4525,威威猫系列故事——吃鸡腿
  6. matlab二维特殊函数面积图area()函数
  7. 双网卡双ip实现双线路共用
  8. 使用iptables和ipset实现大量屏蔽恶意IP地址
  9. LINUX ulimit命令
  10. 什么数据库比mysql效率高_牛x!一款比传统数据库快 100-1000 倍的数据库,来认识一下?...