SpringMVC的Restfull风格接口开发

版权声明:本文为博主原创文章,未经博主允许不得转载

什么是Restfull风格?

简单点说就是访问后台时的请求路径与请求的方式有所不同.
传统的请求路径如:http://127.0.0.1:8080/user/getUser.do?id=1
Restfull风格的请求路径为:http://127.0.0.1:8080/user/1

操作步骤

将web.xml中

在这里插入图片描述

Controller中的写法

//restful风格增删改查

//新增
@RequestMapping(value = "user",method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
@ResponseBody
public String addUser(User user){
    return "新增用户-" + user;
}

//修改
@RequestMapping(value = "user",method = RequestMethod.PUT, produces = "application/json;charset=UTF-8")
@ResponseBody
public String editUser(User user){
    return "修改用户-" + user;
}

//删除
@RequestMapping(value = "user",method = RequestMethod.DELETE, produces = "application/json;charset=UTF-8")
@ResponseBody
public String deleteById(Integer id){
    return "删除用户ID-" + id;
}

//根据id查询
@RequestMapping(value = "user/{id}",method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
@ResponseBody
public String getUserById(@PathVariable("id") Integer id){
    System.out.println(id);
    return "根据主键查询-" + id;
}

//条件查询
@RequestMapping(value = "user",method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
@ResponseBody
public String queryUser(User user){
    return "条件查询-" + user;
}

注意:虽然每个请求的路径都一样,但是请求方式都不同,查询操作一般为GET请求,新增操作一般为POST请求,修改操作一般为PUT请求,删除操作一般为DELETE请求

SpringMVC配置静态资源
https://www.cnblogs.com/banning/p/6195072.html
https://blog.csdn.net/daiyutage/article/details/71250382

猜你喜欢

转载自blog.csdn.net/weixin_44914784/article/details/89313996