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

Spring: define @RequestMapping value in a properties file

It should be possible to use placeholders in @RequestMapping, like for example @RequestMapping(“${}”). Take a look at the documentation for more details: Patterns in @RequestMapping annotations support ${…​} placeholders against local properties and/or system properties and environment variables. This may be useful in cases where the path a controller is mapped to may need to … Read more

Spring @ExceptionHandler does not work with @ResponseBody

Your method @ExceptionHandler(IllegalArgumentException.class) public @ResponseBody Map<String, Object> handleException(final Exception e, final HttpServletRequest request, Writer writer) does not work because it has the wrong return type. @ExceptionHandler methods have only two valid return types: String ModelAndView. See for more information. Here’s the specific text from the link: The return type can be a String, which … Read more

Can I make a custom controller mirror the formatting of Spring-Data-Rest / Spring-Hateoas generated classes?

I’ve found a way to imitate the behavior of Spring Data Rest completely. The trick lies in using a combination of the PagedResourcesAssembler and an argument-injected instance of PersistentEntityResourceAssembler. Simply define your controller as follows… @RepositoryRestController @RequestMapping(“…”) public class ThingController { @Autowired private PagedResourcesAssembler pagedResourcesAssembler; @SuppressWarnings(“unchecked”) // optional – ignores warning on return statement below… … Read more

how to display custom error message in jsp for spring security auth exception

Redefine the properties in inside spring security jar. For example add to the classpath and add a message source to the context: AbstractUserDetailsAuthenticationProvider.badCredentials=Username/Password entered is incorrect. AbstractUserDetailsAuthenticationProvider.disabled=Your account is diabled, please contact administrator. At Salvin Francis: Add to the WAR file inside WEB-INF/classes. Add this bean to spring context config file Message … Read more

Can not find the tag library descriptor of springframework

I know it’s an old question, but the tag library is provided by spring-webmvc package. With Maven it can be added to the project with the following lines to be added in the pom.xml <properties> <spring.version>3.0.6.RELEASE</spring.version> </properties> <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${spring.version}</version> </dependency> Without Maven, just add that jar to your classpath. In any … Read more

Neither BindingResult nor plain target object for bean name available as request attr [duplicate]

Make sure that your Spring form mentions the modelAttribute=”<Model Name”. Example: @Controller @RequestMapping(“/greeting.html”) public class GreetingController { @ModelAttribute(“greeting”) public Greeting getGreetingObject() { return new Greeting(); } /** * GET * * */ @RequestMapping(method = RequestMethod.GET) public String handleRequest() { return “greeting”; } /** * POST * * */ @RequestMapping(method = RequestMethod.POST) public ModelAndView processSubmit(@ModelAttribute(“greeting”) Greeting … Read more

Dynamic Selection Of JsonView in Spring MVC Controller

On the off chance someone else wants to achieve the same thing, it actually is very simple. You can directly return aorg.springframework.http.converter.json.MappingJacksonValue instance from your controller that contains both the object that you want to serialise and the view class. This will be picked up by the org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter#writeInternal method and the appropriate view will be … Read more