将琴存诗
人生 可以不要那么 耀 ,只需要有 一个  平凡的梦想  足以 。—— loveincode -_^ RSS
Fork me on GitHub

SpringMVC 构建Restful风格 及问题处理

基本的请求URL:

/person/{id}  GET     得到id的person

/person   POST        新增person

/person/{id}  PUT      更新id的person

/person/{id}  DELETE    删除id的person

源码地址:https://github.com/loveincode/ssm

1. 查询 GET  

@RequestMapping(value = "/{id}", method = RequestMethod.GET)

复制代码
 1 @RequestMapping(value = "/{id}", method = RequestMethod.GET)
 2     public @ResponseBody String show(@PathVariable Integer id, HttpServletRequest request,
 3             HttpServletResponse response) {
 4         log.info("ENTER " + ToolsUtil.getMethodName());
 5         ResultVO resultVO = new ResultVO();
 6         Person person = new Person();
 7         person = personService.findById(id);
 8         if (person != null) {
 9             resultVO.setSuccess(true);
10             resultVO.setData(person);
11             resultVO.setMessage("查询成功");
12         } else {
13             resultVO.setMessage("查询失败,没找到id=" + id + "的Person");
14         }
15         return resultVO.toString();
16     }
复制代码

测试:

2. 新增 POST

@RequestMapping(method = RequestMethod.POST)

复制代码
 1 @RequestMapping(method = RequestMethod.POST)
 2     public @ResponseBody String add(@ModelAttribute("person") Person person, HttpServletRequest request,
 3             HttpServletResponse response) {
 4         ResultVO resultVO = new ResultVO();
 5         System.out.println(person.toString());
 6         if (person.getName() != null) {
 7             personService.add(person);
 8             resultVO.setSuccess(true);
 9             resultVO.setMessage("插入成功");
10         } else {
11             resultVO.setMessage("name为空,插入失败");
12         }
13         return resultVO.toString();
14     }
复制代码

 测试:

3. 更新 PUT

@RequestMapping(value = "/{id}", method = RequestMethod.PUT)

复制代码
@RequestMapping(value = "/{id}", method = RequestMethod.PUT)
    @ResponseBody
    public String update(@PathVariable Integer id, @ModelAttribute("person") Person person, HttpServletRequest request,
            HttpServletResponse response) {
        ResultVO resultVO = new ResultVO();
        Person oldperson = personService.findById(id);
        if (oldperson != null) {
            if (person.getName() != null) {
                person.setId(oldperson.getId());
                personService.update(person);
                resultVO.setMessage("更新成功");
            } else {
                resultVO.setMessage("更新失败,name为空");
            }
        } else {
            resultVO.setMessage("更新失败,不存在 id = " + id + "的Person");
        }
        return resultVO.toString();
    }
复制代码

 测试:

4. 删除 DELETE

@RequestMapping(value = "/{id}", method = RequestMethod.DELETE)

复制代码
 1 @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
 2     @ResponseBody
 3     public String delete(@PathVariable Integer id, HttpServletRequest request, HttpServletResponse response) {
 4         ResultVO resultVO = new ResultVO();
 5         Person person = new Person();
 6         person = personService.findById(id);
 7         if (person != null) {
 8             resultVO.setSuccess(true);
 9             personService.delete(id);
10             resultVO.setMessage("删除成功");
11         } else {
12             resultVO.setMessage("删除失败,不存在id = " + id + "的Person");
13         }
14         return resultVO.toString();
15     }
复制代码

测试:

 

5. 问题

5.1 SpringMVC接收不了PUT、DELETE  bodydata 解决方法

需要在web.xml中加上:

    <filter>
        <filter-name>HttpMethodFilter</filter-name>
        <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>HttpMethodFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

 

然后在请求PUT、DELETE的时候,使用POST请求,在body中加入_method 参数,参数为PUT或DELETE,即可完成对PUT、DELETE的处理。

例如:

 

 strtus2 构建restful风格 https://www.ibm.com/developerworks/cn/java/j-lo-struts2rest/

posted @   loveincode  阅读(375)  评论(0编辑  收藏  举报
编辑推荐:
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异
· 三行代码完成国际化适配,妙~啊~
More
最简单即最美
有了信仰,自己要坚持努力 2017.07.09 21:34


点击右上角即可分享
微信分享提示