簡體   English   中英

設置值時,將RequestParam設置為默認值始終為null

[英]Setting RequestParam with default value is always null when value is set

我正在創建一個rest方法,該方法可以使用RequestParam批注處理可選參數(在這種情況下為gender)。 但是性別值始終為空。 (春季版本4.0.0)雖然正確考慮了第二個參數。

樣本: http:// localhost:8080 / bll-0.1 / qry / gender / female / age / 40

@RequestMapping(value = {"/age/{age}", "/gender/{gender}/age/{age}"}, method = RequestMethod.GET)
public @ResponseBody String getByAge(@RequestParam(value="gender", defaultValue="unknown") String gender, @PathVariable("age") int age) throws Exception {...}

提前致謝。 潘卡

就像Juned在評論中說的那樣,也將@PathVariable用於性別。

public @ResponseBody String getByAge(@PathVariable("gender") String gender, @PathVariable("age") int age) throws Exception {...}

如果您想要可選參數,並且如果您有請求參數,這不是問題,那么您的網址應類似於http://localhost:8080/bll-0.1/qry/age/40?gender=female ,您可以使用@RequestParam

public @ResponseBody String getByAge(@RequestParam(value="gender", defaultValue="unknown") String gender, @PathVariable("age") int age) throws Exception {...}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM