亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定

Spring常用注解-@RequestMapping(三)

標簽:
Java Spring

@RequestMapping是Spring Web应用中最常见的注解之一,它主要将Http的请求映射到我们的控制器和处理方法上。

@RequestMapping可以用在方法上,也可以用在类上,这里我们一起看下它的用法,可能会和@PathVariable或@RequestParam结合使用。

用法参考

  1. 注解写在类上和方法上。类上面的注解一般和写在方法上的注解一起使用。当请求为/example/index的时候就会映射到ExampleController的index方法上。然后下面是定义多个路径映射到一个方法上
@Controller
@RequestMapping("/example")
public class ExampleController {

    @RequestMapping("/index")
    public String index(){
        return "ok";
    }
    
    @RequestMapping({"/index1", "/", "index2"})
    public String indexMulti() {
        return "ok";
    }
}
  1. 指定特定的HTTP方法才能映射,必须是POST方法,并且路径为/method才可以
    @RequestMapping(value = "/method",method = RequestMethod.POST)
    public String testMethod(){
        return "ok";
    }
  1. 指定http的headers。只有在http请求时请求头里有header1=value1这个请求头的才可以进来
    @RequestMapping(value = "/header",headers = "header1=value1")
    public String testHeaders(){
        return "ok";
    }
  1. 匹配请求的content-type,输出特定类型的请求。只有当请求的content-type为时text/html才能匹配,返回的响应信息的类型为"application/json"或"application/xml"
    @RequestMapping(value = "/accept",produces = {"application/json","application/xml"}, consumes="text/html")
    public String testAccept(){
        return "ok";
    }
@PathVariable

@RequestMapping注解可以用于处理动态的URI,将URI中的一个或者多个值作为参数。甚至可以使用正则表达式。处理动态URI一般要配合@PathVariable注解一起使用。用法如下:

    // 当请求的URI为 /method7/123时,id的值为123
    @RequestMapping(value = "/method7/{id}")
    public String method7(
            @PathVariable("id") Integer id
    ) {
        return "method7 with id=" + id;
    }

    //请求的URI为/method8/10/Lisa时,id为10,name为Lisa
    @RequestMapping(value = "/method8/{id:[\\d]+}/{name}")
    public String method8(
            @PathVariable("id") Long id, 
            @PathVariable("name") String name
    ) {
        return "method8 with id= " + id + " and name=" + name;
    }
@RequestParam

有时候我们想从请求的地址中获取请求的参数,我们可以使用@RequestParam注解。用法如下:

    @RequestMapping(value = "/id")
    String getIdByValue(@RequestParam("id") String personId) {
        System.out.println("ID is " + personId);
        return "从URI中获取id的值";
    }
完整代码
@Controller
@RequestMapping("/example")
public class ExampleController {

    @RequestMapping({"/index", "/", "inde"})
    public String index() {
        return "ok";
    }

    @RequestMapping(value = "/method", method = RequestMethod.POST)
    public String testMethod() {
        return "ok";
    }

    @RequestMapping(value = "/header", headers = "header1=value1")
    public String testHeaders() {
        return "ok";
    }

    @RequestMapping(value = "/accept", produces = {"application/json", "application/xml"}, consumes = "text/html")
    public String testAccept() {
        return "ok";
    }

    @RequestMapping(value = "/method7/{id}")
    public String method7(
            @PathVariable("id") int id
    ) {
        return "method7 with id=" + id;
    }

    @RequestMapping(value = "/method8/{id:[\\d]+}/{name}")
    public String method8(
            @PathVariable("id") long id,
            @PathVariable("name") String name
    ) {
        return "method8 with id= " + id + " and name=" + name;
    }

    @RequestMapping(value = "/id")
    String getIdByValue(@RequestParam("id") String personId) {
        System.out.println("ID is " + personId);
        return "从URI中获取id的值";
    }
}

最后

可以看到@RequestMapping是非常灵活的,基本上满足所有我们对URI映射处理的需求,这里先不对代码作测试了。

参考

點擊查看更多內容
TA 點贊

若覺得本文不錯,就分享一下吧!

評論

作者其他優質文章

正在加載中
JAVA開發工程師
手記
粉絲
1.1萬
獲贊與收藏
1545

關注作者,訂閱最新文章

閱讀免費教程

  • 推薦
  • 評論
  • 收藏
  • 共同學習,寫下你的評論
感謝您的支持,我會繼續努力的~
掃碼打賞,你說多少就多少
贊賞金額會直接到老師賬戶
支付方式
打開微信掃一掃,即可進行掃碼打賞哦
今天注冊有機會得

100積分直接送

付費專欄免費學

大額優惠券免費領

立即參與 放棄機會
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號

舉報

0/150
提交
取消