SpringMVC Restful api接口实现的代码
作者:柒小栈主 时间:2023-11-29 12:34:27
【前言】
面向资源的 Restful 风格的 api 接口本着简洁,资源,便于扩展,便于理解等等各项优势,在如今的系统服务中越来越受欢迎。
.net平台有WebAPi项目是专门用来实现Restful api的,其良好的系统封装,简洁优雅的代码实现,深受.net平台开发人员所青睐,在后台服务api接口中,已经逐步取代了辉煌一时MVC Controller,更准确地说,合适的项目使用更加合适的工具,开发效率将会更加高效。
python平台有tornado框架,也是原生支持了Restful api,在使用上有了很大的便利。
Java平台的SpringMVC主键在Web开发中取代了Struts2而占据了更加有力的地位,我们今天着重讲解如何在Java SpringMVC项目中实现Restful api。
【实现思路】
Restful api的实现脱离不了路由,这里我们的Restful api路由由spring mvc 的 controller来实现。
【开发及部署环境】
开发环境:Windows 7 ×64 英文版
Intellij IDEA 2017.2
部署环境:JDK 1.8.0
Tomcat 8.5.5
测试环境:chrome
fiddler
【实现过程】
1、搭建spring mvc maven项目
这里的搭建步骤不再赘述,如有需要参考:https://www.aspxhome.com/article/117670.htm
2、新建控制器 StudentController
为了体现Restful api 我们采用注解,RequestMapping("/api/Student")
具体的代码如下:
package Controllers;
import org.springframework.web.bind.annotation.*;
@RestController
@RequestMapping("/api/Student")
public class StudentController {
@RequestMapping(method = RequestMethod.GET)
public String Get() {
return "{\"id\":\"1\",\"name\":\"1111111111\"}";
}
@RequestMapping(method = RequestMethod.POST)
public String Post() {
return "{\"id\":\"2\",\"name\":\"2222222222\"}";
}
@RequestMapping(method = RequestMethod.PUT)
public String Put() {
return "{\"id\":\"3\",\"name\":\"3333333333\"}";
}
@RequestMapping(method = RequestMethod.DELETE)
public String DELETE() {
return "{\"id\":\"4\",\"name\":\"4444444444\"}";
}
@RequestMapping(value = "/{id}",method = RequestMethod.GET)
public String Get(@PathVariable("id") Integer id) {
return "{\"id\":\""+id+"\",\"name\":\"get path variable id\"}";
}
}
这里有Get,Post,Put,Delete分别对应 查询,添加,修改,删除四种对资源的操作,即通常所说的CRUD。
spring mvc可实现restful的方式有@Controller和@RestController两种方式,两种方式的区别如下:
@Controller的方式实现如果要返回json,xml等文本,需要额外添加@ResponseBody注解,例如:
@ResponseBody //用于返回json数据或者text格式文本
@RequestMapping(value = "/TestJson", method = RequestMethod.GET)
public String TestJson() {
return "{\"id\":\"1001\",\"name\":\"zhangsan\"}";
}
@RestController方式不需要写@ResponseBody,但是不能返回模型绑定数据和jsp视图,只能返回json,xml文本,仅仅是为了更加方便返回json资源而已。
上述的Rest方法中多写了个Get方法:
@RequestMapping(value = "/{id}",method = RequestMethod.GET)
public String Get(@PathVariable("id") Integer id) {
return "{\"id\":\""+id+"\",\"name\":\"get path variable id\"}";
}
该方法可以直接在url拼接一个参数,更加方便对资源的定向访问,例如查一个student list 可以默认空参数,而查询对应的某一个student详情信息,可以id=studentId 定向查询单条,使得我们对资源的访问更加快捷方便。
【系统测试】
运行系统,使用fiddler调用restful api接口:
1.Get方式
2.Post方式
3.Put方式
4.Delete方式
5.Get/id方式
至此,可见我们的spring mvc Restful api接口已经全部通过测试!
来源:http://www.cnblogs.com/qixiaoyizhan/p/7570010.html?utm_source=tuicool&utm_medium=referral
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
如何使用Jenkins构建GIT+Maven项目
![](https://img.aspxhome.com/file/2023/9/61839_0s.png)
JS+Struts2多文件上传实例详解
Android中Listview点击item不变颜色及设置listselector 无效的解决方案
mybatis原理概述入门教程
Android PickerView实现三级联动效果
Java多线程ThreadAPI详细介绍
![](https://img.aspxhome.com/file/2023/8/68208_0s.png)
java 验证用户是否已经登录与实现自动登录方法详解
Flutter 日历组件简单实现
Android自定义EditText实现登录界面
![](https://img.aspxhome.com/file/2023/8/137338_0s.gif)
Spring MVC4.1服务器端推送实现过程解析
![](https://img.aspxhome.com/file/2023/9/69949_0s.jpg)
一文告诉你为什么要重写hashCode()方法和equals()方法
C#编程中使用ref和out关键字来传递数组对象的用法
SpringBoot整合ElasticSearch实践
![](https://img.aspxhome.com/file/2023/1/78281_0s.png)
JVM垃圾收集器详解
![](https://img.aspxhome.com/file/2023/8/78938_0s.png)
Spring Cloud Feign接口返回流的实现
Sharding-Proxy基本功能用法介绍
![](https://img.aspxhome.com/file/2023/7/69877_0s.jpg)
java9中gc log参数迁移
C#+无unsafe的非托管大数组示例详解(large unmanaged array in c# without ‘unsafe’ keyword)
Android中双击返回键退出应用实例代码
C# StreamReader类实现读取文件的方法
![](https://img.aspxhome.com/file/2023/7/102247_0s.gif)