版权声明:本文为博主原创文章,未经博主允许不得转载
一:认识RESTFull
二:产生背景
近年来移动互联网的发展,前端设备层出不穷(手机、平板、桌面电脑、其他专用设备…),因此,必须有一种统一的机制,方便不同的前端设备与后端进行通信,于是RESTful诞生了,它可以通过一套统一的接口为 Web,iOS和Android提供服务。
三:RESTFull作用
公司在写接口的时候用RESTFull风格,用RESTFull风格提供接口,传参的时候,在浏览器地址栏上
写地址的时候,显得访问地址更加整齐与优雅。
四:Springboot开发RESTFull
1.User.java
package com.mr.model;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
/**
*
*/
@Data
@AllArgsConstructor
@NoArgsConstructor
public class User {
private Integer userId;
private String userName;
private String userPass;
}
2.RESULTFullController.java
import com.mr.model.User;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
*
*/
@RestController
public class RESTFullController {
/**
*
* RESULTFull风格Demo1
*/
@RequestMapping("/boot/user/{id}")
public Object user(@PathVariable("id") Integer id){
User user=new User();
user.setUserId(id);
user.setUserName("ZS");
user.setUserPass("123");
return user;
}
/**
*
* RESULTFull风格Demo2
*/
@RequestMapping("/boot/user/{id}/{name}")
public Object user(@PathVariable("id") Integer id,@PathVariable("name") String name){
User user=new User();
user.setUserId(id);
user.setUserName(name);
user.setUserPass("456");
return user;
}
/**
*
* RESULTFull风格Demo3
*/
@RequestMapping("boot/user/{id}/{name}/{pass}")
public Object user(@PathVariable("id") Integer id,@PathVariable("name") String name,@PathVariable("pass") String pass){
User user=new User();
user.setUserId(id);
user.setUserName(name);
user.setUserPass(pass);
return user;
}
}