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.
@GetMapping("/index")
public void index(OutputStream os, HttpServletResponse response) throws Exception {
response.setContentType("text/plain;charset=utf-8");
os.write("中国🇨🇳".getBytes());
}2. Manually read request body
When you need to parse the request body yourself, use InputStream or Reader parameters.
@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());
}3. Fixed values with @Value
Use @Value to inject configuration values directly into method parameters.
@GetMapping("/index")
public Object index(@Value("${pack.controller.params.version:}") String version) {
return version;
}Configuration file:
pack:
controller:
params:
version: 1.0.14. Using SpEL expressions
@GetMapping("/spel")
public Object spel(@Value("#{systemProperties['java.home']}") String javaHome) {
return javaHome;
}5. Retrieve body and headers together
Define a parameter of type HttpEntity to access both request body and headers.
@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) {}6. Access security context data
When Spring Security is enabled, you can inject data from the current security context directly.
@GetMapping("/name")
public Object name(@CurrentSecurityContext(expression = "authentication.name") String name) {
return name;
}Other examples:
@CurrentSecurityContext(expression = "authentication.authorities")
@CurrentSecurityContext SecurityContext context7. Special Map parameters
You can bind a Map parameter to model attributes using @ModelAttribute.
@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");
}The above examples constitute the complete content of this article. If you find them helpful, please like, share, and bookmark.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
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.
