SPRINGMVC中的RESTFUL风格

RESTful架构:

是一种设计的风格,并不是标准,只是提供了一组设计原则和约束条件,也是目前比较流行的一种互联网软件架构。它结构清晰、符合标准、易于理解、扩展方便,所以正得到越来越多网站的采用。

关于RESTful架构给你一个链接讲的挺好的:http://www.ruanyifeng.com/blog/2011/09/restful

这里我结合springMVC讲解一下RESTful在springMVC中的使用,在讲之前先来看看RESTful提倡哪些做法:

  1. 他会对url进行规范:

    a) 非REST风格的url:localhost:8080/springmvc?name=admin&password=123;

    b) REST风格的url:localhost:8080/springmvc/admin/123;

  分析:

    1. 直观上看是不是比较简洁
    2. 看不懂:隐藏了参数名称,安全性,防止被攻击
    3. 所有的url都可以当成是资源
  1. 对http的方法进行规范

      a)不管是删除,添加,更新….使用的url都是一致,那么如果需要删除,就把http的方法设置删除

      b) 控制器:通过判断http的方法来执行操作(增删改查)

     目前这种做法还没有被广泛采用

  3.对contentType也进行规范

      a) 就是在请求是指定contentType的类型(json交互中就有体现)

  4.接下来看看springMVC中怎么实现RESTful风格

  首先:你在请求路径上@RequestMapping(value = "/hello_rest/{name}/{password}")需要用{}来动态匹配参数

  其次:方法的形参上要@PathVariable("name")来匹配上面的参数,这里@PathVariable中的字符串必须和你{}中的名字一致

  访问路径:localhost:8080/工程名/hello_rest/xx/xx(其中xx就是你随便填写的内容,它会匹配到后台的name和password的值)

    比如:

      你输入的路径是:localhost:8080/工程名/hello_rest/admin/admin

      后台会匹配到:name="amdin",password="admin"

 

/**
     * 1.路径的变化:/hello_rest/{name}/{password}其中{}相当于可以的参数
     * 2.参数的写法:需要利用@PathVariable("name")来匹配上面的参数
     * 3.至于@PathVariable后面跟的形参你就可以随便命名了
     * @param username
     * @param password
     * @return
     */
    @RequestMapping(value = "/hello_rest/{name}/{password}")
    public String hello_rest(@PathVariable("name") String username,
            @PathVariable("password") String password) {
        if("admin".equals(username)&"123".equals(password)){
            System.out.println("登录成功");
            return "hello";
        }
        return "hello";
    }

 

 这种写法和第一种类似,只不过是把{}动态匹配参数的放到前面去了,其原理是一样的,不多说,直接看看这个访问路径的写法就好

   访问路径:localhost:8080/工程名/xx/xx/hello_rest

  只是把参数由后面放到前面去了而已。

/**
     * 1.路径的变化:/{name}/{password}/hello_rest其中{}相当于可以的参数
     * 2.参数的写法:需要利用@PathVariable("name")来匹配上面的参数
     * 3.至于@PathVariable后面跟的形参你就可以随便命名了
     * @param username
     * @param password
     * @return
     */
    @RequestMapping(value = "/{name}/{password}/hello_rest")
    public String hello_rest2(@PathVariable("name") String username,
            @PathVariable("password") String password) {
        if("admin".equals(username)&"123".equals(password)){
            System.out.println("登录成功");
            return "hello";
        }
        return "hello";
    }


http协议

 

@Controller
public class RestController {
	/**
	 * /user/1
	 * @param userId
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/user/{id}",method=RequestMethod.GET)
	public String index(@PathVariable(value="id") String userId) throws Exception{
		return "/day1/user.jsp";
	}
	
	@RequestMapping(value="/user",method=RequestMethod.POST)
	public String addUser(String name,HttpServletResponse response) throws Exception{
		response.getWriter().println(name+" add success");
		return null;
	}
	
	@RequestMapping(value="/user/{id}",method=RequestMethod.PUT)
	public String updateUser(@PathVariable(value="id") String userId,HttpServletResponse response) throws Exception{
		response.getWriter().println(userId+" update success");
		return null;
	}
	
	@RequestMapping(value="/user/{id}",method=RequestMethod.DELETE)
	public String deleteUser(@PathVariable(value="id") String userId,HttpServletResponse response) throws Exception{
		response.getWriter().println(userId+" delete success");
		return null;
	}
}


这里必要要对应增删改查的动作 不然匹配不上会报405的错

 

 

    <form action="${pageContext.request.contextPath}/user/12" method="post">
    	<input type="hidden" name="_method" value="delete"/>
    	<input type="text" name="name"/>
    	<input type="submit" value="提交"/>
    </form>

 

 

 

 

 

 

 

 


 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值