Quick look at Java Spring MVC framework

Java software market is still bigger than .NET. Many times, when I heard that some friend on mine is working as Java Web Developer, he is using Spring MVC framework. Finally I decided to look at this (most popular?) Java Web Framework.

I started with Pluralsight.net tutorials: Introduction to Spring MVC and Spring with JPA and Hibernate.

First surprise for me was a lot of xml based configuration, which needs to be written by developer. E.g. we need to configure which class is controller by declaring its path in xml file (servlet-config.xml). For some other elements we need to provide long uris as packages’ names. So easy to misspell. This is ‘configuration over convention’, instead of ‘convention over configuration’ as it is in Ruby on Rails or ASP.NET MVC.

As an example, look at this piece of xml, which is needed to handle JSON and XML requests (all written by hand):

<bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
		<property name="order" value="1" />
		<property name="contentNegotiationManager">
			<bean class="org.springframework.web.accept.ContentNegotiationManager">
				<constructor-arg>
					<bean class="org.springframework.web.accept.PathExtensionContentNegotiationStrategy">
						<constructor-arg>
							<map>
								<entry key="json" value="application/json" />
								<entry key="xml" value="application/xml" />
							</map>
						</constructor-arg>
					</bean>
				</constructor-arg>
			</bean>
		</property>
		
		<property name="defaultViews">
			<list>
				<bean class="org.springframework.web.servlet.view.json.MappingJacksonJsonView" />
				<bean class="org.springframework.web.servlet.view.xml.MarshallingView">
					<constructor-arg>
						<bean class="org.springframework.oxm.xstream.XStreamMarshaller">
							<property name="autodetectAnnotations" value="true" />
						</bean>
					</constructor-arg>
				</bean>
			</list>
		</property>		
	</bean>

Going back to controller: besides its configuration in XML file, we need to add annotation @Controller on its class and @RequestMapping on methods (no default routing). Additionally we always need to return the name of view (instead of some default value). Here is a sample controller with one action:

@Controller
public class HelloWorldController {

    @RequestMapping("/helloWorld")
    public String helloWorld(Model model) {
        model.addAttribute("message", "Hello World!");
        return "helloWorld";
    }
}

Above code will return the view helloWorld.jsp (according to convention, located in webapp/WEB-INF/jsp/ directory):

<!DOCTYPE html>
<html>
    <head></head>
    <body>
        Message: ${message}
    </body>
</html>

The result should looks like that:

Spring MVC Hello World

Connecting application with database is not easier. Adding dependencies and writing code to make Hibernate working with the app took 1h(!) in Pluralsight tutorial (Spring with JPA and Hibernate). It is over 50 lines of xml. Additionally as we know – it is very easy to make a mistake during writing xml. In ASP.NET MVC it can be done automatically (eventually we can change data source by specifying connection string). I didn’t find easier way to do that (in Spring). Of course we can use copy/paste method and just set some properties, but…where is code generation for that?

After all, I would like to notice how big step was made by other MVC Web Frameworks like Rails, Django or ASP.NET MVC. Lot of stuff, which has to be written by hand in Spring, is already implemented under framework layer (in Rails, Django and ASP.NET MVC). However Spring MVC was also big step ahead, when it was created in 2002. In this video people are explaining that (e.g. because of Spring they do not need to rewrite lots of boilerplate code anymore).

I wonder whether next version of Spring MVC will have more ‘conventions’ and default configurations. It will make the framework more developer friendly and easier to maintain.

If you want to start with Spring MVC i recommend you to start with Pluralsight.net tutorials: Introduction to Spring MVC and Spring with JPA and Hibernate. You can also find this website useful, as well as Spring Framework Reference Documentation. I didn’t read any book, but I found those two most often recommended: Spring Recipes and Spring in Action.