JSR-303/JSR-349 message key in Spring MVC 4 - spring

I have an application (Spring MVC 4 + Hibernate/JPA + MySQL + Maven integration example using annotations), integrating Spring with Hibernate using annotation based configuration.
I have this property
#Pattern(regexp = "^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*#"
+ "[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$",
message = "{general.error.mail}")
private String email;
But when there is an error I see the key in the page instead of the message itself
{general.error.mail}

You need a messages.properties file. Check Spring MVC Form Validation Example with Bean Validation API as a tutorial.

Related

Alternative way for setting default for #Value-annotated field in Spring Boot

I have a Spring Boot application written in Kotlin and I would prefer to set the default of a #Value-annotated field like below:
#Value("\${service-clients.logging.exceptions:/}")
private var loggingExceptions: Array<String> = emptyArray()
Is there anything wrong with doing it this way? Most of the resources I found on the web recommend to set the default in the annotation itself. Is there any advantage of doing it that way?

Spring boot cloud oaut2 custom field

I'm trying to build an app that use custom oaut2 authentication with spring boot. I want to set a groupId for principal and use it for spring jpa on resource server.
#Query("select o from BusinessObject o where o.owner.id = ?#{principal.claims['companyId']} or 1=?#{hasRole('ROLE_ADMIN') ? 1 : 0}")
List<BusinessObject> findBusinessObjectsForCurrentUserById();
How to succeed this?

Display default value of a model attribute in Swagger UI

I am using Spring Boot Java and Swagger 2 to document my APIs.
What Spring annotation can I use to show the default value mydoggie in Swagger UI?
You can add an example and a default value to show in Swagger 2 with this annotation:
#ApiModelProperty(value = "value to show", example = "example to show")
I hope it helps

Enabling Mandatory Validation while fetching Data from database using Spring JPA with Hibernate

I am developing an application using Spring JPA 2.0 with Hibernate as a ORM provider. We have only read only access to database and will generate report. I would like do some validation while fetching data.
#Column(name = "LOGICAL_ID", nullable = false)
#NotNull
private Long logicalId;
I added Hibernate validator which implements JSR 303 specs. But while fetching it doesn't throw any runtime exception or ConstraintViolationException? Do i add something in the configuration or am i missing something? Please advice me.
Like i've posted in Implementing cross-validation in java
You can use the following piece of code to validate an entity manually;
ValidatorFactory factory = Validation.byDefaultProvider().configure().traversableResolver(new CustomTraversableResolver() ).buildValidatorFactory();
Validator validator = factory.getValidator();
Set<ConstraintViolation<YourBaseClass>> constraintViolations = Validator.validate(myEntityToValidate);
If you would like this to be done for you automatically, it might be that the 'javax.persistence.validation.mode' property is set to none (http://docs.jboss.org/hibernate/entitymanager/3.6/reference/en/html/configuration.html).
I prefer the manual validation though, since then you have control and details, of which constraints are not passing.

How to consume REST URLs using Spring MVC?

I have developed few RESTful methods and exposed them via Apache Cxf
I'm developing the client side application using Spring MVC and I'm looking for a simple example to demonstrate how to call/consume these REST methods using Spring MVC
I know how to do it using Apache http client but prefer to use Spring MVC in case such this has already been implemented there.
Spring provides simple wrapper to consume RESTful services called RestTemplate. It performs path variable resolution, marshalling and unmarshalling:
Map<String, Integer> vars = new HashMap<String, Integer>();
vars.put("hotelId", 42);
vars.put("roomId", 13);
Room room = restTemplate.getForObject(
"http://example.com/hotels/{hotelId}/rooms/{roomId}",
Room.class, vars);
Assuming Room is a JAXB object which can be understood by The RestTemplate.
Note that this class has nothing to do with Spring MVC. You can use it in MVC application, but also in a standalone app. It is a client library.
See also
REST in Spring 3: RestTemplate
Use path variables to consume REST data. For example:
https://localhost/products/{12345}
This pattern should give you the detail of the product having product id 12345.
#RequestMapping(value="/products/{productId}")
#ResponseBody
public SomeModel doProductProcessing(#PathVariable("productId") String productId){
//do prpcessing with productid
return someModel;
}
If you want to consume Rest Service from another service then have a look at:
http://static.springsource.org/spring/docs/3.0.x/javadoc-api/org/springframework/web/client/RestTemplate.html
and
http://www.informit.com/guides/content.aspx?g=java&seqNum=546

Resources