JavaEE:@RequestMapping注解详解
创始人
2025-01-10 14:04:23
0

目录

        1、@RequestMapping 介绍

        2、@RequestMapping 使用

        3、@RequestMapping 支持的请求类型


1、@RequestMapping 介绍

        @RequestMapping 是Spring Web MVC 应用程序中最常被用到的注解之一,它是用来注册接口的路由映射的。

路由映射:

        当一个用户访问一个URL的时候,将用户的请求对应到程序中 某个类的某个方法 的过程就叫映射。

import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController;  @RestController public class UserController {     @RequestMapping("/user")     public String user() {         return "成功建立连接";     } }

这边就是将用户的请求对应到程序中的 user 方法

2、@RequestMapping 使用

        @RequestMapping 既可以修饰类,也可以修饰方法。当修饰类和方法时,访问的地址是类路径+方法路径,如果只使用方法路径去访问会报错。

@RequestMapping 标识一个类:设置映射请求的请求路径的初始信息

@RequestMapping 标识一个方法:设置映射请求的请求路径的具体信息

import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController;  @RestController @RequestMapping("/controller") public class UserController {     @RequestMapping("/user")     public String user() {         return "成功建立连接";     } }

@RequestMapping 的URL路径也可以是多层路径,最终访问时,依然是 类路径+方法路径。

import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController;  @RestController @RequestMapping("/controller/two") public class UserController {     @RequestMapping("/user/u")     public String user() {         return "成功建立连接";     } }

3、@RequestMapping 支持的请求类型

        @RequestMapping 既支持POST请求,也支持GET请求,也能接受其他的请求方式,简而言之就是 支持所有请求方式

        我们也可以通过代码来设置指定支持哪种,或哪几种请求。

        在RequestMapping注解的参数中设置,想要哪种就选哪种。(以GET请求为例)

import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController;  @RestController @RequestMapping("/controller") public class UserController {     @RequestMapping(value = "/user",method = RequestMethod.GET)     //仅能支持GET请求方式     public String user() {         return "成功建立连接";     } }

        想要使其能支持多种请求方式,就以数组形式列出即可:

@RestController @RequestMapping("/controller") public class UserController {     @RequestMapping(value = "/user",method = {RequestMethod.GET, RequestMethod.POST})     //能支持GET和POST请求方式     public String user() {         return "成功建立连接";     } }

        以上就是 JavaEE:@RequestMapping注解详解 的全部内容了,希望能对您有所帮助!

相关内容

热门资讯

绝活儿辅助!广西老友玩老是输怎... 绝活儿辅助!广西老友玩老是输怎么办(辅助挂)都是真的有辅助app(讲解有挂)在进入广西老友玩老是输怎...
法门辅助!福建13水插件(辅助... 法门辅助!福建13水插件(辅助挂)一贯是有辅助技巧(有挂技术)1、许多玩家不知道福建13水插件辅助怎...
办法辅助!潮友会app下载官方... 办法辅助!潮友会app下载官方辅助器(辅助挂)真是真的是有辅助app(有挂教程)该软件可以轻松地帮助...
妙招辅助!邯郸胡乐挂辅助(辅助... 妙招辅助!邯郸胡乐挂辅助(辅助挂)好像存在有辅助插件(有挂方略)1、上手简单,内置详细流程视频教学,...
教程书辅助!乐酷辅助(辅助挂)... 教程书辅助!乐酷辅助(辅助挂)其实存在有辅助脚本(有挂细节)乐酷辅助能透视中分为三种模型:乐酷辅助模...
学习辅助!决战卡五星辅助(辅助... 学习辅助!决战卡五星辅助(辅助挂)本来真的是有辅助软件(有人有挂)学习辅助!决战卡五星辅助(辅助挂)...
绝活辅助!边锋嘉兴麻将辅助器(... 绝活辅助!边锋嘉兴麻将辅助器(辅助挂)真是真的有辅助神器(新版有挂)1、边锋嘉兴麻将辅助器公共底牌简...
举措辅助!枫叶辅助器(辅助挂)... 举措辅助!枫叶辅助器(辅助挂)本来存在有辅助技巧(竟然有挂)1、下载好枫叶辅助器正确养号方法之后点击...
讲义辅助!点我达辅助(辅助挂)... 讲义辅助!点我达辅助(辅助挂)一直存在有辅助技巧(有人有挂)1、点我达辅助辅助器安装包、点我达辅助辅...
模块辅助!威信茶馆有挂的吗(辅... 模块辅助!威信茶馆有挂的吗(辅助挂)一直真的是有辅助脚本(揭秘有挂)1、玩家可以在威信茶馆有挂的吗线...