Master SpringBoot 3.2.5: 9 Essential Techniques to Access Request Data
This tutorial demonstrates nine practical ways to handle SpringBoot 3.2.5 request data, including Optional‑wrapped parameters, direct Servlet API access, retrieving the authenticated Principal, extracting method and locale, reading InputStream, using HttpEntity for headers and body, building request URIs, handling multipart parts, and passing flash attributes on redirects.
1. Optional Parameter Wrapping
Wrap request parameters with java.util.Optional to make them optional, equivalent to using @RequestParam(required=false) .
<code>@GetMapping("/optional")
public Object optional(Optional<String> name) {
return String.format("请求参数: %s", name.orElse(""));
}
</code>2. Convenient Access to Servlet API
Inject WebRequest or NativeWebRequest to obtain HttpServletRequest , HttpServletResponse , HttpSession , etc., or declare the specific objects directly.
<code>@GetMapping("/servlet/api")
public Object servletApi(WebRequest request, NativeWebRequest webRequest) {
String name = request.getParameter("name");
HttpServletRequest req = webRequest.getNativeRequest(HttpServletRequest.class);
HttpServletResponse resp = webRequest.getNativeResponse(HttpServletResponse.class);
HttpSession session = webRequest.getNativeRequest(HttpSession.class);
return "servlet api";
}
</code> <code>public Object servletApi(HttpServletRequest req, HttpServletResponse resp) {
// ...
}
</code>3. Retrieve the Current Authenticated User
Inject java.security.Principal (or Authentication ) to get the logged‑in user when Spring Security is used.
<code>@GetMapping("/principal")
public Object principal(Principal principal) {
return principal;
}
</code>4. Access Other Request Information
Inject HttpMethod and Locale to obtain the request method and locale, or use java.util.TimeZone / java.time.ZoneId for time‑zone data.
<code>@GetMapping("/other")
public Object other(HttpMethod method, Locale locale) {
return method.name() + ", " + locale.toString();
}
// Output example: GET, zh_CN
</code>5. Read Request Body as InputStream
Accept an InputStream parameter to read the raw request body.
<code>@PostMapping("/inputStream")
public Object inputStream(InputStream is) throws Exception {
return String.format("读取到内容: %s",
StreamUtils.copyToString(is, StandardCharsets.UTF_8));
}
</code>6. Get Headers and Body via HttpEntity
Use HttpEntity<String> to access request headers and body together.
<code>@PostMapping("/httpentity")
public Object httpentity(HttpEntity<String> entity) {
return Map.of(
"headers", entity.getHeaders(),
"body", entity.getBody()
);
}
</code>7. Build Current Request URI
Inject UriComponentsBuilder to obtain the full request URI (scheme, host, port, context).
<code>@GetMapping("/uri")
public Object uri(UriComponentsBuilder builder) {
return builder.toUriString();
}
</code>http://localhost:9001/api
8. Handle Multipart Request Parts
Use @RequestPart to retrieve individual parts of a multipart/form‑data request, either as simple values or as JSON‑deserialized objects (remember to set the part’s Content‑Type ).
<code>@PostMapping("/requestpart")
public Object requestpart(@RequestPart("user") String user) {
return user;
}
</code> <code>public Object requestpart(@RequestPart("user") User user)
</code>When sending a JSON object, the part must include Content-Type: application/json ; otherwise a 415 error occurs.
9. Pass Flash Attributes on Redirect
Use RedirectAttributes to add flash attributes that survive a redirect.
<code>@PostMapping("/")
public String handleFileUpload(RedirectAttributes redirectAttributes) {
redirectAttributes.addFlashAttribute("message", "You successfully uploaded file!");
return "redirect:/";
}
</code>These attributes can be accessed on the redirected page.
Spring Full-Stack Practical Cases
Full-stack Java development with Vue 2/3 front-end suite; hands-on examples and source code analysis for Spring, Spring Boot 2/3, and Spring Cloud.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.