Is there "The Proper way" of using Spring validator? - java

I was looking up on how to perform validation in Spring MVC.
Manually call Spring Annotation Validation
Spring MVC: How to perform validation?
The above to links are couple of those. What I was wondering is that calling the Validator.validate manually is considered a bad practice compared to that of using #Valid/#Validated annotations ?

Related

Spring WebFlux Handler Interceptor Adapter

I am migrating a Spring MVC library to Spring WebFlux. There is a feature that lets our clients annotate their controller methods to perform custom validation (and applying some business rules) on incoming headers before granting access to the API.
In Spring MVC we had that accomplished by using HandlerInterceptorAdapter. Since WebFlux doesn't have anything similar I was trying out the solution as suggested by Rossen here. However it doesn't work for this use case since the method handler info is only available in onSuccess operator and its too late to get the info for annotation processing.
I was trying out the other approach suggested there using #ModelAttribute method on a #ControllerAdvice but that only work if the annotation is applied to the Controller class in our case the annotation is applied on methods of controller class.
Here is a sample https://github.com/ranarula/handleInterceptor with the issue
Any pointers on how to go about implementing the annotation processing in WebFlux will help.

Spring with Hibernate annotation based

I am looking at Spring documentation to learn Spring integration with Hibernate using annotation based spring configuration.
The documentation link is here.
Now the docs tell about how to configure Spring with Hibernate using xml files and there is no where mentioned how to use annotations for integrating Spring and Hibernate.
Please help me where can I find the explanation on Spring with Hibernate integration using annotations.
You can find the required annotations here - Annotations used for configuring DAO or Repository classes
Here is a good example for using Spring with Hibernate - Spring Hibernate Integration Using Annotations
http://examples.javacodegeeks.com/enterprise-java/hibernate/hibernate-jpa-dao-example/
I'm not sure if I should downvote you - you could easily find it on your own.
Ok, maybe it is not exactly Spring + Hibernate, but it really does not matter.

How does the EnableAutoConfiguration spring annotation work?

I am no fan of gross over abstractions, And i think Spring has committed a major felony.
But I'm willing to overlook it this time if someone can explain the algorithm behind the 'auto' configuration.
Having a look at spring's own javadocs, It doesn't give much away other than saying that it will intelligently guess what you need and something to do about conditional beans.
Does someone know what algorithm is used to determine what needs to be loaded?
In my experience as a Spring Boot user the basic factors for Spring Boot to decide on what auto-configurations will be enabled are:
1) The classes present on the classpath. For example if RabbitMQ and Spring AMQP classes are present, then the RabbitAutoConfiguration will be enabled. The corresponding annotation is #ConditionalOnClass,
2) The presence or not of user defined beans. For example, if all the Spring Data JPA is present on the classpath, Spring Boot will register a LocalContainerEntityManagerFactoryBean bean only if the user has not already done so. The beans registered by the user will 'override' the default ones. The relevant annotation is #ConditionalOnMissingBean
As #DaveSyer mentions, you can of course use Spring Boot without #EnableAutoConfiguration if you want to include the relevant configuration on your own. Or you could use the less drastic solution of the exclude field of #EnableAutoConfiguration. If for example you want Spring Boot to autoconfigure everything except ActiveMQ, you would use #EnableAutoConfiguration(exclude=ActiveMQAutoConfiguration.class)
In my opinion, there is absolutely no felony here! You can use what you want from Spring Boot. When you don't want something it has to offer, you can easily opt out partially or completely!
Also if you want to get a look under the covers, you can add the property
logging.level.org.springframework.boot=DEBUG
to application.properties and Spring Boot will gladly give a detailed report of what was auto-configured and what wasn't
There is some documentation in the Spring Boot Reference Guide. It's not terribly complicated, and I hardly think it's a felony to just include a bunch of #Configuration that you might have written anyway (because that's all it does). Feel free not to use #EnableAutoConfiguration if you prefer to include the individual configurations individually.

spring ModelMap implementation

I'm planning to use spring ModelMap. And also like to specify the view name explicitly.
Do you think it's a good idea ? so that i can use the controller ?
How can I specify a view name with ModelMap? If possible give a small implementation.
Refer spring Web MVC tutorial
You will get spring Web MVC simple example from here

create new spring annotation

I'm developing an application in Spring which has numerous methods in the controller, and are mapped to various URL using #RequestMapping.
Some of these methods (and hence url) are accessible to some user roles, and not to others. Can I create annotations, and annotate these methods so that only privileged users have access to right methods, while not to the others?
Thanks in advance.
I am not sure if creating new annotations is what you really need. You could probably use Spring Security and its annotations. Please take a look here to see if it is what you are looking for.
You can use Spring Security and its annotations, restrict access to some methods. This Blog post show some features of annotations used with Spring Security 3.
Have a look at this question and answer, to see how you build your own annotations on top of spring security.
See also the documentation of Spring Security 3.1, Chapter Expression-Based Access Control.

Categories