How do you implement data validation in Spring MVC?
Annotate your form model, add @Valid with a BindingResult in the controller, and show field errors in Thymeleaf. Includes custom validators and central handling.
2,887 posts about Java: questions with answers, tutorials and articles, newest first.
Annotate your form model, add @Valid with a BindingResult in the controller, and show field errors in Thymeleaf. Includes custom validators and central handling.
@ModelAttribute binds form and query parameters to a Java object and exposes it to the view in Spring MVC. See usage on parameters and methods.
Return objects as JSON in Spring MVC with @RestController, @ResponseBody and ResponseEntity, and control status codes, headers and serialisation.
@ResponseStatus changes the default 200 status of a Spring handler method. Learn 201 and 204 usage, exception handlers, pitfalls and testing.
Compare the four REST API versioning strategies in Spring and see working code for each, including the native version support added in Spring Framework 7.
@RequestMapping routes web requests to Spring controller methods by path, HTTP method, parameters, headers and media type. Learn its attributes and best practices.
Set up multiple message bundles in Spring Boot, resolve the user locale, switch languages with a request parameter and localise validation messages.
MessageSource resolves translated messages by code and locale in Spring. See how to configure bundles, pass arguments and avoid encoding problems.
Learn how Spring's MessageSource resolves translated text by code and locale, in web requests, services and background jobs.
@Controller returns view names for server-rendered pages, while @RestController writes return values straight to the response body, usually as JSON.
SecurityContextHolder is where Spring Security keeps the current user's Authentication. Learn how to read it, set it manually, and avoid common pitfalls.
Write and register a custom AuthenticationProvider in Spring Security 6, with the SecurityFilterChain setup, UserDetailsService and tests.
@PreAuthorize secures a method with a Spring Security expression checked before it runs. Learn @EnableMethodSecurity, roles, SpEL and common pitfalls.
Method-level security protects service methods with annotations such as @PreAuthorize. See how to enable it with @EnableMethodSecurity and avoid common pitfalls.
The SecurityFilterChain is the ordered set of servlet filters Spring Security applies to each request. Learn to configure, extend and combine chains.
Sign JWTs with RSA keys, validate them with Spring Security 6, protect methods with scopes and add refresh tokens for a production-ready REST API.
UserDetailsService is the lookup hook that loads a user for authentication. Learn how DaoAuthenticationProvider uses it.
@InjectMocks builds the object under test and injects your mocks through constructor, setter or field injection. Learn the order, the JUnit 5 setup and common traps.
Why and how to verify mock interactions in Mockito: call counts, argument matching, call order and unexpected calls, with runnable JUnit 5 tests.
Stubbing tells a Mockito mock what to return or throw. See when/thenReturn, doReturn, matchers, answers and strict stubs with runnable examples.