简体   繁体   English

Spring Boot验证:错误消息中为{1}

[英]Spring Boot Validation : coming as {1} in error message

Trying to archive below things in REST API using spring boot, 尝试使用Spring Boot在REST API中归档以下内容,

  1. Entity classes annotated with 用注释的实体类

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

private String name; 私有字符串名称;

  1. errorMessages.properties looks like below, errorMessages.properties如下所示,

errorMessages.properties errorMessages.properties
Size.foo.name=Name field must be more than {1} characters Size.foo.name =名称字段必须超过{1}个字符

  1. Added below custom class advice to map spring error message to pojo error style. 在自定义类建议下方添加了将spring错误消息映射到pojo错误样式的建议。

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? 有没有一种方法可以打印下面的实际最小大小(4)并发送给用户,或者我是否需要在类中进行更多配置更改?

Name field must be more than 4 characters 名称字段必须超过4个字符

I guess I understand what happens. 我想我知道会发生什么。 You just retrieve the message property. 您只需检索message属性。 The message is not evaluated. 该消息未评估。

You need a MessageSource here and pass FieldError to the message source to get message for. 您在这里需要一个MessageSource并将FieldError传递给消息源以获取消息。

<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 这里获得了代码示例

Change Min to max, min means minimum size is 4. And max means maximum size is about 20 or anything you want. 将“最小值”更改为“最大值”,“最小值”表示最小大小为4。“最大值”表示最大大小约为20或您想要的任何值。

You type @Size(min=4,message="Size.foo.name") 您键入@Size(min = 4,message =“ Size.foo.name”)

private String name; 私有字符串名称;

so you have to enter more than 4 character or equal. 因此您必须输入4个或更多字符。 if you take max than its a highest bound. 如果您接受的最大值大于其上限。 like if max=20 than you can enter maximum 20 character. 例如,如果max = 20,那么您最多可以输入20个字符。 and here min is lower bound so you have to enter minimum 4 character. 并且min是下限,因此您必须输入至少4个字符。

this is the solution 这是解决方案

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM