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.

Spring Full-Stack Practical Cases
Spring Full-Stack Practical Cases
Spring Full-Stack Practical Cases
Master SpringBoot 3.2.5: 9 Essential Techniques to Access Request Data

1. Optional Parameter Wrapping

Wrap request parameters with java.util.Optional to make them optional, equivalent to using @RequestParam(required=false).

@GetMapping("/optional")
public Object optional(Optional<String> name) {
    return String.format("请求参数: %s", name.orElse(""));
}

2. Convenient Access to Servlet API

Inject WebRequest or NativeWebRequest to obtain HttpServletRequest, HttpServletResponse, HttpSession, etc., or declare the specific objects directly.

@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";
}
public Object servletApi(HttpServletRequest req, HttpServletResponse resp) {
    // ...
}

3. Retrieve the Current Authenticated User

Inject java.security.Principal (or Authentication) to get the logged‑in user when Spring Security is used.

@GetMapping("/principal")
public Object principal(Principal principal) {
    return principal;
}
Principal output
Principal output

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.

@GetMapping("/other")
public Object other(HttpMethod method, Locale locale) {
    return method.name() + ", " + locale.toString();
}
// Output example: GET, zh_CN

5. Read Request Body as InputStream

Accept an InputStream parameter to read the raw request body.

@PostMapping("/inputStream")
public Object inputStream(InputStream is) throws Exception {
    return String.format("读取到内容: %s",
        StreamUtils.copyToString(is, StandardCharsets.UTF_8));
}
InputStream output
InputStream output

6. Get Headers and Body via HttpEntity

Use HttpEntity<String> to access request headers and body together.

@PostMapping("/httpentity")
public Object httpentity(HttpEntity<String> entity) {
    return Map.of(
        "headers", entity.getHeaders(),
        "body", entity.getBody()
    );
}
HttpEntity output
HttpEntity output

7. Build Current Request URI

Inject UriComponentsBuilder to obtain the full request URI (scheme, host, port, context).

@GetMapping("/uri")
public Object uri(UriComponentsBuilder builder) {
    return builder.toUriString();
}
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).

@PostMapping("/requestpart")
public Object requestpart(@RequestPart("user") String user) {
    return user;
}
RequestPart output
RequestPart output
public Object requestpart(@RequestPart("user") User user)

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.

@PostMapping("/")
public String handleFileUpload(RedirectAttributes redirectAttributes) {
    redirectAttributes.addFlashAttribute("message", "You successfully uploaded file!");
    return "redirect:/";
}

These attributes can be accessed on the redirected page.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

BackendJavamultipartspring-securityrequest-handling
Spring Full-Stack Practical Cases
Written by

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.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.