如何在SpringMVC中使用REST风格的url

如何在SpringMVC中使用REST风格的url

1.url写法:

get:/restUrl/{id}

post:/restUrl

delete:/restUrl/{id}

put:/restUrl

2.controller写法:

1)GET请求的目标方法:

    @RequestMapping(value="/restUrl/{id}", method=RequestMethod.GET)
    public String get(Map<String, Object> map, @PathVariable("id") Integer id){
        Object obj = new Object();
        map.put("obj", obj);
        return "success";   
    }

注意:

1.必须在@RequestMapping注解中添加method=RequestMethod.GET,表明这是一个处理get请求的目标方法

2.通过@PathVariable("id") Integer id注解,将url中的{id}值取出,并赋值给该注解修饰的入参id

2)POST请求的目标方法:

    @RequestMapping(value="/restUrl", method=RequestMethod.POST)
    public String post(Object obj){
        System.out.println(obj);
        return "success";
    }

注意:

1.必须在@RequestMapping注解中添加method=RequestMethod.POST,表明这是一个处理post请求的目标方法

2.post请求的url中不需要写参数{id}

3)DELETE请求的目标方法:

    @RequestMapping(value="/restUrl/{id}", method=RequestMethod.DELETE)
    public String delete(@PathVariable("id") Integerid){
        System.out.println(id);
        return "success";
    }

注意:

1.必须在@RequestMapping注解中添加method=RequestMethod.DELETE,表明这是一个处理delete请求的目标方法

2.url中必须带有参数{id}

4)PUT请求的目标方法:

    @RequestMapping(value="/restUrl", method=RequestMethod.PUT)
    public String put(Object obj){
        System.out.println(obj);
        return "success";
    }

注意:

1.必须在@RequestMapping注解中添加method=RequestMethod.PUT,表明这是一个处理put请求的目标方法

2.url中不需要带有参数{id}

3.如果需要使用@ModelAttribute来进行一些修改前的操作(如:先去数据库查询一个实体,在使用put目标方法),请参考我的另一篇博客《@ModelAttribute注解的使用详解》

3.jsp页面中的链接写法:

1)get请求:

<a href="${pageContext.request.contextPath}/user/restUrl/{id}">get user</a>

注意:

1.这里的{id}不能直接写{id},而是你要动态赋值的

2)post请求:

<form action="${pageContext.request.contextPath }/restUrl" method="post" >
    name:<input type="text" name="username"><br>
    password:<input type="password" name="password"><br>
    <input type="submit" value="submit">
</form>

注意:

1.因为超链接是get请求,所以要使用post风格的url请求必须使用表单

2.必须表明表单的提交方式为method=post

3)delete请求:

<a class="delete_href" href="${pageContext.request.contextPath }/restUrl/{id}">remove</a>
<form id="delete_form" action="" method="post">
    <input type="hidden" name="_method" value="DELETE">
</form>
$(function(){
    $(".delete_href").on("click", function(){var href = $(this).attr("href");
        $("#delete_form").attr("action", href).submit();
        return false;
    })
})

注意:

1.由于超链接只能发送get请求,我们需要发送delete请求的话,必须通过一个表单提交,将表单的post请求,转换成delete请求

2.在表单中添加一个隐藏域<input type="hidden" name="_method" value="DELETE">,能让表单在提交的时候将请求转换成delete请求

3.用js实现在点击超链接时,实际上提交的是表单。但是要注意在js之前,请先引入jquery文件

4)put请求:

<form action="${pageContext.request.contextPath }/restUrl" method="post" >
    <input type="hidden" name="_method" value="PUT">
    <input type="hidden" name="id" value="${id }">
    name:<input type="text" name="username"><br>
    password:<input type="password" name="password"><br>
    <input type="submit" value="submit">
</form>

注意:

1.跟delete请求类似,我们需要一个隐藏域<input type="hidden" name="_method" value="PUT">,来将post请求转换成put请求

原文地址:https://www.cnblogs.com/javafucker/p/7509999.html