成人国产在线小视频_日韩寡妇人妻调教在线播放_色成人www永久在线观看_2018国产精品久久_亚洲欧美高清在线30p_亚洲少妇综合一区_黄色在线播放国产_亚洲另类技巧小说校园_国产主播xx日韩_a级毛片在线免费

資訊專欄INFORMATION COLUMN

SpringBoot入門0x002:URL 映射

jlanglang / 2668人閱讀

摘要:概述將某個(gè)請(qǐng)求映射到某個(gè)方法上這個(gè)注解可以加在某個(gè)或者某個(gè)方法上,如果加在上,則這個(gè)中的所有路由映射都將會(huì)加上這個(gè)前綴下面會(huì)有栗子,如果加在方法上,則沒(méi)有前綴下面也有栗子。

0x000 概述

將某個(gè)http請(qǐng)求映射到某個(gè)方法上

0x001 @RequestMapping

這個(gè)注解可以加在某個(gè)Controller或者某個(gè)方法上,如果加在Controller上,則這個(gè)Controller中的所有路由映射都將會(huì)加上這個(gè)前綴(下面會(huì)有栗子),如果加在方法上,則沒(méi)有前綴(下面也有栗子)。

@RequestMapping有以下屬性

value: 請(qǐng)求的URL的路徑

path: 和value一樣

method: 請(qǐng)求的方法

consumes: 允許的媒體類型,也就是Content-Type

produces: 相應(yīng)的媒體類型,也就是Accept

params: 請(qǐng)求參數(shù)

headers: 請(qǐng)求頭部

0x002 路由匹配

首先編寫ControllerController頭部的@RestController將這個(gè)控制器標(biāo)注為Rest控制器:

package com.lyxxxx.rest.controller;

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

@RestController
public class HelloController {

}

添加方法,并添加URL匹配:

    @RequestMapping()
    public Object hello() {
        return "hello";
    }
    

說(shuō)明:上面添加了一個(gè)方法,名為hello,沒(méi)有給定任何的屬性,則默認(rèn)匹配所有URL,方法為GET,所以我們可以直接使用GET方式來(lái)訪問(wèn)該路由

$ curl 127.0.0.1:8080
hello
$ curl 127.0.0.1:8080/user
hello
$ curl 127.0.0.1:8080/user/1
hello

精確匹配

@RequestMapping(value = "/hello2")
public Object hello2() {
    return "hello2";
}

說(shuō)明:上面將value設(shè)置為hello2,所以訪問(wèn)hello2將會(huì)執(zhí)行hello2方法

$ curl 127.0.0.1:8080/hello2
hello2

字符模糊匹配

@RequestMapping(value = "/hello3/*")
public Object hello3() {
    return "hello3";
}

說(shuō)明:上面將value設(shè)置為/hello3/*,*為匹配所有字符,也就是訪問(wèn)hello3下的所有URL都將匹配該防范

$ curl 127.0.0.1:8080/hello3/user
hello3
 curl 127.0.0.1:8080/hello3/1
hello3

單字符模糊匹配

$ curl 127.0.0.1:8080/hello4/1
hello4

說(shuō)明:上面將value設(shè)置為/hello4/?,?為匹配單個(gè)字符,匹配hello4/1,但是不會(huì)匹配hello4/11

$ curl 127.0.0.1:8080/hello4/1
hello4
$ curl 127.0.0.1:8080/hello4/12
{"timestamp":"2018-07-25T05:29:39.105+0000","status":404,"error":"Not Found","message":"No message available","path":"/hello4/12"}

全路徑模糊匹配

@RequestMapping(value = "/hello5/**")
public Object hello5() {
    return "hello5";
}

說(shuō)明:上面將value設(shè)置為/hello5/**,**為匹配所有路徑,所以hello5下面的所有路由都將匹配這個(gè)方法

$ curl 127.0.0.1:8080/hello5
hello5
$ curl 127.0.0.1:8080/hello5/user/1
hello5

多個(gè)路徑匹配

@RequestMapping(value = {"/hello6", "/hello6/1"})
public Object hello6() {
    return "hello6";
}
$ curl 127.0.0.1:8080/hello6
hello6
$ curl 127.0.0.1:8080/hello6/1
hello6F

讀取配置

// resources/application.properties

app.name=hello7

// com.lyxxxx.rest.controller.HelloController

@RequestMapping(value = "${app.name}")
public Object hello7() {
    return "hello7";
}
$ curl 127.0.0.1:8080/hello7
hello7

0x003 方法匹配

匹配請(qǐng)求中的method,寫在RequestMethod中的枚舉值:

GET

HEAD

POST

PUT

PATCH

DELETE

OPTIONS

TRACE

package com.lyxxxx.rest.controller;

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

@RestController
public class MethodController {
    @RequestMapping(path = "method/get", method = RequestMethod.GET)
    public Object get() {
        return "get";
    }

    @RequestMapping(path = "method/head", method = RequestMethod.HEAD)
    public Object head() {
        return "head";
    }

    @RequestMapping(path = "method/post", method = RequestMethod.POST)
    public Object post() {
        return "post";
    }

    @RequestMapping(path = "method/put", method = RequestMethod.PUT)
    public Object put() {
        return "put";
    }

    @RequestMapping(path = "method/patch", method = RequestMethod.PATCH)
    public Object patch() {
        return "patch";
    }

    @RequestMapping(path = "method/delete", method = RequestMethod.DELETE)
    public Object delete() {
        return "delete";
    }

    @RequestMapping(path = "method/options", method = RequestMethod.OPTIONS)
    public Object options() {
        return "options";
    }

    @RequestMapping(path = "method/trace", method = RequestMethod.TRACE)
    public Object trace() {
        return "trace";
    }


}
$ curl -X GET  127.0.0.1:8080/method/get
get
$ curl -X POST 127.0.0.1:8080/method/post
post
$ curl -X DELETE 127.0.0.1:8080/method/delete
delete
$ curl -X PUT 127.0.0.1:8080/method/put
put
...
0x003 params 匹配

除了可以匹配URLmethod之外,還可以匹配params

package com.lyxxxx.rest.controller;

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

@RestController
public class ParamsController {
    @RequestMapping(path = "/params", params = "userId=1")
    public Object params() {
        return "params";
    }
}
$ curl 127.0.0.1:8080/params?userId=1
params
0x004 header 匹配
@RestController
public class HeaderController {
    @RequestMapping(path = "/headers", headers = "userId=1")
    public Object headers() {
        return "headers";
    }
}
$ curl 127.0.0.1:8080/headers
{"timestamp":"2018-08-01T06:11:40.037+0000","status":404,"error":"Not Found","message":"No message available","path":"/headers"}
$ curl 127.0.0.1:8080/headers -H "userId:1"
headers
0x005 consumes 匹配
@RestController
public class ConsumesController {

    @RequestMapping(value = "consumes",consumes = "application/json")
    public Object json() {
        return "consumes";
    }
}
$ curl 127.0.0.1:8080/consumes -H "Content-Type:application/json"
consumes
$ curl 127.0.0.1:8080/consumes -H "Content-Type:none"
{"timestamp":"2018-08-01T06:15:10.919+0000","status":415,"error":"Unsupported Media Type","message":"Invalid mime type "none": does not contain "/"","path":"/consumes"}
0x006 produces 匹配
@RestController
public class ProducesController {
    @RequestMapping(value = "/produces", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public Object produces() {
        return "produces";
    }
}
$ curl 127.0.0.1:8080/produces 
produces
$ curl 127.0.0.1:8080/produces -H "Accept:text"
// 空
0x007 說(shuō)明

以上參考數(shù)據(jù):《Spring Boot2精髓 從構(gòu)建小系統(tǒng)到架構(gòu)分部署大系統(tǒng)》

文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請(qǐng)注明本文地址:http://systransis.cn/yun/76491.html

相關(guān)文章

  • SpringBoot入門0x001:idea創(chuàng)建 SpringBoot 項(xiàng)目并運(yùn)行

    摘要:創(chuàng)建項(xiàng)目創(chuàng)建一個(gè)項(xiàng)目選擇填寫,這兩個(gè)可以組合成,一般是項(xiàng)目域名倒置,是項(xiàng)目名,然后由這兩個(gè)組合成主包名。等待初次導(dǎo)包結(jié)束查看創(chuàng)建一個(gè)最簡(jiǎn)單的服務(wù)并測(cè)試添加一個(gè)打開(kāi),并點(diǎn)擊運(yùn)行使用自帶服務(wù)自帶測(cè)試,或者其他任意工具,看到返回就成功了 0x001 創(chuàng)建項(xiàng)目 創(chuàng)建一個(gè)項(xiàng)目showImg(https://segmentfault.com/img/bVbeaIU?w=777&h=482); ...

    Miracle_lihb 評(píng)論0 收藏0
  • SpringBoot整合MyBatis并使用Redis作為緩存組件的Demo

    摘要:本博客貓叔的博客,轉(zhuǎn)載請(qǐng)申明出處本系列教程為項(xiàng)目附帶。歷史文章如何在安裝最新版安裝安裝最新版的入門教程的入門教程安裝教程安裝流程安裝如果不清楚是什么,請(qǐng)查看的文檔和簡(jiǎn)介,這里給出的安裝過(guò)程安裝虛擬機(jī)如果有遠(yuǎn)程服務(wù)器的,請(qǐng)略過(guò)此步驟本文推 本博客 貓叔的博客,轉(zhuǎn)載請(qǐng)申明出處本系列教程為HMStrange項(xiàng)目附帶。 Auth:HMStrange-TIAN e-mail:zhangqihao...

    mo0n1andin 評(píng)論0 收藏0
  • 慕課網(wǎng)_《2小時(shí)學(xué)會(huì)SpringBoot》學(xué)習(xí)總結(jié)

    摘要:小時(shí)學(xué)會(huì)學(xué)習(xí)總結(jié)時(shí)間年月日星期六說(shuō)明本文部分內(nèi)容均來(lái)自慕課網(wǎng)。慕課網(wǎng)教學(xué)示例源碼暫無(wú)。數(shù)據(jù)庫(kù)操作下第六章事務(wù)管理事務(wù)管理只有查詢的時(shí)候不加事務(wù),其它任何操作都要加事務(wù)。第七章課程回顧課程回顧總結(jié)介紹安裝配置的使用數(shù)據(jù)庫(kù)操作 《2小時(shí)學(xué)會(huì)SpringBoot》學(xué)習(xí)總結(jié) 時(shí)間:2017年2月18日星期六說(shuō)明:本文部分內(nèi)容均來(lái)自慕課網(wǎng)。@慕課網(wǎng):http://www.imooc.com教學(xué)示...

    aisuhua 評(píng)論0 收藏0

發(fā)表評(píng)論

0條評(píng)論

最新活動(dòng)
閱讀需要支付1元查看
<