環境:SpringBoot3.2.5
請求參數通過java.util.Optional包裝
@GetMapping("/optional")public Object optional(Optional<String> name) { return String.format("請求參數: %s", name.orElse("")) ;}
通過Optional接受參數,效果等同于
public Object optional(@RequestParam(required=false) String name){}
與將required設置為false效果一樣(@RequestHeader同樣)
在請求參數中你可以使用WebRequest, NativeWebRequest兩個任意對象來獲取Request,Response,Session等對象。
@GetMapping("/servlet/api")public Object servletApi(WebRequest request, NativeWebRequest webRequest) { String name = request.getParameter("name") ; // 獲取Servlet API HttpServletRequest req = webRequest.getNativeRequest(HttpServletRequest.class) ; HttpServletResponse resp = webRequest.getNativeResponse(HttpServletResponse.class) ; HttpSession session = webRequest.getNativeRequest(HttpSession.class) ; return "servlet api" ;}
當然你可以直接寫你需要的具體對象
public Object servletApi(HttpServletRequest req, HttpServletResponse resp) { // ...}
NativeWebRequest本身提供了很多通用的方法,并且還可以獲取其它對象,使用起來更加方便。
你的請求參數還可以使用java.security.Principal該對象用來獲取當前請求中已經認證過的用戶信息。這尤其在使用Spring Security時非常有用,在Security中的Authentication接口實現了Principal。
@GetMapping("/principal")public Object principal(Principal principal) { return principal ;}
輸出如下:
圖片
你還可以非常方便的獲取當前請求Method及Locale等信息。
@GetMapping("/other")public Object other(HttpMethod method, Locale locale) { return method.name() + ", " + locale.toString() ;}// 輸出GET, zh_CN
除此之外,你還可以獲取時區信息java.util.TimeZone, java.time.ZoneId。
將請求body中的內容以流InputStream形式獲取。
@PostMapping("/inputStream")public Object inputStream(InputStream is) throws Exception { return String.format("讀取到內容: %s", StreamUtils.copyToString(is, StandardCharsets.UTF_8)) ;}
輸出結果:
圖片
通過HttpEntity獲取請求header及body內容信息;
@PostMapping("/httpentity")public Object httpentity(HttpEntity<String> entity) { return Map.of( "headers", entity.getHeaders(), "body", entity.getBody() ) ;}
輸出結果:
圖片
如果你想獲取當前請求的Schema,Host,Port,上下文,那么你可以通過如下參數獲取
@GetMapping("/uri")public Object uri(UriComponentsBuilder builder) { return builder.toUriString() ;}
輸出結果:
http://localhost:9001/api。
只包含了schema://host:port/context
如果你的請求是multipart/form-data,那么你可以通過如下方式獲取部分請求信息
@PostMapping("/requestpart")public Object requestpart(@RequestPart("user") String user) { return user ;}
請求結果:
圖片
你還可以以JSON對象讀取,如下:
public Object requestpart(@RequestPart("user") User user)
注意,對象接受時,你需要設置每part的Content-Type
Content-Type: multipart/mixed--edt7Tfrdusa7r3lNQc79vXuhIIMlatb7PQg7VpContent-Disposition: form-data; name="user"Content-Type: application/json; charset=UTF-8Content-Transfer-Encoding: 8bit{ "age": 20, "name": "張三"}
沒有設置Content-Type將會拋出415錯誤。
指定在發生重定向時使用的屬性(即要附加到查詢字符串中的屬性)以及要在重定向請求期間臨時存儲的屬性。
@PostMapping("/")public String handleFileUpload(RedirectAttributes redirectAttributes) { // 重定向后能夠獲取到這里指定的屬性信息 redirectAttributes.addFlashAttribute("message", "You successfully uploaded file!"); // 重定向 return "redirect:/";}
通過該種方式,重定向后頁面中也能獲取設置的屬性信息。
本文鏈接:http://www.tebozhan.com/showinfo-26-93507-0.htmlSpringBoot中Controller接口參數還可以這樣玩?
聲明:本網頁內容旨在傳播知識,若有侵權等問題請及時與本網聯系,我們將在第一時間刪除處理。郵件:2376512515@qq.com
上一篇: Spring Cloud Gateway中優化Netty線程池,提升系統性能
下一篇: VS Code 常用快捷鍵大全