Article directory
foreword
提示:这里可以添加本文要记录的大概内容:
The restful style is actually a specification, which is used to standardize the naming when we write the interface, and is used to transfer data between the front end and the back end, and between projects. Restful can make our interface more concise, fast, efficient and transparent.
1. What is Restful?
For information interaction between different front-ends and back-ends, Resultful API is a popular API specification. The structure is clear and standard, easy to understand and easy to expand, and it is convenient for front-end developers to distinguish and access interface resources.
The Restfule style is a software architectural style, not a standard, but only provides a design principle and constraints.
2. Use steps
1. What are the types of Resultful?
-
Get Get resources
-
Put to update resources
-
Patch updates some properties
-
Delete delete resource
-
Post create resource
2. Write code
Here I only list the important codes. If you need a full set of codes, you can get them from me on the official account.
UserController class
@RestController
@RequestMapping("/users")
public class UserController {
@Autowired
UserService userService;
//注意:参数记得加注解@PathVariable,对象实体类记得加@ModelAttribute,不一样的
/*查询单条*/
@GetMapping("/{id}")
public User getUserById(@PathVariable Integer id){
System.out.println(id);
return userService.getUserById(id);
}
/*查询全部*/
@GetMapping
public List<User> getUsers(){
return userService.getUsers();
}
/*新增*/
@PostMapping
public Map insert(@ModelAttribute User user){
userService.insert(user);
return Map.of("code","0000");
}
/*删除*/
@DeleteMapping("/{id}")
public Map delete(@PathVariable Integer id){
userService.delete(id);
return Map.of("code","0000");
}
/*修改*/
@PutMapping("/{id}")
public Map update(@ModelAttribute User user,@PathVariable Integer id){
userService.update(user,id);
return Map.of("code","0000");
}
}
Note: Remember to add annotations @PathVariable for parameters, remember to add @ModelAttribute for object entity classes, different
It is worth noting that since the update involves the original id and the modified id, there are 2 parameters, so there is no need to specify the parameterType in xml, just specify the annotation in UserMapper.java!
UserMapper class
@Mapper
public interface UserMapper {
int deleteByPrimaryKey(Integer id);
int insert(User record);
void BatchInsert(List<User> list);
int insertSelective(User record);
/* @Select("select * from t_user where id = #{id}")*/
User selectByPrimaryKey(Integer id);
/*@Select("select * from t_user")*/
List<User> selectAll();
int updateByPrimaryKeySelective(User record);
int updateByPrimaryKey(@Param("record")User record, @Param("id")Integer id);
}
Verify the result using postman
Pro-test is available, note that if you pass an object (required for new modification), you need to add key-value with the form-data attribute in the Body
3. Common status codes
200 200 ok 服务器成功返回用户的请求数据 。
201 create 用户创建或修改数据成功
202 Accept有一个请求进入后台排队
204 No Content 删除数据成功
400 用户发送的请求有错误,服务器没有进行新建或修改操作
401用户没有权限 用户名,密码错误
403 用户得到授权,但是访问被禁止
404 用户发出的请求是不存在的记录,服务器没有进行操作
406用户请求的格式不对
410 用户请求的资源被永久删除,不会被诶获得
500 服务器错误,用户无法进行判断是否请求成功
200 系列是成功的,400系列是客户端,500系列是服务端
Summarize
In general, now most companies have started to use resuful style coding, which is convenient for development, testing and joint debugging. If you want detailed source code or do not understand the place to pay attentionNo public, pay attention to the official account replyrestful style java versionGet all source code
If you have questions about Java (not limited to the questions in this article), welcome to ask your questions on the official account, and I will answer your questions for free as soon as possible~
There is a surprise~