美文网首页it技术IT技术征服Spring
SpringMVC(一)Request Mapping

SpringMVC(一)Request Mapping

作者: yongguang423 | 来源:发表于2016-11-21 17:16 被阅读371次
    根据路径映射
    @RequestMapping("/mapping/path")
    public @ResponseBody String byPath() {
            return "Mapped by path!";
    }
    
    根据表达式映射
    @RequestMapping(value="/mapping/path/*", method=RequestMethod.GET)
    public @ResponseBody String byPathPattern(HttpServletRequest request) {
            return "Mapped by path pattern ('" + request.getRequestURI() + "')";
        }
    

    例如 url:http://localhost:8080/mapping/path/wildcard 即可进入此方法

    根据路径和方法映射
    @RequestMapping(value="/mapping/method", method=RequestMethod.GET)
    public @ResponseBody String byMethod() {
        return "Mapped by path + method";
    }
    

    同一个url可以根据请求方式映射不同的后台处理方法,例如/mapping/method,可以通过GET方法映射byMethod,通过POST方法映射byMethod1。

    根据路径、方法和参数映射
    @RequestMapping(value="/mapping/parameter", method=RequestMethod.GET, params="foo")
    public @ResponseBody String byParameter() {
        return "Mapped by path + method + presence of query parameter!";
    }
    

    例如 url:http://localhost:8080/mapping/parameter?foo=car 即可进入此方法。

    如果需要匹配多个方法,则使用params={"foo","t"}即可,如下段代码所示:

    @RequestMapping(value="/mapping/parameter", method=RequestMethod.GET, params={"foo","t"})
    public @ResponseBody String byParameter() {
        return "Mapped by path + method + presence of query parameter!";
    }
    

    例如 url:http://localhost:8080/mapping/parameter?foo=car&t=xx可进入此方法。
    参数映射还可以按照如下方式匹配:

    1. 根据参数和参数值匹配params="foo=car"params="foo!=car"
    2. 参数名不等于匹配 params="!foo",只要参数不等于foo即可匹配;
    3. 组合匹配params={foo=car,create,id=1} ,需要满足参数foo=car 包含create参数,包括id参数并且id=1方可匹配。
    根据header参数映射
    @RequestMapping(value="/mapping/header", method=RequestMethod.GET, headers="FooHeader=foo")
    public @ResponseBody String byHeader() {
        return "Mapped by path + method + presence of header!";
    }
    

    通过以下代码设置header参数,即可访问此方法

        $("#byHeader").click(function(){
            var link = $(this);
            $.ajax({ url: this.href, dataType: "text", beforeSend: function(req) { req.setRequestHeader("FooHeader", "foo"); }, success: function(form) { MvcUtil.showSuccessResponse(form, link); }, error: function(xhr) { MvcUtil.showErrorResponse(xhr.responseText, link); }});
            return false;
        });
    

    header映射还有如下几种方式
    与参数映射类似header映射还可以按照如下方式匹配:

    1. 根据参数和参数值匹配headers="foo=car"headers="foo!=car"
    2. 参数名不等于匹配 headers="!foo",只要参数不包含foo即可匹配;
    3. 组合匹配headers={foo=car,create,id=1} ,需要满足参数foo=car 包含create参数,包括id参数并且id=1方可匹配。
    根据请求内容类型匹配
    @RequestMapping(value="/mapping/consumes", method=RequestMethod.POST, consumes=MediaType.APPLICATION_JSON_VALUE)
    public @ResponseBody String byConsumes(@RequestBody JavaBean javaBean) {
        return "Mapped by path + method + consumable media type (javaBean '" + javaBean + "')";
    }
    

    该方法匹配 contentType='application/json'的请求。 通过 @RequestBody来接受json字符串参数。SpringMvc会根据参数名匹配javabean的属性,并自动转型。注意这里只能将前端传的json字符串转型成javabean,而不是json对象,json对象要用JSON.stringify(data)转成字符串,同时ajax请求的时候也要指定dataType: "json",contentType:"application/json"
    如图所示,请求内容类型是application/json 即可匹配此方法

    1.png

    通过如下代码,发起请求,请求参数是json

        $("form.readJsonForm").submit(function() {
            var form = $(this);
            var button = form.children(":first");
            var data = form.hasClass("invalid") ?
                    "{ \"foo\": \"bar\" }" : 
                    "{ \"foo\": \"bar\", \"fruit\": \"apple\" }";
            $.ajax({ type: "POST", url: form.attr("action"), data: data, contentType: "application/json", dataType: "text", success: function(text) { MvcUtil.showSuccessResponse(text, button); }, error: function(xhr) { MvcUtil.showErrorResponse(xhr.responseText, button); }});
            return false;
        });
    
    根据客户端接受的响应类型匹配
    @RequestMapping(value="/mapping/produces", method=RequestMethod.GET, produces=MediaType.APPLICATION_JSON_VALUE)
    public @ResponseBody JavaBean byProducesJson() {
            return new JavaBean();
    }
    
        $("a.writeJsonLink").click(function() {
            var link = $(this);
            $.ajax({ url: this.href,
                beforeSend: function(req) {
                    if (!this.url.match(/\.json$/)) {
                        req.setRequestHeader("Accept", "application/json");
                    }
                },
                success: function(json) {
                    MvcUtil.showSuccessResponse(JSON.stringify(json), link);
                },
                error: function(xhr) {
                    MvcUtil.showErrorResponse(xhr.responseText, link);
                }});
            return false;
        });
    

    以上方式通过设置header的accecpt 为application/json指定客户端接受的响应类型为json,后台通过produces=MediaType.APPLICATION_JSON_VALUE来匹配该路径。如不指定accecpt,通过请求url加.json后缀也可以匹配produces=MediaType.APPLICATION_JSON_VALUE对应的路径

    相关文章

      网友评论

        本文标题:SpringMVC(一)Request Mapping

        本文链接:https://www.haomeiwen.com/subject/syvupttx.html