0

Trying to archive below things in REST API using spring boot,

  1. Entity classes annotated with

@Size(min=4,message="Size.foo.name")

private String name;

  1. errorMessages.properties looks like below,

errorMessages.properties
Size.foo.name=Name field must be more than {1} characters

  1. Added below custom class advice to map spring error message to pojo error style.

code

> @ControllerAdvice
@PropertySource("classpath:errorMessages.properties")
public class RestExceptionHandler 
{ 

@Autowired
private Environment environment;

@ExceptionHandler(MethodArgumentNotValidException.class)
@ResponseStatus(HttpStatus.BAD_REQUEST)

public @ResponseBody ErrorDetail handleValidationError(MethodArgumentNotValidException manve, HttpServletRequest request) {
    List<FieldError> fieldErrors = manve.getBindingResult().getFieldErrors();
    for(FieldError fieldError : fieldErrors) {
        System.out.println(environment.getProperty(fieldError.getDefaultMessage())); //This prints Name field must be more than {1} characters
    }
    }

}

is there a way we can print the actual min size (4) as below and send to the user, or should i need to make some more configuration changes in classes?

Name field must be more than 4 characters

Community
  • 1
  • 1
Satscreate
  • 495
  • 12
  • 38

2 Answers2

0

I guess I understand what happens. You just retrieve the message property. The message is not evaluated.

You need a MessageSource here and pass FieldError to the message source to get message for.

<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
    <property name="basenames" value="ValidationMessages"/>
</bean>

Then autowire the source in your controller

@Autowired
private MessageSource messageSource;

And to get your message, do as follows

for (Object object : bindingResult.getAllErrors()) {
    if(object instanceof FieldError) {
        FieldError fieldError = (FieldError) object;

        /**
          * Use null as second parameter if you do not use i18n (internationalization) or Locale to be specified if you need localized one
          */

        String message = messageSource.getMessage(fieldError, null);
    }
}

Got code examples from here

StanislavL
  • 56,971
  • 9
  • 68
  • 98
  • Added messagesource bean definition as mentioned above and applied this line also, messageSource.getMessage(fieldError, null);. But the result was "Name field must be more than {0} {1} characters" . Added {0} {1} also in the property file.. but min size value not getting appended in property message. – Satscreate Dec 12 '17 at 14:50
  • 1
    Try {min} and {max} – StanislavL Dec 12 '17 at 14:59
  • i added as below, Size.foo.name=Name must be more than {0} characters and Entity will be @Size(min=5,message="Size.foo.name"). this case it prints output as : Name must be more than {0} characters; If i specify Size.foo.name=Name must be more than {min} & model as @Size(min=5,message="{Size.foo.name}") then prints output as (Exception):org.springframework.context.NoSuchMessageException: No message found under code 'Description must be more than 5 characters' for locale 'null'. Its searching for the code in properties.... – Satscreate Dec 12 '17 at 18:19
0

Change Min to max, min means minimum size is 4. And max means maximum size is about 20 or anything you want.

You type @Size(min=4,message="Size.foo.name")

private String name;

so you have to enter more than 4 character or equal. if you take max than its a highest bound. like if max=20 than you can enter maximum 20 character. and here min is lower bound so you have to enter minimum 4 character.

this is the solution