spring @RequestMapping注解与Spring @RequestParam注解的使用

 

一,关于@RequestMapping:

1.@RequestMapping(value="/login",method = RequestMethod.GET)

   请求方式比如 GET, PUT, POST, DELETE 以及 PATCH,method不写,默认为任何访问方式都行。

2.@RequestMapping 来处理多个 URI 

@RestController
@RequestMapping("/home")
public class IndexController {

    @RequestMapping(value = {
        "",
        "/page",
        "page*",
        "view/*,**/msg"
    })
    String indexMultipleMapping() {
        return "Hello from index multiple mapping.";
    }
}

如你在这段代码中所看到的,@RequestMapping 支持统配符以及ANT风格的路径。前面这段代码中,如下的这些 URL 都会由 indexMultipleMapping() 来处理:

  • localhost:8080/home
  • localhost:8080/home/
  • localhost:8080/home/page
  • localhost:8080/home/pageabc
  • localhost:8080/home/view/
  • localhost:8080/home/view/view

3.使用 @RequestMapping 来处理消息头

参考文档https://www.cnblogs.com/Sunnor/p/6129091.html

@RequestMapping 注解提供了一个 header 元素来根据请求中的消息头内容缩小请求映射的范围

在可以指定 header 元素的值,用 myHeader = myValue 这样的格式:

 

@RestController
@RequestMapping("/home")
public class IndexController {
    @RequestMapping(value = "/head", headers = {
        "content-type=text/plain"
    })
    String post() {
        return "Mapping applied along with headers";
    }
}

 

在上面这段代码中, @RequestMapping 注解的 headers 属性将映射范围缩小到了 post() 方法。有了这个,post() 方法就只会处理到 /home/head 并且 content-typeheader 被指定为 text/plain 这个值的请求。

你也可以像下面这样指定多个消息头:

 

@RestController
@RequestMapping("/home")
public class IndexController {
    @RequestMapping(value = "/head", headers = {
        "content-type=text/plain",
        "content-type=text/html"
    }) String post() {
        return "Mapping applied along with headers";
    }
}

这样, post() 方法就能同时接受 text/plain 还有 text/html 的请求了。

4.使用 @RequestMapping 处理动态 URI

@RequestMapping 注解可以同 @PathVaraible 注解一起使用,用来处理动态的 URI,URI 的值可以作为控制器中处理方法的参数。你也可以使用正则表达式来只处理可以匹配到正则表达式的动态 URI。

@RestController
@RequestMapping("/home")
public class IndexController {
    @RequestMapping(value = "/fetch/{id}", method = RequestMethod.GET)
    String getDynamicUriValue(@PathVariable String id) {
        System.out.println("ID is " + id);
        return "Dynamic URI parameter fetched";
    }
    @RequestMapping(value = "/fetch/{id:[a-z]+}/{name}", method = RequestMethod.GET)
    String getDynamicUriValueRegex(@PathVariable("name") String name) {
        System.out.println("Name is " + name);
        return "Dynamic URI parameter fetched using regex";
    }
}

在这段代码中,方法 getDynamicUriValue() 会在发起到 localhost:8080/home/fetch/10 的请求时执行。这里 getDynamicUriValue() 方法 id 参数也会动态地被填充为 10 这个值。

方法 getDynamicUriValueRegex() 会在发起到 localhost:8080/home/fetch/category/shirt 的请求时执行。不过,如果发起的请求是 /home/fetch/10/shirt 的话,会抛出异常,因为这个URI并不能匹配正则表达式。

@PathVariable 同 @RequestParam的运行方式不同。你使用 @PathVariable 是为了从 URI 里取到查询参数值。换言之,你使用 @RequestParam 是为了从 URI 模板中获取参数值。

5.@RequestMapping 快捷方式

Spring 4.3 引入了方法级注解的变体,也被叫做 @RequestMapping 的组合注解。组合注解可以更好的表达被注解方法的语义。它们所扮演的角色就是针对 @RequestMapping 的封装,而且成了定义端点的标准方法。

例如,@GetMapping 是一个组合注解,它所扮演的是 @RequestMapping(method =RequestMethod.GET) 的一个快捷方式。

方法级别的注解变体有如下几个: 

  •  @GetMapping
  • @PostMapping
  • @PutMapping
  • @DeleteMapping
  • @PatchMapping

二,关于@RequestParam

1、@RequestParam作用:

将请求参数绑定到你控制器的方法参数上

2、@RequestParam三个属性:

@RequestParam(value=”参数名”, required=true/false, defaultValue=””):

1)value:请求参数名(必须配置)单一参数时,可简化如下:

   /**
    * @Description:    url参数中的name必须要和@RequestParam("name")一致
    * @Author:         vdi100
    */
    @GetMapping("edit1")
    public String edit1(@RequestParam("userId") Integer userId, Model model) {
        System.out.println("*******************" + userId);
        model.addAttribute("userId" , userId);
        return "/admin/ronghe/rongheMobileList/edit";
    }
    /**
     * @Description:    url参数中的name必须要和@RequestParam("name")一致
     *                  参数名字不一样
     * @Author:         vdi100
     */
    @GetMapping("edit2")
    public String edit2(@RequestParam("userId") Integer id, Model model) {
        System.out.println("*******************" + id);
        model.addAttribute("id" , id);
        return "/admin/ronghe/rongheMobileList/edit";
    }
(2)required:是否必需,默认为 true,即 请求中必须包含该参数,如果没有包含,将会抛出异常(可选配置)

   /**
    * @Description:    required = true时必须有参数id,否则会报错
    *                  required = false时参数id可不传,默认为null,所以此时参数类型不能为int
    * @Author:         vdi100
    */
    @GetMapping("edit3")
    public String edit3(@RequestParam(value = "id", required = true) Integer id, Model model) {
        System.out.println("*******************" + id);
        model.addAttribute("id" , id);
        return "/admin/ronghe/rongheMobileList/edit";
    }
(3)defaultValue:默认值,如果设置了该值,required 将自动设为 false,无论你是否配置了required,配置了什么值,都是 false;如果没有传该参数,就使用默认值(可选配置)

   /**
    * @Description:    设置defaultValue值时,required无论设置是何值,都默认为false
    * @Author:         vdi100
    */
    @GetMapping("edit4")
    public String edit4(@RequestParam(value = "id", required = false, defaultValue = "10") Integer id, Model model) {
        System.out.println("*******************" + id);
        model.addAttribute("id" , id);
        return "/admin/ronghe/rongheMobileList/edit";
    }
3、@RequestParam接收前台传递过来的数组:

如下图传递的参数是数组:

@RequestParam接收参数:

   /**
    * @Description:    接收的参数ids是数组
    * @Author:         vdi100
    */
    @PostMapping("delete")
    @ResponseBody
    public RestResponse delete(@RequestParam(value = "ids[]", required = false) List<Long> ids) {
        if (null == ids) {
            return RestResponse.failure("ID不能为空" );
        }
        for (Long id : ids) {
            rongheMobileListService.deleteById(id);
        }
        return RestResponse.success();
    }

@RequestMapping

参考文档:https://www.cnblogs.com/zj-phper/p/8961719.html

Spring @RequestParam注解的使用

参考文档:https://blog.csdn.net/Third_Week/article/details/90376578

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值