0

0

怎么将某个http请求映射到某个方法上?SpringBoot入门:URL 映射

php是最好的语言

php是最好的语言

发布时间:2018-07-27 10:24:02

|

6319人浏览过

|

来源于php中文网

原创

0x000 概述:将某个http请求映射到某个方法上。0x001 @requestmapping:这个注解可以加在某个controller或者某个方法上,如果加在controller上

0x000 概述

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

0x001 @RequestMapping

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

@RequestMapping有以下属性

  • value: 请求的URL的路径

  • path: 和value一样

  • method: 请求的方法

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

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

  • params: 请求参数

  • headers: 请求头部

0x002 路由匹配

  1. 首先编写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 {
    
    }
  2. 添加方法,并添加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
  3. 精确匹配

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

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

    $ curl 127.0.0.1:8080/hello2
    hello2
  4. 字符模糊匹配

    @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
  5. 单字符模糊匹配

    PathFinder
    PathFinder

    AI驱动的销售漏斗分析工具

    下载
    $ 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"}
  6. 全路径模糊匹配

    @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
  7. 多个路径匹配

    @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
  8. 读取配置

    // 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 说明

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

相关文章:

mybatis入门基础(四)----输入映射和输出映射

django中“url映射规则”和“服务端响应顺序”

相关视频:

CSS3 入门教程

热门AI工具

更多
DeepSeek
DeepSeek

幻方量化公司旗下的开源大模型平台

豆包大模型
豆包大模型

字节跳动自主研发的一系列大型语言模型

WorkBuddy
WorkBuddy

腾讯云推出的AI原生桌面智能体工作台

腾讯元宝
腾讯元宝

腾讯混元平台推出的AI助手

文心一言
文心一言

文心一言是百度开发的AI聊天机器人,通过对话可以生成各种形式的内容。

讯飞写作
讯飞写作

基于讯飞星火大模型的AI写作工具,可以快速生成新闻稿件、品宣文案、工作总结、心得体会等各种文文稿

即梦AI
即梦AI

一站式AI创作平台,免费AI图片和视频生成。

ChatGPT
ChatGPT

最最强大的AI聊天机器人程序,ChatGPT不单是聊天机器人,还能进行撰写邮件、视频脚本、文案、翻译、代码等任务。

相关专题

更多
spring框架介绍
spring框架介绍

本专题整合了spring框架相关内容,想了解更多详细内容,请阅读专题下面的文章。

160

2025.08.06

Java Spring Security 与认证授权
Java Spring Security 与认证授权

本专题系统讲解 Java Spring Security 框架在认证与授权中的应用,涵盖用户身份验证、权限控制、JWT与OAuth2实现、跨站请求伪造(CSRF)防护、会话管理与安全漏洞防范。通过实际项目案例,帮助学习者掌握如何 使用 Spring Security 实现高安全性认证与授权机制,提升 Web 应用的安全性与用户数据保护。

88

2026.01.26

Python Web 框架 Django 深度开发
Python Web 框架 Django 深度开发

本专题系统讲解 Python Django 框架的核心功能与进阶开发技巧,包括 Django 项目结构、数据库模型与迁移、视图与模板渲染、表单与认证管理、RESTful API 开发、Django 中间件与缓存优化、部署与性能调优。通过实战案例,帮助学习者掌握 使用 Django 快速构建功能全面的 Web 应用与全栈开发能力。

166

2026.02.04

css3教程
css3教程

php中文网为大家提供css3教程合集,CSS3的语法是建立在CSS原先版本基础上的,它允许使用者在标签中指定特定的HTML元素而不必使用多余的class、ID或JavaScript。php中文网还为大家带来css3的相关下载资源、相关课程以及相关文章等内容,供大家免费下载使用。

427

2023.06.14

有哪些css3渐变属性
有哪些css3渐变属性

css3中渐变属性有linear-gradient、radial-gradient、conic-gradient、repeating-linear-gradient、repeating-radial-gradient等。本专题为大家提供相关的文章、下载、课程内容,供大家免费下载体验。

143

2023.11.01

mybatis一级缓存和二级缓存
mybatis一级缓存和二级缓存

在MyBatis中,一级缓存和二级缓存是两种不同级别的缓存机制,它们都可以用来提高性能。本专题提供mybatis一级缓存和二级缓存相关文章,大家可以免费阅读。

304

2023.08.21

ibatis和mybatis有什么区别
ibatis和mybatis有什么区别

ibatis和mybatis的区别:1、基本信息不同;2、开发时间不同;3、功能与易用性;4、配置文件;5、入参类型与出参类型;6、返回结果集接受方式;7、语法差异;8、数据库方言支持;9、插件支持;10、社区活跃度;11、全球化支持。本专题为大家提供相关的文章、下载、课程内容,供大家免费下载体验。

94

2024.02.23

mybatis如何配置数据库连接
mybatis如何配置数据库连接

mybatis配置数据库连接的方法:1、指定数据源;2、配置事务管理器;3、配置类型处理器和映射器;4、使用环境元素;5、配置别名。本专题为大家提供相关的文章、下载、课程内容,供大家免费下载体验。

106

2024.02.23

Python异步编程与Asyncio高并发应用实践
Python异步编程与Asyncio高并发应用实践

本专题围绕 Python 异步编程模型展开,深入讲解 Asyncio 框架的核心原理与应用实践。内容包括事件循环机制、协程任务调度、异步 IO 处理以及并发任务管理策略。通过构建高并发网络请求与异步数据处理案例,帮助开发者掌握 Python 在高并发场景中的高效开发方法,并提升系统资源利用率与整体运行性能。

37

2026.03.12

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
Redis6入门到精通超详细教程
Redis6入门到精通超详细教程

共47课时 | 5.6万人学习

Kotlin 教程
Kotlin 教程

共23课时 | 4.4万人学习

C# 教程
C# 教程

共94课时 | 11.2万人学习

关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号