File tree Expand file tree Collapse file tree 5 files changed +113
-0
lines changed
spring-boot-modules/spring-boot-custom-validation/src/main/java/com/baeldung/validation/custommessage Expand file tree Collapse file tree 5 files changed +113
-0
lines changed Original file line number Diff line number Diff line change 1+ package com .baeldung .validation .custommessage ;
2+
3+ import org .springframework .context .MessageSource ;
4+ import org .springframework .context .annotation .Bean ;
5+ import org .springframework .context .annotation .Configuration ;
6+ import org .springframework .context .support .ResourceBundleMessageSource ;
7+
8+ @ Configuration
9+ public class AppConfig {
10+
11+ @ Bean
12+ public MessageSource messageSource () {
13+ ResourceBundleMessageSource source = new ResourceBundleMessageSource ();
14+ source .setBasename ("ValidationMessages" );
15+ source .setDefaultEncoding ("UTF-8" );
16+ source .setUseCodeAsDefaultMessage (true );
17+ return source ;
18+ }
19+ }
Original file line number Diff line number Diff line change 1+ package com .baeldung .validation .custommessage ;
2+ import ch .qos .logback .core .util .StatusPrinter ;
3+ import org .slf4j .LoggerFactory ;
4+ import ch .qos .logback .classic .LoggerContext ;
5+
6+ public class LogbackPing {
7+ public static void ping () {
8+ LoggerContext context = (LoggerContext ) LoggerFactory .getILoggerFactory ();
9+ StatusPrinter .print (context );
10+ }
11+ }
Original file line number Diff line number Diff line change 1+ package com .baeldung .validation .custommessage ;
2+
3+ import org .springframework .boot .SpringApplication ;
4+ import org .springframework .boot .autoconfigure .SpringBootApplication ;
5+
6+ @ SpringBootApplication
7+ public class SpringBootCustomValidationApplication {
8+ public static void main (String [] args ) {
9+ LogbackPing .ping (); // Ensures logback-core is included
10+ SpringApplication .run (SpringBootCustomValidationApplication .class , args );
11+ }
12+ }
Original file line number Diff line number Diff line change 1+ // Minor change to trigger PR comparison
2+ package com .baeldung .validation .custommessage ;
3+
4+ import org .springframework .http .ResponseEntity ;
5+ import org .springframework .validation .BindingResult ;
6+ import org .springframework .validation .FieldError ;
7+ import org .springframework .web .bind .annotation .*;
8+
9+ import jakarta .validation .Valid ;
10+ import java .util .List ;
11+ import java .util .stream .Collectors ;
12+
13+ @ RestController
14+ @ RequestMapping ("/register" )
15+ public class UserController {
16+
17+ @ PostMapping
18+ public ResponseEntity <?> registerUser (@ RequestBody @ Valid UserDTO userDTO , BindingResult result ) {
19+ if (result .hasErrors ()) {
20+ List <String > errors = result .getFieldErrors ()
21+ .stream ()
22+ .map (FieldError ::getDefaultMessage )
23+ .collect (Collectors .toList ());
24+ return ResponseEntity .badRequest ().body (errors );
25+ }
26+
27+ return ResponseEntity .ok ("User registered successfully" );
28+ }
29+ }
Original file line number Diff line number Diff line change 1+ package com .baeldung .validation .custommessage ;
2+
3+ import jakarta .validation .constraints .Email ;
4+ import jakarta .validation .constraints .Min ;
5+ import jakarta .validation .constraints .NotBlank ;
6+
7+ public class UserDTO {
8+
9+ @ NotBlank (message = "{user.name.notblank}" )
10+ private String name ;
11+
12+ @ Email (message = "{user.email.invalid}" )
13+ private String email ;
14+
15+ @ Min (value = 18 , message = "{user.age.min}" )
16+ private int age ;
17+
18+ // Getters and setters
19+ public String getName () {
20+ return name ;
21+ }
22+
23+ public void setName (String name ) {
24+ this .name = name ;
25+ }
26+
27+ public String getEmail () {
28+ return email ;
29+ }
30+
31+ public void setEmail (String email ) {
32+ this .email = email ;
33+ }
34+
35+ public int getAge () {
36+ return age ;
37+ }
38+
39+ public void setAge (int age ) {
40+ this .age = age ;
41+ }
42+ }
You can’t perform that action at this time.
0 commit comments