As Spring continues to evolve each new release improves support for creating and consuming REST web services. This brief article provides two simple example projects: one that uses Spring Web MVC to create a REST web service and one that uses Spring RestTemplate to call the REST web service and process the response.
Spring Web MVC and REST
Updated in January 2014 to include using a Spring MVC REST web service to create a Person object and added unit/integration tests of the REST web service methods.
Visit GitHub to get the example application, springwebservice. See the project's ReadMe.txt file for how to use Maven to build and deploy the application. I tested the application on Tomcat 7.
With just a few lines of code and several Spring annotations you can create a REST web service. See class PersonController in the example application. The web service returns a single JSON-formatted Person object.
Consult the references below to learn more about using Spring Web MVC to create a REST web service.
Visit GitHub to get the example application, springwebserviceclient. Again view the project's ReadMe.txt file for how to use Maven to build and run the example application. Note that this application expects the Servlet container (e.g. Tomcat) to be running on localhost:8080. If that is not the case then you will need to modify the url String value in the App.java class (see line 44).
Using RestTemplate you just need one line of code to call the web service and process its response. See class App.java, line 51.
Consult the references below to learn more about using Spring RestTemplate to consume a REST web service.
The Spring Security framework can be used to implement authentication and authorization between the web service client and the web service. I did not include security in these simple example applications.
Testing Spring Web MVC REST
One of the new features in Spring 3.2 is the Spring MVC Test Framework. See the reference below to learn how to use this framework to create unit tests of REST web services.
- Spring Example Applications, https://github.com/phillips1021/phillips_spring_examples
- Spring Framework Documentation, http://docs.spring.io/spring/docs/3.2.x/spring-framework-reference/html/
- Spring Web MVC, http://docs.spring.io/spring/docs/3.2.x/spring-framework-reference/html/mvc.html
- Spring RestTemplate Documentation, http://docs.spring.io/spring/docs/3.2.x/spring-framework-reference/html/remoting.html#rest-resttemplate
- Spring RestTemplate API, http://docs.spring.io/spring/docs/3.2.x/javadoc-api/org/springframeworkRestTemplate.html
- Spring Security, http://docs.spring.io/spring-security/site/index.html
- Spring MVC – Easy REST-Based JSON Services with @ResponseBody, http://www.javacodegeeks.com/2013/04/spring-mvc-easy-rest-based-json-services-with-responsebody.html
- Restful code example using Spring MVC, http://byteposts.blogspot.com/2013/08/restful-code-example-using-spring-mvc.html
- How To Setup BASIC Authentication with Spring Web Services, http://java.dzone.com/articles/how-setup-basic-authentication
- Spring MVC Test Framework, http://docs.spring.io/spring/docs/3.2.x/spring-framework-reference/htmlsingle/#spring-mvc-test-framework