How to handle static content in Spring MVC?

Since I spent a lot of time on this issue, I thought I’d share my solution. Since spring 3.0.4, there is a configuration parameter that is called <mvc:resources/> (more about that on the reference documentation website) which can be used to serve static resources while still using the DispatchServlet on your site’s root. In order … Read more

What causes “java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name ‘command’ available as request attribute”?

You’re trying to use Spring MVC’s form tag. This tag renders an HTML form tag and exposes a binding path to inner tags for binding. It puts the command object in the PageContext so that the command object can be accessed by inner tags. [..] Let’s assume we have a domain object called User. It … Read more

Spring Boot Configure and Use Two DataSources

Here you go. Add in your application.properties file: #first db spring.datasource.url = [url] spring.datasource.username = [username] spring.datasource.password = [password] spring.datasource.driverClassName = oracle.jdbc.OracleDriver #second db … spring.secondDatasource.url = [url] spring.secondDatasource.username = [username] spring.secondDatasource.password = [password] spring.secondDatasource.driverClassName = oracle.jdbc.OracleDriver Add in any class annotated with @Configuration the following methods: @Bean @Primary @ConfigurationProperties(prefix=”spring.datasource”) public DataSource primaryDataSource() { return … Read more

Infinite Recursion with Jackson JSON and Hibernate JPA issue

JsonIgnoreProperties [2017 Update]: You can now use JsonIgnoreProperties to suppress serialization of properties (during serialization), or ignore processing of JSON properties read (during deserialization). If this is not what you’re looking for, please keep reading below. (Thanks to As Zammel AlaaEddine for pointing this out). JsonManagedReference and JsonBackReference Since Jackson 1.6 you can use two … Read more

Why does Spring MVC respond with a 404 and report “No mapping found for HTTP request with URI […] in DispatcherServlet”?

Your standard Spring MVC application will serve all requests through a DispatcherServlet that you’ve registered with your Servlet container. The DispatcherServlet looks at its ApplicationContext and, if available, the ApplicationContext registered with a ContextLoaderListener for special beans it needs to setup its request serving logic. These beans are described in the documentation. Arguably the most … Read more

What’s the difference between @Component, @Repository & @Service annotations in Spring?

From Spring Documentation: The @Repository annotation is a marker for any class that fulfils the role or stereotype of a repository (also known as Data Access Object or DAO). Among the uses of this marker is the automatic translation of exceptions, as described in Exception Translation. Spring provides further stereotype annotations: @Component, @Service, and @Controller. … Read more

How do I POST JSON data with cURL?

You need to set your content-type to application/json. But -d (or –data) sends the Content-Type application/x-www-form-urlencoded, which is not accepted on Spring’s side. Looking at the curl man page, I think you can use -H (or –header): -H “Content-Type: application/json” Full example: curl –header “Content-Type: application/json” \ –request POST \ –data ‘{“username”:”xyz”,”password”:”xyz”}’ \ http://localhost:3000/api/login (-H … Read more

@Requestparam with a param and without

Yes, it’s possible. Assuming you’r using Spring take this code as an example: @RequestMapping(value=”/getStudentOverallPerformance) public String example(@RequestParam(required=false) long yid, etc…){ /* your code */ } In this case you can call this method with or without parameter “yid” that i declare as “long” but you will use type you declared. I set: (required=false) because as … Read more