资讯专栏INFORMATION COLUMN

SpringBoot入门0x002:URL 映射

jlanglang / 2521人阅读

摘要:概述将某个请求映射到某个方法上这个注解可以加在某个或者某个方法上,如果加在上,则这个中的所有路由映射都将会加上这个前缀下面会有栗子,如果加在方法上,则没有前缀下面也有栗子。

0x000 概述

将某个http请求映射到某个方法上

0x001 @RequestMapping

这个注解可以加在某个Controller或者某个方法上,如果加在Controller上,则这个Controller中的所有路由映射都将会加上这个前缀(下面会有栗子),如果加在方法上,则没有前缀(下面也有栗子)。

@RequestMapping有以下属性

value: 请求的URL的路径

path: 和value一样

method: 请求的方法

consumes: 允许的媒体类型,也就是Content-Type

produces: 相应的媒体类型,也就是Accept

params: 请求参数

headers: 请求头部

0x002 路由匹配

首先编写ControllerController头部的@RestController将这个控制器标注为Rest控制器:

package com.lyxxxx.rest.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloController {

}

添加方法,并添加URL匹配:

    @RequestMapping()
    public Object hello() {
        return "hello";
    }
    

说明:上面添加了一个方法,名为hello,没有给定任何的属性,则默认匹配所有URL,方法为GET,所以我们可以直接使用GET方式来访问该路由

$ curl 127.0.0.1:8080
hello
$ curl 127.0.0.1:8080/user
hello
$ curl 127.0.0.1:8080/user/1
hello

精确匹配

@RequestMapping(value = "/hello2")
public Object hello2() {
    return "hello2";
}

说明:上面将value设置为hello2,所以访问hello2将会执行hello2方法

$ curl 127.0.0.1:8080/hello2
hello2

字符模糊匹配

@RequestMapping(value = "/hello3/*")
public Object hello3() {
    return "hello3";
}

说明:上面将value设置为/hello3/*,*为匹配所有字符,也就是访问hello3下的所有URL都将匹配该防范

$ curl 127.0.0.1:8080/hello3/user
hello3
 curl 127.0.0.1:8080/hello3/1
hello3

单字符模糊匹配

$ curl 127.0.0.1:8080/hello4/1
hello4

说明:上面将value设置为/hello4/?,?为匹配单个字符,匹配hello4/1,但是不会匹配hello4/11

$ curl 127.0.0.1:8080/hello4/1
hello4
$ curl 127.0.0.1:8080/hello4/12
{"timestamp":"2018-07-25T05:29:39.105+0000","status":404,"error":"Not Found","message":"No message available","path":"/hello4/12"}

全路径模糊匹配

@RequestMapping(value = "/hello5/**")
public Object hello5() {
    return "hello5";
}

说明:上面将value设置为/hello5/**,**为匹配所有路径,所以hello5下面的所有路由都将匹配这个方法

$ curl 127.0.0.1:8080/hello5
hello5
$ curl 127.0.0.1:8080/hello5/user/1
hello5

多个路径匹配

@RequestMapping(value = {"/hello6", "/hello6/1"})
public Object hello6() {
    return "hello6";
}
$ curl 127.0.0.1:8080/hello6
hello6
$ curl 127.0.0.1:8080/hello6/1
hello6F

读取配置

// resources/application.properties

app.name=hello7

// com.lyxxxx.rest.controller.HelloController

@RequestMapping(value = "${app.name}")
public Object hello7() {
    return "hello7";
}
$ curl 127.0.0.1:8080/hello7
hello7

0x003 方法匹配

匹配请求中的method,写在RequestMethod中的枚举值:

GET

HEAD

POST

PUT

PATCH

DELETE

OPTIONS

TRACE

package com.lyxxxx.rest.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class MethodController {
    @RequestMapping(path = "method/get", method = RequestMethod.GET)
    public Object get() {
        return "get";
    }

    @RequestMapping(path = "method/head", method = RequestMethod.HEAD)
    public Object head() {
        return "head";
    }

    @RequestMapping(path = "method/post", method = RequestMethod.POST)
    public Object post() {
        return "post";
    }

    @RequestMapping(path = "method/put", method = RequestMethod.PUT)
    public Object put() {
        return "put";
    }

    @RequestMapping(path = "method/patch", method = RequestMethod.PATCH)
    public Object patch() {
        return "patch";
    }

    @RequestMapping(path = "method/delete", method = RequestMethod.DELETE)
    public Object delete() {
        return "delete";
    }

    @RequestMapping(path = "method/options", method = RequestMethod.OPTIONS)
    public Object options() {
        return "options";
    }

    @RequestMapping(path = "method/trace", method = RequestMethod.TRACE)
    public Object trace() {
        return "trace";
    }


}
$ curl -X GET  127.0.0.1:8080/method/get
get
$ curl -X POST 127.0.0.1:8080/method/post
post
$ curl -X DELETE 127.0.0.1:8080/method/delete
delete
$ curl -X PUT 127.0.0.1:8080/method/put
put
...
0x003 params 匹配

除了可以匹配URLmethod之外,还可以匹配params

package com.lyxxxx.rest.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class ParamsController {
    @RequestMapping(path = "/params", params = "userId=1")
    public Object params() {
        return "params";
    }
}
$ curl 127.0.0.1:8080/params?userId=1
params
0x004 header 匹配
@RestController
public class HeaderController {
    @RequestMapping(path = "/headers", headers = "userId=1")
    public Object headers() {
        return "headers";
    }
}
$ curl 127.0.0.1:8080/headers
{"timestamp":"2018-08-01T06:11:40.037+0000","status":404,"error":"Not Found","message":"No message available","path":"/headers"}
$ curl 127.0.0.1:8080/headers -H "userId:1"
headers
0x005 consumes 匹配
@RestController
public class ConsumesController {

    @RequestMapping(value = "consumes",consumes = "application/json")
    public Object json() {
        return "consumes";
    }
}
$ curl 127.0.0.1:8080/consumes -H "Content-Type:application/json"
consumes
$ curl 127.0.0.1:8080/consumes -H "Content-Type:none"
{"timestamp":"2018-08-01T06:15:10.919+0000","status":415,"error":"Unsupported Media Type","message":"Invalid mime type "none": does not contain "/"","path":"/consumes"}
0x006 produces 匹配
@RestController
public class ProducesController {
    @RequestMapping(value = "/produces", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public Object produces() {
        return "produces";
    }
}
$ curl 127.0.0.1:8080/produces 
produces
$ curl 127.0.0.1:8080/produces -H "Accept:text"
// 空
0x007 说明

以上参考数据:《Spring Boot2精髓 从构建小系统到架构分部署大系统》

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/76491.html

相关文章

  • SpringBoot入门0x001:idea创建 SpringBoot 项目并运行

    摘要:创建项目创建一个项目选择填写,这两个可以组合成,一般是项目域名倒置,是项目名,然后由这两个组合成主包名。等待初次导包结束查看创建一个最简单的服务并测试添加一个打开,并点击运行使用自带服务自带测试,或者其他任意工具,看到返回就成功了 0x001 创建项目 创建一个项目showImg(https://segmentfault.com/img/bVbeaIU?w=777&h=482); ...

    Miracle_lihb 评论0 收藏0
  • SpringBoot整合MyBatis并使用Redis作为缓存组件的Demo

    摘要:本博客猫叔的博客,转载请申明出处本系列教程为项目附带。历史文章如何在安装最新版安装安装最新版的入门教程的入门教程安装教程安装流程安装如果不清楚是什么,请查看的文档和简介,这里给出的安装过程安装虚拟机如果有远程服务器的,请略过此步骤本文推 本博客 猫叔的博客,转载请申明出处本系列教程为HMStrange项目附带。 Auth:HMStrange-TIAN e-mail:zhangqihao...

    mo0n1andin 评论0 收藏0
  • 慕课网_《2小时学会SpringBoot》学习总结

    摘要:小时学会学习总结时间年月日星期六说明本文部分内容均来自慕课网。慕课网教学示例源码暂无。数据库操作下第六章事务管理事务管理只有查询的时候不加事务,其它任何操作都要加事务。第七章课程回顾课程回顾总结介绍安装配置的使用数据库操作 《2小时学会SpringBoot》学习总结 时间:2017年2月18日星期六说明:本文部分内容均来自慕课网。@慕课网:http://www.imooc.com教学示...

    aisuhua 评论0 收藏0

发表评论

0条评论

jlanglang

|高级讲师

TA的文章

阅读更多
最新活动
阅读需要支付1元查看
<