我有一個(gè) Spring Boot API,它有提供分頁的端點(diǎn)。@RequestMapping(path = "/most-popular", method = GET)@Overridepublic List<RefinedAlbum> getMostPopularDefault() { return albumService.getMostPopular(0, 25);}@RequestMapping(path = "/most-popular?offset={offset}&limit={limit}", method = GET)@Overridepublic List<RefinedAlbum> getMostPopular(@PathVariable("limit") int limit, @PathVariable("offset") int offset) { inputValidation(limit, offset); return albumService.getMostPopular(limit, offset);}但是當(dāng)我向服務(wù)提出請求時(shí):http://localhost:5250/api/v1/albums/most-popular?offset=100&limit=125第一個(gè)函數(shù)被調(diào)用。我的理解是應(yīng)該先進(jìn)行精確匹配。那不正確嗎?
1 回答

慕無忌1623718
TA貢獻(xiàn)1744條經(jīng)驗(yàn) 獲得超4個(gè)贊
?以下 URL 中的之后的內(nèi)容不能使用@PathVariable注釋綁定:
http://localhost:5250/api/v1/albums/most-popular?offset=100&limit=125
您的路徑只是http://localhost:5250/api/v1/albums/most-popular,之后的內(nèi)容由兩個(gè)請求參數(shù)組成,即。offset和limit。您可以使用@RequestParam注解將請求參數(shù)綁定到控制器中的方法參數(shù):
@RequestMapping(path = "/most-popular", method = GET)
@Override
public List<RefinedAlbum> getMostPopular (@RequestParam("limit") int limit,
@RequestParam("offset") int offset) {
inputValidation(limit, offset);
return albumService.getMostPopular(limit, offset);
}
添加回答
舉報(bào)
0/150
提交
取消