SpringBoot入門0x002 URL 對映

2021-09-13 13:03:26 字數 4322 閱讀 5744

將某個http請求對映到某個方法上

這個註解可以加在某個controller或者某個方法上,如果加在controller上,則這個controller中的所有路由對映都將會加上這個字首(下面會有栗子),如果加在方法上,則沒有字首(下面也有栗子)。

首先編寫controllercontroller頭部的@restcontroller將這個控制器標註為rest控制器:

package com.ly***x.rest.controller;

import org.springframework.web.bind.annotation.restcontroller;

@restcontroller

public class hellocontroller

新增方法,並新增url匹配:

public object hello()

說明:上面新增了乙個方法,名為hello,沒有給定任何的屬性,則預設匹配所有url,方法為get,所以我們可以直接使用get方式來訪問該路由

$ curl 127.0.0.1:8080

hello

$ curl 127.0.0.1:8080/user

hello

$ curl 127.0.0.1:8080/user/1

hello

精確匹配

public object hello2()

說明:上面將value設定為hello2,所以訪問hello2將會執行hello2方法

$ curl 127.0.0.1:8080/hello2

hello2

字元模糊匹配

public object hello3()

說明:上面將value設定為/hello3/*,*為匹配所有字元,也就是訪問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

說明:上面將value設定為/hello4/?,?為匹配單個字元,匹配hello4/1,但是不會匹配hello4/11

$ curl 127.0.0.1:8080/hello4/1

hello4

$ curl 127.0.0.1:8080/hello4/12

全路徑模糊匹配

public object hello5()

說明:上面將value設定為/hello5/**,**為匹配所有路徑,所以hello5下面的所有路由都將匹配這個方法

$ curl 127.0.0.1:8080/hello5

hello5

$ curl 127.0.0.1:8080/hello5/user/1

hello5

多個路徑匹配

public object hello6()

$ curl 127.0.0.1:8080/hello6

hello6

$ curl 127.0.0.1:8080/hello6/1

hello6f

讀取配置

// com.ly***x.rest.controller.hellocontroller

public object hello7()

$ curl 127.0.0.1:8080/hello7

hello7

匹配請求中的method,寫在requestmethod中的列舉值:

package com.ly***x.rest.controller;

import org.springframework.web.bind.annotation.requestmethod;

import org.springframework.web.bind.annotation.restcontroller;

@restcontroller

public class methodcontroller

public object head()

public object post()

public object put()

public object patch()

public object delete()

public object options()

public object 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...

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

package com.ly***x.rest.controller;

import org.springframework.web.bind.annotation.restcontroller;

@restcontroller

public class paramscontroller

}

$ curl 127.0.0.1:8080/params?userid=1

params

@restcontroller

public class headercontroller

}

$ curl 127.0.0.1:8080/headers

$ curl 127.0.0.1:8080/headers -h "userid:1"

headers

@restcontroller

public class consumescontroller

}

consumes

$ curl 127.0.0.1:8080/consumes -h "content-type:none"

@restcontroller

public class producescontroller

}

$ curl 127.0.0.1:8080/produces 

produces

$ curl 127.0.0.1:8080/produces -h "accept:text"

// 空

資料結構複習 0x0 基礎

邏輯結構與物理結構 邏輯結構 集合結構 線性結構 樹形結構 圖形結構 物理結構 順序儲存結構 鏈式儲存結構 資料型別 一組性質相同的值的集合及定義在此集合上的一些操作的總稱。抽象資料型別 adt,abstract data type 是指乙個數學模型及定義在該模型上的一組操作。演算法 解決特定問題求...

Linux下接收串列埠資料0x0d變0X0a問題

許多流行的linux串列埠程式設計的版本中都沒對c iflag termios成員變數 這個變數進行有效的設定,這樣傳送ascii碼時沒什麼問題,但傳送二進位制資料時遇到0x0d,0x11和0x13卻會被丟掉。不用說也知道,這幾個肯定是特殊字元,被用作特殊控制了。關掉icrnl和ixon選項即可解決...

2019網路工作室暑假留校學習筆記0x01

目錄結構 cj 倉庫名稱 姓名首字母 0709 日期 readme.md 筆記 code 長段 短的可以直接寫到markdown裡 img 資源 0710 readme.md code img 0711 readme.md code img學習linux基本操作 推薦ubuntu 命令基本格式 cm...