Spring請求路徑帶參數(shù)URL使用注解的寫法說明
Spring請求路徑帶參數(shù)URL使用注解的寫法
調(diào)用外部平臺http接口,Post請求,url 為路徑帶有參數(shù)的形式:
http://xxxxxx.com/openApi/auth/getUserAuth?version=v1.0
使用 Retrofit 框架(本文使用的2.6.2版本)發(fā)送Post請求,可以在 @Post 注解中直接帶上參數(shù),如下:
@POST("auth/getUserAuth?version=v1.0")
Call<McgjResponse<UserAuthResponseDTO>> getUserAuth(@Body UserAuthRequest userAuthRequest);
因為初次使用 Retrofit 框架,所以自己啟動Spring服務(wù)模擬外部平臺接口,發(fā)現(xiàn)之前一直都在@PostMapping中定義路徑,還沒怎么寫過帶參數(shù)的,導(dǎo)致寫錯了,報 404錯誤,記錄一下下。先說正確寫法:
正確寫法:
@PostMapping(value ="/authorize/addRecord",params = "version=v1.0")
public McgjResponse<UserAuthResponseDTO> test(){
其實@RequestMapping、@GetMapping、@PostMapping 三個注解都可以指定請求Header、請求path、以及請求params。
@RequestMapping("/foo") 等價于 @RequestMapping(path="/foo")
/**
* The primary mapping expressed by this annotation.
* <p>In a Servlet environment this is an alias for {@link #path}.
* For example {@code @RequestMapping("/foo")} is equivalent to
* {@code @RequestMapping(path="/foo")}.
* <p>In a Portlet environment this is the mapped portlet modes
* (i.e. "EDIT", "VIEW", "HELP" or any custom modes).
* <p><b>Supported at the type level as well as at the method level!</b>
* When used at the type level, all method-level mappings inherit
* this primary mapping, narrowing it for a specific handler method.
*/
@AliasFor("path")
String[] value() default {};
所以平常在括號中直接寫,只是指定了 path。如果錯誤地把參數(shù)寫到請求 path 中,則會報 HTTP 404 錯誤,如下錯誤寫法:
錯誤寫法:
//錯誤寫法
@PostMapping(value ="/auth/getUserAuth?version=v1.0")
public McgjResponse<UserAuthResponseDTO> test(){
小結(jié):
這三個注解平時用的是如此之多,卻如此不熟悉,實在不應(yīng)該!
Spring注解@RequestMapping請求路徑映射問題
@RequestMapping請求路徑映射,如果標(biāo)注在某個controller的類級別上,則表明訪問此類路徑下的方法都要加上其配置的路徑;最常用是標(biāo)注在方法上,表明哪個具體的方法來接受處理某次請求。
以下兩種方式都可以從url中傳參數(shù),但是第二種方式的適用性更高一些,當(dāng)參數(shù)中包含中文的時候,如果用第一種方式傳參數(shù),經(jīng)常會出現(xiàn)參數(shù)還沒到controller就已經(jīng)經(jīng)過編碼了(例如:經(jīng)過utf-8編碼后,原本要傳的參數(shù)就會以%+ab...cd這樣的方式出現(xiàn)),然后controller接受到這樣的請求后,根本無法解析該請求應(yīng)該走那個業(yè)務(wù)方法。
然后就會出現(xiàn)常見的404問題。。。
package com.test.jeofey.web;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
@RequestMapping("/path")
public class TestController {
// 第一種傳參數(shù)的方式 訪問地址例如:http:域名/path/method1/keyWord.html
@RequestMapping("method1/{keyWord}")
public String getZhiShiDetailData(@PathVariable("keyWord") String keyWord,
HttpServletRequest request, HttpServletResponse response){
System.out.println(keyWord);
return "v1/detail";
}
// 第二種傳參數(shù)的方式 訪問地址例如:http:域名/path/method2.html?key=keyWord
@RequestMapping("method2")
public String getCommonData(HttpServletRequest request,
HttpServletResponse response){
String keyWord= request.getParameter("key");
System.out.println(keyWord);
return "v1/common";
}
}
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Spring?boot框架JWT實現(xiàn)用戶賬戶密碼登錄驗證流程
這篇文章主要介紹了Springboot框架JWT實現(xiàn)用戶賬戶密碼登錄驗證,本文通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-06-06
例舉fastJson和jackson轉(zhuǎn)json的區(qū)別
今天小編就為大家分享一篇關(guān)于例舉fastJson和jackson轉(zhuǎn)json的區(qū)別,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2018-12-12
MyBatis-Plus updateById不更新null值的方法解決
用Mybatis-Plus的updateById()來更新數(shù)據(jù)時,無法將字段設(shè)置為null值,更新后數(shù)據(jù)還是原來的值,本文就來詳細(xì)的介紹一下解決方法,具有一定的參考價值,感興趣的可以了解一下2023-08-08
淺談Java高并發(fā)解決方案以及高負(fù)載優(yōu)化方法
這篇文章主要介紹了淺談Java高并發(fā)解決方案以及高負(fù)載優(yōu)化方法,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-08-08
三行Java代碼實現(xiàn)計算多邊形的幾何中心點(diǎn)
因為工作需要計算采煤機(jī)工作面的中心點(diǎn),如果套用數(shù)學(xué)的計算公式,用java去實現(xiàn),太麻煩了。本文將利用java幾何計算的工具包,幾行代碼就能求出多變形的中心,簡直yyds!還不快跟隨小編一起學(xué)起來2022-10-10
Java設(shè)置Access-Control-Allow-Origin允許多域名訪問的實現(xiàn)方法
這篇文章主要介紹了Java設(shè)置Access-Control-Allow-Origin允許多域名訪問的實現(xiàn)方法,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下2018-10-10

