RequestMapping 6個基本用法小結

2021-09-02 12:08:09 字數 1325 閱讀 2632

1)最基本的,方法級別上應用,例如:

public string ******pattern()

則訪問http://localhost/***x/departments的時候,會呼叫 ******pattern方法了

2)引數繫結

public string finddepatment(

@requestparam("departmentid") string departmentid)

形如這樣的訪問形式:

/departments?departmentid=23就可以觸發訪問finddepatment方法了

3 rest風格的引數

public string finddepatment(@pathvariable string departmentid)

/departments/23,其中用(@pathvariable接收rest風格的引數

4 rest風格的引數繫結形式之2

public string finddepatmentalternative(

@pathvariable("departmentid") string somedepartmentid)

先看例子,這個有點象之前的

這個有點不同,就是接收形如/departments/23的url訪問,把23作為傳入的departmetnid,,但是在實際的方法finddepatmentalternative中,使用

@pathvariable("departmentid") string somedepartmentid,將其繫結為

somedepartmentid,所以這裡somedepartmentid為23

5 url中同時繫結多個id

public string findemployee(

@pathvariable string departmentid,

@pathvariable string employeeid)

6 支援正規表示式

public string regularexpression_r(

@pathvariable string textualpart,

@pathvariable string numericpart)

比如如下的url:/sometext.123,則輸出:

textual part: sometext, numeric part: 123.

RequestMapping註解用法

1.標註在方法上 作為請求處理方法在程式接收到對應的url請求時被呼叫 package com.itheima.controller import org.springframework.stereotype.controller controller public class firstcontr...

RequestMapping使用須知

即 也可以定義方法上 一般來說,類級別的註解負責將乙個特定 或符合某種模式 的請求 路徑對映到乙個控制器上,同時通過方法級別的註解來細化對映,即 根據特定的http請求方法 get post 方法等 http請求中是 否攜帶特定引數等條件,將請求對映到匹配的方法上 具體配置方法 1 對映單個url ...

RequestMapping註解詳解

method 指定請求的method型別,get post put delete等 produces 指定返回的內容型別,僅當request請求頭中的 accept 型別中包含該指定型別才返回 params 指定request中必須包含某些引數值是,才讓該方法處理。headers 指定request...