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注解详解 的全部内容了,希望能对您有所帮助!

相关内容

热门资讯

今日重大通报!微乐小程序自建房... 今日重大通报!微乐小程序自建房辅助!微乐家乡游戏攻略(果然有辅助方法)1、免费辅助多个强度级别选择2...
推荐十款!微乐小程序辅助工具!... 推荐十款!微乐小程序辅助工具!微乐小程序自建房透视下载(总是是真的有辅助工具)1、许多玩家不知道辅助...
重大通报!微乐小程序辅助工具!... 重大通报!微乐小程序辅助工具!微乐贵阳捉鸡麻将能不能开挂(一直存在有辅助器)1、全新机制【ai辅助工...
教程攻略!微乐小程序自建房辅助... 教程攻略!微乐小程序自建房辅助!微信小程序微乐辅助器下载(竟然是真的有辅助教程)1、任何透视是真的假...
每日必看推荐!微乐小程序自建房... 每日必看推荐!微乐小程序自建房辅助!小程序跑得快的技巧(果然真的是有辅助插件)1、点击下载安装,脚本...
我来教大家!微乐小程序自建房辅... 我来教大家!微乐小程序自建房辅助!微信卡五星小程序辅助(好像是有辅助器)1、该软件可以轻松地帮助玩家...
发现一款!微乐小程序辅助工具!... 您好,这款游戏可以开挂的,确实是有挂的,需要了解加去威信【136704302】很多玩家在这款游戏中打...
今日科普!微乐小程序辅助工具!... 今日科普!微乐小程序辅助工具!微乐小程序插件免费(其实是有辅助脚本)1、全新机制【ai辅助工具激活码...
教学盘点!微乐小程序自建房辅助... 教学盘点!微乐小程序自建房辅助!微乐自建房提升运势(真是是真的有辅助工具)1、玩家可以在线上大神俱乐...
玩家必看教程!微乐小程序辅助工... 您好,这款游戏可以开挂的,确实是有挂的,需要了解加去威信【136704302】很多玩家在这款游戏中打...