SpringMVC4.3x教程之九RESTFul使用

来源:互联网 发布:mysql创建多个用户 编辑:程序博客网 时间:2024/05/16 15:37

REST(英文:Representational State Transfer,简称REST)描述了一个架构样式的网络系统.REST 指的是一组架构约束条件和原则。满足这些约束条件和原则的应用程序或设计就是 RESTful。
在服务器端,应用程序状态和功能可以分为各种资源。资源是一个有趣的概念实体,它向客户端公开。资源的例子有:应用程序对象、数据库记录、算法等等。每个资源都使用 URI (Universal Resource Identifier) 得到一个唯一的地址。所有资源都共享统一的接口,以便在客户端和服务器之间传输状态。使用的是标准的 HTTP 方法,比如 GET、PUT、POST 和 DELETE。Hypermedia 是应用程序状态的引擎,资源表示通过超链接互联.——摘自百度百科
REST核心特点:
1、url就是一个资源
2、可以通过请求方式对资源进行增删改查的操作

而符合REST的就是RESTFul
SpringMVC支持RESTFul的使用,主要可以借助@PathVariable,RequestMethod,Spring表单标签等实现。
下面就通过实例来看一下:
控制器:

//RESTFul风格的接口@Controllerpublic class UserController {    //访问页面    @RequestMapping("/restful")    public String pre() {        // TODO Auto-generated method stub        return "restful";    }    //实现User资源的操作---RESTFul    //新增    @RequestMapping(value="/user/{name}/{pass}",method=RequestMethod.POST)    public void test1(@PathVariable String name,@PathVariable String pass,HttpServletResponse response) throws IOException{        System.out.println("POST:用户名:"+name+"--->密码:"+pass);        response.getWriter().println("POST请成功");    }    //查询    @RequestMapping(value="/user/{name}/{pass}",method=RequestMethod.GET)    public void test2(@PathVariable String name,@PathVariable String pass,HttpServletResponse response) throws IOException{        System.out.println("GET:用户名:"+name+"--->密码:"+pass);        response.getWriter().println("GET请成功");    }    //修改    @RequestMapping(value="/user/{name}/{pass}",method=RequestMethod.PUT)    public void test3(@PathVariable String name,@PathVariable String pass,HttpServletResponse response) throws IOException{        System.out.println("PUT:用户名:"+name+"--->密码:"+pass);        response.getWriter().println("PUT请成功");    }    //删除    @RequestMapping(value="/user/{name}/{pass}",method=RequestMethod.DELETE)    public void test4(@PathVariable String name,@PathVariable String pass,HttpServletResponse response) throws IOException{        System.out.println("DELETE:用户名:"+name+"--->密码:"+pass);        response.getWriter().println("DELETE请成功");    }}

只要是操作User那么url都是一样:http://localhost:8080/SpringMVC_5/user/
那么我们需要通过请求方式来区分具体是哪种操作。
前端:

<%@ page language="java" contentType="text/html; charset=UTF-8"    pageEncoding="UTF-8"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>RESTFul接口的请求</title><script type="text/javascript" src="js/jquery-3.2.1.min.js"></script><script  type="text/javascript">var url1="http://localhost:8080/SpringMVC_5/user/";$(function(){    $("#btn").click(function(){        var n=$("#n").val();        var p=$("#p").val();        var t=$("#t").val();        $.ajax({            url:url1+n+"/"+p,            type:t,            success:function(data){                alert(data);            },            error:function(e){                console.log(e);            }        });    });});</script></head><body>用户名:<input id="n"/><br/>密码:<input id="p"/><br/>请求方式:<select id="t"><option>get</option><option>post</option><option>put</option><option>delete</option></select><br/><button id="btn">发起请求</button></body></html>

这样就可以了。

阅读全文
0 0
原创粉丝点击