![](/img/trans.png)
[英]How to read the @Request Param attribute values that contains ampersand (&) in Spring Boot Rest API
[英]How to read the request param values in spring using HTTPServletRequest
我想使用HttpServletRequest從url讀取requestParams數據
http://localhost:8080/api/type?name=xyz&age=20
我的控制器中的方法不會定義@RequestParam,它只是
@RequestMapping(value = "/**", method = RequestMethod.GET)
public ResponseEntity<String> getResponse(
final HttpServletRequest request) {}
我想閱讀請求只使用params而不是整個url。
首先,為什么要定義:
@RequestMapping(value = "/**", method = RequestMethod.GET)`
?
也許你應該使用:
@RequestMapping(value = "/api/type", method = RequestMethod.GET)
並閱讀參數:
request.getParameter("name");
request.getParameter("age"):
xiang對你的確切問題是正確的:“我想閱讀請求只使用參數”
但是你為什么要這么難。 Spring支持您,因此您不需要自己處理請求對象以執行以下常見任務:
我建議使用
@RequestMapping(value = "/*", method = RequestMethod.GET)
public ResponseEntity<String> getResponse(
@RequestParam("name") String name
@RequestParam("age") int age){
...
}
代替。
@參見Spring參考章節15.3.2.4。 使用@RequestParam將請求參數綁定到方法參數
您可以使用
request.getParameter("parameter name")
這是你想要的?
public java.lang.String getParameter(java.lang.String name)
來自API :
的getParameter
String getParameter(String name)以String形式返回請求參數的值,如果參數不存在,則返回null。 請求參數是隨請求一起發送的額外信息。 對於HTTP servlet,參數包含在查詢字符串或發布的表單數據中。 只有在確定參數只有一個值時才應使用此方法。 如果參數可能具有多個值,請使用getParameterValues(java.lang.String)。
如果將此方法與多值參數一起使用,則返回的值等於getParameterValues返回的數組中的第一個值。
如果參數數據是在請求正文中發送的,例如在HTTP POST請求中發生,則直接通過getInputStream()或getReader()讀取正文可能會干擾此方法的執行。
參數:name - 指定參數名稱的String返回:表示參數的單個值的String參見:getParameterValues(java.lang.String)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.