1

I have a bean defined as below:

public class PostDTO {
    @Autowired
    private Messages messages;  

    private String id;

    @Size(min = Constant.POST_TITLE_MIN_LEN, max = Constant.POST_TITLE_MAX_LEN, message = messages.get("post.err.title"))
    private String title;
}

I want the message value to be dynamic based on locale. Above code shows The value for annotation attribute Size.message must be a constant expression error.

Manish Kumar
  • 10,214
  • 25
  • 77
  • 147

1 Answers1

0

I think you could use SpEL (Spring Expression Language)


public class PostDTO {

    private String id;

    @Size(min = Constant.POST_TITLE_MIN_LEN, max = Constant.POST_TITLE_MAX_LEN, message = "#{'${post.err.title}'}")
    private String title;
}

and define the variable in spring properties file like: post.err.title = “Post Error”

More details for SpEL: https://docs.spring.io/spring/docs/4.3.10.RELEASE/spring-framework-reference/html/expressions.html

Hugo Zhang
  • 51
  • 3