Spring validation annotation using message with parameters - java

I'm trying to use annotations with a custom message in spring/hibernate validation. What I want to do is support internationalization while being able to pass in a parameter.
For instance, here is the field in the java class:
#NotEmpty (message = "{error.required}")
public String name;
ValidationMessages.properties has this:
error.required = {0} is a required field
So I'm looking for a way to pass in the field name into that message using the message annotation in the java class. Is this even possible?
EDIT: Similar you this question: JSR 303 Bean Validation in Spring

Related

Spring require property when Spring MVC binding

I have a #RestController that has a #RequestMapping method. It takes one parameter that is an instance of a class, EmailTemplate:
public List<Message> sendEmail(EmailTemplate emailTemplate) {
return emailService.sendEmail(emailTemplate);
}
I would like to require that two String properties of emailTemplate not contain null or only whitespace. However, it should not be required in general. In other words, it should be required when passing to sendEmail but not necessarily in other places where the code might instantiate the class. Does Spring provide an annotated way to accomplish this?
Yes. Spring support Bean Validation. And Bean Validation allows setting groups on validation constraints.
So, just add constraints to the two fields with a group like SendEmailGroup.class, and annotate the EmailTemplate argument of sendEmail() with #Validated(SendEmailGroup.class).

How to use a Spring #Value from properties to set an annotation attribute

I'm trying to set an attribute in an annotation, using Spring #Value, but I get Type mismatch: cannot convert from Value to String. Here is what I tried:
#Table(name = "myTable", catalog = #Value("${database.myCatalog}") )
Is it possible? And if yes, how to do it?
I think you are a little bit confused with how Spring uses that annotation.
As far as I know, the only way that annotation can only be set at field or method/constructor parameters.
Also, for Spring to resolve it, the POJO must be a Spring managed bean. That means that it must be defined in the Spring (Web)ApplicationContext implementation to be resolved.
Your question seems like you are annotating a JPA Entity which is not a Spring bean but a Class to be used by the JPA implementation that you are using (e.g. Hibernate).

How do I customise the resource key lookup for spring MVC validation messages

The default behaviour for the resource key look seems to be to prefix the field name with the constraint. I would like to post fix it instead.
public class MyForm {
#NotEmpty
private String name;
}
In my messages.properties:
myForm.name=The label
NotEmpty.myForm.name=Please enter your name
I would like to use:
myForm.name=The label
myForm.name.NotEmpty=Please enter your name
This obviously purely cosmetic, but my OCD can't handle the look of my resource file.
I'm using spring 3.1.2 with hibernate validator 4.3.0.Final. The application it's self is using the #Configuration, #EnableWebMvc and WebMvcConfigurerAdapter style configuration, but I should be able to translate any xml based solution.
A simple idea is to extend DefaultMessageCodesResolver and override the method postProcessMessageCode() which originally is:
protected String postProcessMessageCode(String code) {
return getPrefix() + code;
}
and change it the way you desire. I believe Spring will pickup your bean of type MessageCodesResolver as the default behavior when bootstrapping. Hope this helps.
In Spring 3.1+, now you can configure a message codes resolver through <mvc:annotation-driven />. It seems that support for the Java API will come in 3.2.

JSF 2.0 + Spring 3, retrieving request parameter via annotation

In native JSF 2.0 environment user is able to refer to request parameters with something like
#ManagedProperty("#{param.id}")
private Long id;
However I'm using Spring to manage JSF beans, so #ManagedProperty annotation is ignored in my case. It's still possible to use #{param.id} statement in faces-config.xml, but annotation-based configuration is obviously much more preferable.
Is there any way to resolve such statements with Spring annotations?
You can try to use #Value with Spring Expression Language. There should be request variable available:
#Value("#{request.getParameter('id')}")
private Long id

Is spring Form validation different than JSR bean validation

I have seen that annotation like #NotNull etc are given in JSR Specifications.
Now as i am currently studying Spring MVC and building website in that
so i am confused are they same or different and have they any relation with spring MVC.
because for .eg if i use #NotEmpty then will spring knows that if i leave it empty then it displays in form with error message like we code in validator and its messages
This is my method , i am confused were to add #Valid
public String add(#ModelAttribute("personAttribute") Person person) {
logger.debug("Received request to add new person");
personService.add(person);
// This will resolve to /WEB-INF/jsp/addedpage.jsp
return "hibernate/addedpage";
}
Spring form validation is different, but it also support JSR-303 validation. You can validate a whole model attribute by annotating it with #Valid (as a method parameter)
public String add(#Valid #ModelAttribute("personAttribute") Person person) { .. }
You would need:
a JSR-303 provider on the classpath
<mvc:annotation-driven /> in the spring-mvc xml to enable validation

Categories

Resources