Annotation Based Spring MVC

Introduction – Annotation based spring MVC

With increased support for annotations in Spring framework, it has evolved more beautifully. Spring exploits annotations to the best of its use. Eliminating the use of xml based configuration, which I was waiting for a long time. Imagine a scenario where you really do not need to load xml based configuration files to host a spring web application. Here we try to write a simple Annotation Based Spring MVC REST Web-service.

This article describes briefly how to use annotation based configuration in Spring.

Web Application Development from Scratch

Here is another series of posts which helps you learn web application development using Spring and Hibernate and related J2EE technologies.

Need for annotation based configuration

There have been mixed talks about its pros and cons, many people think its not easy to debug the annotation based spring applications whereas others think there is nothing much to debug in the application if annotations are properly used.
What ever be the result of the discussion or debate annotations are here to stay and according to me they add a powerful feature to the layered architecture and programming.

Goal

Here we will try to demonstrate how to bootstrap Spring MVC Web application without a configuration file. We will create a simple REST web-service which hosts a single endpoint where GET requests can be made to retrieve the present list of users. A pretty trivial application but it solves the purpose.

Assumptions

  • I would like to assume that the reader has basic understanding of, how Spring works
  • Also, one more assumption is that we are trying to learn Spring MVC web application configuration.

Requirements

We need the following set of jars which can be downloaded along with the Spring distribution package.

  • commons-logging-1.1.1.jar
  • jackson-core-asl-1.9.5.jar
  • jackson-mapper-asl-1.9.5.jar
  • servlet-api-2.5.jar
  • spring-beans-3.2.0.RC1.jar
  • spring-context-3.2.0.RC1.jar
  • spring-core-3.2.0.RC1.jar
  • spring-expression-3.2.0.RC1.jar
  • spring-web-3.2.0.RC1.jar
  • spring-webmvc-3.2.0.RC1.jar

Implementation and Code

Now that we know, we are not using spring xml configuration, we will write a java class for the configuration. below is the code for the same.

@Configuration tells the container that this is the configuration class for Spring.
@EnableWebMvc tells the container that we are using WebMVC.
@ComponentScan tells to scan the base packages and all packages inside the base package to search for any class or method which is annotated and should be loaded for Spring to boot up without errors. The excludeFilters tells not to consider the @Configuration annotation again as it will be done by the web container before loading the Spring configuration.

The class will implement various methods depending upon what our application requires. As we are just writing a simple REST web-service which returns a JSON object containing the list of users. We will only require content negotiation which will eventually identify the request and return a JSON object in response.

Web.xml configuration

Now that we are done with the configuration class we would like to load this class when the application server container starts. Here is the web.xml, the xml is fairly simple.

In the first tag it says that we are using annotation based configuration.
In the second tag it specifies the location of the Configuration class, it can be a package as well.
And then the normal servlet mapping.

Controller for the REST Web-Service

We need a controller class to receive all the request from the client and respond accordingly. This would be a simple java class with one method, which handles the /all URL request. Here is the code for the same.

Service Layer

We also need a service class, to satisfy the MVC architecture, the job of the service class would be to respond to the calls made from Controller and return appropriate results. Here is the code for the Service class. Spring prefers an interface driven design hence we will have an interface and an implementation for the Service. The service simply invokes the method of DAO layer to fetch the results.

Below is the implementation for the above service interface.

DAO Layer

DAO layer is the layer of our application which accesses the data store and fetches required set of data. In here we just prepare a list of the model object and return it back all along to the controller layer. Here goes the code:

Below is the implementation for the above dao interface.

This is all the code we needed, for running the same if you are using Eclipse, you can download the attached source code and try to run the same afte putting all the jars in the class path.

Run the project on server (Apache Tomcat or any other server). And when you access the URL http://localhost:8080/TestAnnotation/all
This is what we get:

Conclusion

This is a simple Spring application which can be hosted as a REST web-service without using configuration xmls.
Stay connected and stay Subscribed