【Restful】三分钟彻底了解Restful最佳实践&【Restful接口】restful接口的两种使用方式
REST是英文representational state transfer(表象性状态转变)或者表述性状态转移;Rest是web服务的一种架构风格;使用HTTP,URI,XML,JSON,HTML等广泛流行的标准和协议;轻量级,跨平台,跨语言的架构设计;它是一种设计风格,不是一种标准,是一种思想
Rest架构的主要原则
网络上的所有事物都被抽象为资源
每个资源都有一个唯一的资源标识符
同一个资源具有多种表现形式(xml,json等)
对资源的各种操作不会改变资源标识符
所有的操作都是无状态的
符合REST原则的架构方式即可称为RESTful
什么是Restful:
对应的中文是rest式的;Restful web service是一种常见的rest的应用,是遵守了rest风格的web服务;rest式的web服务是一种ROA(The Resource-Oriented Architecture)(面向资源的架构).
为什么会出现Restful
在Restful之前的操作:
http://127.0.0.1/user/query/1 GET 根据用户id查询用户数据
http://127.0.0.1/user/save POST 新增用户
http://127.0.0.1/user/update POST 修改用户信息
http://127.0.0.1/user/delete GET/POST 删除用户信息
RESTful用法:
http://127.0.0.1/user/1 GET 根据用户id查询用户数据
http://127.0.0.1/user POST 新增用户
http://127.0.0.1/user PUT 修改用户信息
http://127.0.0.1/user DELETE 删除用户信息
之前的操作是没有问题的,大神认为是有问题的,有什么问题呢?你每次请求的接口或者地址,都在做描述,例如查询的时候用了query,新增的时候用了save,其实完全没有这个必要,我使用了get请求,就是查询.使用post请求,就是新增的请求,我的意图很明显,完全没有必要做描述,这就是为什么有了restful.
如何使用:
SpringMVC实现restful服务:
SpringMVC原生态的支持了REST风格的架构设计
所涉及到的注解:
--@RequestMapping
---@PathVariable
---@ResponseBody
package cn.itcast.mybatis.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import cn.itcast.mybatis.pojo.User;
import cn.itcast.mybatis.service.NewUserService;
@RequestMapping("restful/user")
@Controller
public class RestUserController {
@Autowired
private NewUserService newUserService;
/**
* 根据用户id查询用户数据
*
* @param id
* @return
*/
@RequestMapping(value = "{id}", method = RequestMethod.GET)
@ResponseBody
public ResponseEntity<User> queryUserById(@PathVariable("id") Long id) {
try {
User user = this.newUserService.queryUserById(id);
if (null == user) {
// 资源不存在,响应404
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(null);
}
// 200
// return ResponseEntity.status(HttpStatus.OK).body(user);
return ResponseEntity.ok(user);
} catch (Exception e) {
e.printStackTrace();
}
// 500
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
}
/**
* 新增用户
*
* @param user
* @return
*/
@RequestMapping(method = RequestMethod.POST)
public ResponseEntity<Void> saveUser(User user) {
try {
this.newUserService.saveUser(user);
return ResponseEntity.status(HttpStatus.CREATED).build();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// 500
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
}
/**
* 更新用户资源
*
* @param user
* @return
*/
@RequestMapping(method = RequestMethod.PUT)
public ResponseEntity<Void> updateUser(User user) {
try {
this.newUserService.updateUser(user);
return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
} catch (Exception e) {
e.printStackTrace();
}
// 500
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
}
/**
* 删除用户资源
*
* @param user
* @return
*/
@RequestMapping(method = RequestMethod.DELETE)
public ResponseEntity<Void> deleteUser(@RequestParam(value = "id", defaultValue = "0") Long id) {
try {
if (id.intValue() == 0) {
// 请求参数有误
return ResponseEntity.status(HttpStatus.BAD_REQUEST).build();
}
this.newUserService.deleteUserById(id);
// 204
return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
} catch (Exception e) {
e.printStackTrace();
}
// 500
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
}
}
HTTP相应状态码:
总结:
restful就是旧技术,新风格.之前写过一篇关于restful接口的博客:【Restful接口】restful接口的两种使用方式
【Restful接口】restful接口的两种使用方式
小编最近的项目是好几个团队的一块合作,由于项目大,功能多,各个团队负责的东西不同,我的团队除了自己的开发前端和后端外,还负责给别的团队提供后端支持,在这里就用上了restful接口.
小编刚开始都是本本分分的做着自己的dubbo接口,后来被分了几个restful接口,之前没有写过restful接口,别人给讲了讲以后开始写dobbo接口的旅程.
为什么用restful接口?
怎么用呢?
restful接口常用的两种方式是get和post.下面简单介绍一下这两种方式的使用.
由于调用restful接口是通过url的方式来访问后端的代码.新建CustRegisterApi类以后,除了基本的注入外,还需要配置url的地址.以后的demo就在这个类里面写了.
<strong><span style="font-size:18px;">@RestController
@RequestMapping(value = "/customer/register", produces = { MediaType.APPLICATION_JSON_UTF8_VALUE })
@CrossOrigin(origins = "*")
public class CustRegisterApi {
@Autowired
private HttpServletRequest request;
@Autowired
private HttpServletResponse response;
}</span></strong>
1:get方式,url地址会在地址栏显示出参数.
<strong><span style="font-size:18px;">/**
* 检查邮箱是否已经绑定
* @param email 邮箱
* @return
*/
@RequestMapping(value = "/checkEmail", method = { RequestMethod.GET })
@ApiOperation(value = "检查邮箱是否已经绑定")
public RestResponse<Boolean> checkEmail(@RequestParam(value = "email") String email) {
RestResponse<Boolean> restResponse = null;
try {
boolean checkIsMailBinding = custService.checkIsMailBinding(email);
// restResponse = new RestResponse<Boolean>(RestRespCode.OK, MessageUtil.getMessage(RestRespCode.OK),
// checkIsMailBinding);
if (checkIsMailBinding == false) {
restResponse = new RestResponse<Boolean>(RestRespCode.REGISTER_USERNAME_EXISTED,
MessageUtil.getMessage(RestRespCode.REGISTER_USERNAME_EXISTED), null);
} else {
restResponse = new RestResponse<Boolean>(RestRespCode.OK, MessageUtil.getMessage(RestRespCode.OK), null);
}
} catch (Exception e) {
e.printStackTrace();
restResponse = new RestResponse<Boolean>(RestRespCode.INTERNAL_ERROR,
MessageUtil.getMessage(RestRespCode.INTERNAL_ERROR), null);
}
return restResponse;
}</span></strong>
访问方式:http://localhost:8080(端口号)/模块名称/register/checkEmail?email=****
2:post方式,url地址会在地址栏不会显示出参数.
<strong><span style="font-size:18px;">/**
* 修改密码
* @param memberId 用户编号
* @param oldPassword 旧密码
* @param newPassword 新密码
* @return
* @throws Exception
*/
@RequestMapping(value = "/modifyPassword", method = RequestMethod.POST, consumes = "application/json")
@ApiOperation(value = "修改支付密码")
public RestResponse<Boolean> changePassword(@RequestBody CaptchaVO captchaVO) throws Exception {
// 验证旧密码是否正确
Boolean findPassword = registerService.findPassword(captchaVO.getMemberId(), captchaVO.getOldPassword());
if (findPassword == false) {
return new RestResponse<Boolean>(RestRespCode.PASSWORD_WRONG,
MessageUtil.getMessage(RestRespCode.PASSWORD_WRONG), null);
}
return new RestResponse<Boolean>(RestRespCode.OK, MessageUtil.getMessage(RestRespCode.OK), null);
}</span></strong>
post方式
是通过application/json;charset=utf-8来访问一级custom的方式来访问,一般是用于修改密码或者是不让别人看到参数的情况下用的post方式.

浙公网安备 33010602011771号