Is it possible to invalidate a spring security session?

You can’t usually invalidate a user session(s) immediately you change their account information without resorting to a container specific API, since the only way to access the HttpSession is through the HttpServletRequest object. Instead you can cache the username in an in-memory store and consult it either in a filter or a custom AccessDecisionVoter. Using … Read more

How to handle DataIntegrityViolationException in Spring?

The problem with showing user-friendly messages in the case of constraint violation is that the constraint name is lost when Hibernate’s ConstraintViolationException is being translated into Spring’s DataIntegrityViolationException. However, you can customize this translation logic. If you use LocalSessionFactoryBean to access Hibernate, you can supply it with a custom SQLExceptionTranslator (see LocalSessionFactoryBean.jdbcExceptionTranslator). This exception translator … Read more

Adding custom RequestCondition’s in Spring mvc 3.1

Spring MVC already provides a mechanism for distinguishing between json and html, the RequestMapping annotation takes a consumes attribute which looks at the content type of the request… // REST version, Content-type is “application/json” @RequestMapping(value = “https://stackoverflow.com/”, consumes = “application/json”) public void myRestService() { … // HTML version, Content-type is not “application/json” @RequestMapping(value = “https://stackoverflow.com/”, … Read more

Spring MVC bean mapping to HTTP GET request parameters similar to @BeanParam

Simply create a Pojo Java Bean with fields with names that match your request parameters. Then use this class as an argument for your request handler method (without any additional annotations) public class Example { private String x; private Integer y; //Constructor without parameter needed! public Example(){} //Getter + Setter } @Controller @RequestMapping(“someUrl”) public class … Read more