Master Advanced Spring Boot 3 Techniques: Output Control, Body Parsing, and Security Context
This article presents a collection of over 50 practical Spring Boot 3 examples, demonstrating how to directly write responses with OutputStream, read request bodies via InputStream, use @Value and SpEL for fixed parameters, combine body and headers with HttpEntity, access security data through @CurrentSecurityContext, and handle Map parameters with @ModelAttribute.
Spring Boot 3.2.5 provides many flexible ways to handle request and response data. Below are several practical techniques.
1. Directly control output content
You can define OutputStream or Writer parameters in a controller method and write directly to the response.
<code>@GetMapping("/index")
public void index(OutputStream os, HttpServletResponse response) throws Exception {
response.setContentType("text/plain;charset=utf-8");
os.write("中国🇨🇳".getBytes());
}</code>2. Manually read request body
When you need to parse the request body yourself, use InputStream or Reader parameters.
<code>@PostMapping("/index")
public void index(InputStream is, HttpServletResponse response) throws Exception {
response.setContentType("application/json;charset=utf-8");
StringBuilder sb = new StringBuilder();
String line = null;
InputStreamReader isr = new InputStreamReader(is, StandardCharsets.UTF_8);
BufferedReader br = new BufferedReader(isr);
while ((line = br.readLine()) != null) {
sb.append(line);
}
response.getWriter().println(sb.toString());
}</code>3. Fixed values with @Value
Use @Value to inject configuration values directly into method parameters.
<code>@GetMapping("/index")
public Object index(@Value("${pack.controller.params.version:}") String version) {
return version;
}</code>Configuration file:
<code>pack:
controller:
params:
version: 1.0.1</code>4. Using SpEL expressions
<code>@GetMapping("/spel")
public Object spel(@Value("#{systemProperties['java.home']}") String javaHome) {
return javaHome;
}</code>5. Retrieve body and headers together
Define a parameter of type HttpEntity to access both request body and headers.
<code>@PostMapping("/index")
public Object index(HttpEntity<User> entity) {
System.out.printf("headers: %s%n", entity.getHeaders());
return entity.getBody();
}
public static record User(Long id, String name) {}
</code>6. Access security context data
When Spring Security is enabled, you can inject data from the current security context directly.
<code>@GetMapping("/name")
public Object name(@CurrentSecurityContext(expression = "authentication.name") String name) {
return name;
}</code>Other examples:
<code>@CurrentSecurityContext(expression = "authentication.authorities")
@CurrentSecurityContext SecurityContext context
</code>7. Special Map parameters
You can bind a Map parameter to model attributes using @ModelAttribute .
<code>@ModelAttribute("user")
public User init(User user) {
return user;
}
@GetMapping("/index")
public Object index(Map<String, Object> map) {
// Retrieve the user object populated by @ModelAttribute
return map.get("user");
}
</code>The above examples constitute the complete content of this article. If you find them helpful, please like, share, and bookmark.
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.