JavaEE:@RequestMapping注解详解

目录

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

  • 4
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
注解(Annotation)是Java中的一种特殊语法,用于在代码中添加元数据,以便在运行时进行处理。注解可以用于类、方法、字段等元素上,用来提供元数据信息,比如配置信息、描述信息等。 下面是一些常见的注解及其作用: 1. @RestController @RestController是Spring MVC中的注解,用于标识该类为RESTful风格的控制器。它是@Controller和@ResponseBody的组合注解,表示该类的所有方法都返回JSON或XML格式的数据。 2. @RequestMapping @RequestMapping用于指定请求的URL路径和HTTP请求方法。它可以用在类和方法上,用于处理HTTP请求。当用在类上时,表示该类处理的所有请求都是以该路径为前缀的。 3. @Autowired @Autowired是Spring框架中的注解,用于自动装配依赖对象。它可以用在构造方法、属性和方法上。当Spring容器加载时,会自动扫描所有被@Autowired标注的类,并将其注入到需要的类中。 4. @PostMapping @PostMapping是Spring MVC中的注解,用于指定处理HTTP POST请求的方法。它是@RequestMapping注解的衍生注解。 5. @RequestParam @RequestParam用于指定HTTP请求参数的名称和默认值。它可以用在方法的参数上,用于从HTTP请求中获取参数的值。 6. @Service @Service是Spring框架中的注解,用于标识该类为业务逻辑层的实现类。它可以用在类上,表示该类是一个服务类。 7. @Resource @Resource是JavaEE中的注解,用于指定依赖注入的名称。它可以用在属性、方法和构造方法上,用于指定需要注入的依赖对象的名称。 使用这些注解可以使代码更加简洁、易读,并且能够提高开发效率。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值