@RequestMapping的作用、参数和基础用法

作用

在Spring MVC 中使用 @RequestMapping 来映射请求,也就是通过它来指定控制器可以处理哪些URL请求,即将请求和处理请求的控制器方法关联起来,建立映射关系。

在这里插入图片描述
如上图,我创建了一个controller包,并在其内创建了HelloController的Java文件,具体代码为:

package com.example.demo.controller;

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

@RestController
public class HelloController {
    @RequestMapping("/hello2")
    public String hello(){
        return "helloworld";
    }
}

这边的@RequestMapping使用了最简单的效果,在项目URL后加入/hello2即可出现return的内容,如下:
在这里插入图片描述
这说明可以使用@RequestMapping注解来将请求URL,如/hello2等,映射到整个类上或某个特定的处理器方法上。类级别的注解负责将一个特定(或符合某种模式)的请求路径映射到一个控制器上,同时通过方法级别的注解来细化映射,即根据特定的HTTP请求方法(“GET”“POST”方法等)、HTTP请求中是否携带特定参数等条件,将请求映射到匹配的方法上。

一般属性

path|value : 指定访问路径
method : 指定请求方式GET/POST/PUT/DELETE…
params : 指定请求该路径必须携带的参数
@RequestMapping可以选用其中任意部分来完成约束控制,也可以像上面的例子一样只将请求路径映射。

path|value : 指定访问路径

直接使用@RequestMapping(“xxxx”)亦或是@RequestMapping(path=“xxxx”)、@RequestMapping(value=“xxxx”)
效果是一样的。
经测试xxxx中首位的/是否加上对结果没有影响,即@RequestMapping("/hello")与@RequestMapping(“hello”)效果上是一致的。
在这里插入图片描述

package com.example.demo.controller;


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

@RestController
public class HelloController2 {
    @RequestMapping("/hello2-2")
    public String hello2(){
        return "hello2-2";
    }

    @RequestMapping(path="hello3-2")
    public String hello3(){
        return "rello3-2";
    }

    @RequestMapping(value="hello4-2")
    public String hello4(){
        return "rello4-2";
    }

//    @RequestMapping()
}

method : 指定请求方式GET/POST/PUT/DELETE…

@GetMapping即@RequestMapping(method = RequestMethod.GET)
@PostMapping即@RequestMapping(method = RequestMethod.POST)

GET、POST是方法的映射。
表示为@RequestMapping(method = RequestMethod.${方法})
如今往往使用@GetMapping、@PostMapping、@PutMapping、@DeleteMapping、@PatchMapping注解来帮助简化常用的HTTP方法的映射。
GET方法可以携带交互需要的所有数据,因此搜索百度或谷歌的时候,点击搜索形成的URL搜索关键字。但是对一些有安全需求的请求,如账号密码等,HTTP设计了POST请求,把请求信息放在HTTP请求里,防止信息泄露等问题。

@GetMapping只能通过url传参数。所对应的接口参数只能是用@RequestParam注解或者不注解

@RequestMapping(value = "/hello-get",method= RequestMethod.GET)
    public String hello_get(@RequestParam(value = "name", defaultValue = "World") String name){
        return String.format("您好,%s",name);
    }
    @GetMapping("hello-get2")
    public String hello_get2(@RequestParam(value = "name", defaultValue = "World") String name){
        return String.format("您好,%s",name);
    }

在这里插入图片描述
在这里插入图片描述
通过测试可以看到两种方法效果一致。
对参数测试可以用?参数名=参数值,如下:
在这里插入图片描述
使用post不能在网页上测试,可以用apipost测试

@PostMapping("hello-post")
    public String hello_post( String name){
        return String.format("您好,%s",name);
    }

在这里插入图片描述

  • 4
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
`@RequestMapping` 和 `@RequestParam` 是 Spring MVC 框架中常用的注解,用于处理 HTTP 请求映射和参数绑定。 `@RequestMapping` 注解用于将一个 HTTP 请求映射到一个特定的处理方法或控制器类。它可以用在类级别和方法级别上。在类级别上使用时,可以为整个控制器类指定一个基础的请求路径,而在方法级别上使用时,可以为具体的处理方法指定相对于类级别请求路径的具体路径。 示例: ```java @Controller @RequestMapping("/users") public class UserController { @RequestMapping(value = "/{id}", method = RequestMethod.GET) public String getUser(@PathVariable("id") int userId) { // 处理获取用户信息的逻辑 return "user"; } } ``` 上面的示例中,`@RequestMapping` 注解用于将 `/users/{id}` 请求映射到 `getUser()` 方法,其中 `{id}` 表示一个路径变量,在处理方法中通过 `@PathVariable` 注解将路径变量绑定到方法参数上。 `@RequestParam` 注解用于将请求参数绑定到方法参数上。它可以用于处理 GET、POST 等请求方法中的参数。默认情况下,`@RequestParam` 注解要求请求参数必须存在,否则会抛出异常。如果要将参数设置为可选,可以通过设置 `required` 属性为 `false`。 示例: ```java @Controller @RequestMapping("/users") public class UserController { @RequestMapping(value = "/search", method = RequestMethod.GET) public String searchUsers(@RequestParam("name") String name, @RequestParam(value = "age", required = false) Integer age) { // 处理根据姓名和年龄搜索用户的逻辑 return "users"; } } ``` 上面的示例中,`@RequestParam` 注解用于将 URL 查询参数 `name` 绑定到 `name` 参数上,并将其设置为必需参数;将 URL 查询参数 `age` 绑定到 `age` 参数上,并将其设置为可选参数。 通过使用 `@RequestMapping` 和 `@RequestParam` 注解,可以方便地处理不同的请求映射和参数绑定需求,使代码更加清晰和易于维护。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值