Why Spring MVC is best for Creating RESTful Web Services in Java?
Tags: beginners, java, webdev, programming
Published: 2019-05-10 14:02:26

---
title: Why Spring MVC is best for Creating RESTful Web Services in Java?
published: true
description: A couple of reasons why creating RESTful Web services in Java using Spring make sense. 
tags: beginners,java,webdev,programming 
cover_image: https://res.cloudinary.com/practicaldev/image/fetch/s--2z33vhIJ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/1000/0%2A3TbwpBINLVfEsv2v.png
---
*Disclosure: This post includes affiliate links; I may receive compensation if you purchase products or services from the different links provided in this article.*

Many of you agree that REST has now become a standard way to develop web services (some may argue GraphQL) and when it comes to Java, there are many frameworks and libraries available, like [JAX-RS](https://github.com/jax-rs), [Restlet](http://javarevisited.blogspot.sg/2016/10/restlet-helloworld-example-in-java-and-Eclipse.html), [Jersey](http://javarevisited.blogspot.sg/2017/06/jersey-web-service-hello-world-example.html), [RESTEasy](http://javarevisited.blogspot.sg/2017/02/difference-between-jax-rs-restlet-jersey-apache-cfx-RESTEasy.html), Apache CXF, etc., but I encourage Java developers to use Spring MVC to develop RESTful web services.

Some of you might ask, *why use Spring MVC Framework to develop RESTful web services in Java?* What are the advantages, and why is it better than other [frameworks](https://dev.to/javinpaul/10-frameworks-java-and-web-developers-can-learn-in-2019-17ke) and [libraries](https://javarevisited.blogspot.com/2018/01/top-20-libraries-and-apis-for-java-programmers.html) available out there?

Well, the most important reason, I think, to use Spring for developing RESTful web service is that you can use your [Spring MVC](http://javarevisited.blogspot.sg/2017/06/how-spring-mvc-framework-works-web-flow.html) experience for developing RESTful web services and you don't need to learn a new framework or library, which means you can quickly roll out your REST API.

This is one of the biggest advantages, I mean, leveraging your years of experience on Spring MVC to expose your application as REST APIs.

Another reason is that *Spring has excellent support for developing RESTful web services*.

In the last couple of versions, starting from Spring version 3.0, it has provided a lot of enhancements to Spring MVC to provide first-class REST support. It has provided dedicated annotations, like [**@RestController**](http://javarevisited.blogspot.sg/2017/08/difference-between-restcontroller-and-controller-annotations-spring-mvc-rest.html)and [**@ResponseStatus**](http://javarevisited.blogspot.sg/2011/09/spring-interview-questions-answers-j2ee.html#axzz55mVSPFfH) to make the development of RESTful resources even easier in Spring 4.0.

It's also not only help you to create RESTful web services but also provides classes to consume REST resources like you can use the [**RestTemplate**](http://javarevisited.blogspot.sg/2017/02/how-to-consume-json-from-restful-web-services-Spring-RESTTemplate-Example.html)class to consume RESTful resources.

There are many more utility classes and annotations which make the development of RESTful web services in Spring easier and seamless and I'll share a couple of them in this article to prove my point that using Spring to develop [RESTful Web service](https://javarevisited.blogspot.com/2018/02/top-5-restful-web-services-with-spring-courses-for-experienced-java-programmers.html) is the right decision.



### 7 Reasons for Using Spring MVC for Creating RESTful Web Services in Java

As I told you in the first paragraph, we can use [Spring MVC](https://javarevisited.blogspot.com/2020/08/top-5-courses-to-learn-spring-mvc-for.html) to create and consume RESTful web services. 

Now, let's see those supports in a little bit more detail so that you can make the best use of them and quickly develop the RESTful services you always wanted.

**1.** In Spring MVC, a controller can handle the requests for all HTTP methods, which is a backbone of RESTful web services. For example, you can handle a GET method to perform read operations, [POST](http://javarevisited.blogspot.sg/2016/10/difference-between-put-and-post-in-restful-web-service.html) methods to create resources, [PUT](http://www.java67.com/2016/09/when-to-use-put-or-post-in-restful-web-services.html) methods to update resources, and DELETE methods to remove resources from the server.

From Spring 3.2 onwards, you can also handle PATCH requests. By the way, if you are not familiar with the Spring MVC framework, then you should first take a look at that; [Spring MVC For Beginners](https://click.linksynergy.com/fs-bin/click?id=JVFxdTr9V80&subid=0&offerid=562016.1&type=10&tmpid=14538&RD_PARM1=https%3A%2F%2Fwww.udemy.com%2Fspring-mvc-tutorial-for-beginners-step-by-step%2F) is a good place to begin.

[![](https://cdn-images-1.medium.com/max/1000/1*XF39gw6pVak96d6XP3gEPw.jpeg)
](https://click.linksynergy.com/fs-bin/click?id=JVFxdTr9V80&subid=0&offerid=562016.1&type=10&tmpid=14538&RD_PARM1=https%3A%2F%2Fwww.udemy.com%2Fspring-mvc-tutorial-for-beginners-step-by-step%2F)


**2.** In the case of REST, the representation of data is very important and that's why Spring MVC allows you to bypass View-based rendering altogether by using the `@ResponseBody` annotation and various `HttpMessgeConverter`implementations.

By using this, you can directly send a response to a client, e.g. the resource clients want and also in the format they want. See [here](http://courses.baeldung.com/p/rest-with-spring-the-certification-class?affcode=22136_bkwjs9xa) to learn more about `HttpMessageConvert` and `@ResponseBody` annotation.

[![](https://cdn-images-1.medium.com/max/1000/0*3TbwpBINLVfEsv2v.png)](http://courses.baeldung.com/p/rest-with-spring-the-certification-class?affcode=22136_bkwjs9xa)


**3.** The Spring 4.0 release added a dedicated annotation, `@RestController`, to make the development of RESTful web services even easier.

If you annotate your controller class using `@RestController` instead of [@Controller](http://javarevisited.blogspot.sg/2017/08/difference-between-restcontroller-and-controller-annotations-spring-mvc-rest.html) then Spring applies message conversations to all handler methods in the controller.

This means you don't need to annotate each method with the `@ResponseBody`annotation. This also makes your code much cleaner. If you don't know about `@ResponseBody and `@RequestBody annotation, I suggest you check [Spring Framework 5: Beginner to Guru](https://click.linksynergy.com/fs-bin/click?id=JVFxdTr9V80&subid=0&offerid=323058.1&type=10&tmpid=14538&RD_PARM1=https%3A%2F%2Fwww.udemy.com%2Fspring-framework-5-beginner-to-guru%2F) course by John Thomson, one of the best Spring Framework instructor. 


**4\.** One of the main differences between a REST web service and a normal web application is that the REST pass resource identifies data in the URI itself, like `/messages/101`, while web applications normally use a query parameter, like`/messages?Id=101`.

If you remember, we use [@RequestParam](http://javarevisited.blogspot.sg/2017/10/differences-between-requestparam-and-pathvariable-annotations-spring-mvc.html) to get the value of those query parameters but, not to worry, Spring MVC also provides a [@PathVariable](http://javarevisited.blogspot.sg/2017/10/differences-between-requestparam-and-pathvariable-annotations-spring-mvc.html)annotation which can extract data from a URL. It allows the controller to handle requests for parameterized URLs.

**5\.** Another key aspect of RESTful web services is Representation, meaning the same resource can be represented in different formats, i.e. JSON, XML, HTML, etc. Thankfully, Spring provides several view implementations and views resolvers to render data as [JSON](http://www.java67.com/2016/10/3-ways-to-convert-string-to-json-object-in-java.html#.WkOmLRqktTU.linkedin), [XML](http://javarevisited.blogspot.sg/2015/07/how-to-read-xml-file-as-string-in-java-example.html#axzz55mVSPFfH), and [HTML](http://javarevisited.blogspot.sg/2014/09/how-to-parse-html-file-in-java-jsoup-example.html).

For example, `ContentNegotiatingViewResolver` can look at the file extension of requests or Accept header to find out the correct representation of a resource for the client.

**6\.** Similar to the `@ResponseBody` annotation, which is used for converting the response to the format client wants (by using `HttpMessageConverts`), [Spring MVC](https://medium.com/javarevisited/21-spring-mvc-rest-interview-questions-answers-for-beginners-and-experienced-developers-21ad3d4c9b82) also provides `@RequestBody` annotation, which uses `HttpMethodConverter` implementations to convert inbound HTTP data into Java objects passed into a controller's handler method.

If you are not familiar with handler methods and Spring MVC in general, then you can also check out [3 ways to learn Spring MVC](http://javarevisited.blogspot.sg/2018/01/how-to-learn-spring-core-spring-mvc-boot-security-framework.html).

[![](https://cdn-images-1.medium.com/max/1000/0*gbFt9EjXvR_wsD4A.png)](https://click.linksynergy.com/fs-bin/click?id=JVFxdTr9V80&subid=0&offerid=562016.1&type=10&tmpid=14538&RD_PARM1=https%3A%2F%2Fwww.udemy.com%2Fspring-mvc-tutorial-for-beginners-step-by-step%2F)


**7\.** TheSpring framework also provides a template class, `RestTemplate`, which is similar to `JdbcTemplate`, and `JmsTemplate`, which can consume REST resources. You can use this class to [test your RESTful web service](http://www.java67.com/2017/10/how-to-test-restful-web-services-using.html) or develop REST clients.

These were some of the **important features of the Spring MVC framework**which assist in developing [RESTful web services](https://medium.com/javarevisited/top-5-books-and-courses-to-learn-restful-web-services-in-java-using-spring-mvc-and-spring-boot-79ec4b351d12). As I said earlier, the most important reason for me to choose Spring or Spring Boot for developing RESTful resources is that I can use my existing knowledge of the framework, which means no steep learning curve.

If you look at it from a high level, developing RESTful services is not very different from developing a web application.

The fundamental difference is that in the case of the former, we mostly deal with human users where in the case of REST you have to deal with non-human users, mostly rich JavaScript clients and mobile applications.

This key difference then causes other differences, like representing data in JSON or XML instead of HTML which is suitable for human users but not for non-human systems. If you want to learn more before starting developing production-level RESTful web services using Spring, [**REST with Spring**](http://courses.baeldung.com/p/rest-with-spring-the-certification-class?affcode=22136_bkwjs9xa)course is a good place to start.


Other **Spring** and **REST Resources** you may like
[How to Build Microservices with Spring Cloud?](https://click.linksynergy.com/fs-bin/click?id=JVFxdTr9V80&subid=0&offerid=323058.1&type=10&tmpid=14538&RD_PARM1=https%3A%2F%2Fwww.udemy.com%2Fmicroservices-with-spring-cloud%2F)
 [Microservices With Spring Boot and Spring Cloud](https://click.linksynergy.com/fs-bin/click?id=JVFxdTr9V80&subid=0&offerid=323058.1&type=10&tmpid=14538&RD_PARM1=https%3A%2F%2Fwww.udemy.com%2Fexploring-spring-boot-and-spring-cloud-microservices%2F)\
 [Difference between Restlet, Jersey, and RESTEasy in Java?](http://javarevisited.blogspot.sg/2017/02/difference-between-jax-rs-restlet-jersey-apache-cfx-RESTEasy.html)
 [What is the use of DispatcherServlet in Spring MVC?](http://www.java67.com/2017/06/what-is-use-of-dispatcherservlet-in-spring-mvc.html)
  [Top 5 Courses to Learn Spring Boot in 2021](https://hackernoon.com/top-5-online-courses-to-learn-spring-boot-in-2019-c2fd7a0282c2)
 [How to enable Spring security in a Java web application?](http://javarevisited.blogspot.sg/2017/05/how-to-enable-spring-security-in-java-web-application.html)
 [Spring in Action by Craig Walls](https://www.amazon.com/Spring-Action-Craig-Walls/dp/1617294942?tag=javamysqlanta-20)
 [Spring Framework 5: Beginner to Guru](https://click.linksynergy.com/fs-bin/click?id=JVFxdTr9V80&subid=0&offerid=323058.1&type=10&tmpid=14538&RD_PARM1=https%3A%2F%2Fwww.udemy.com%2Fspring-framework-5-beginner-to-guru%2F)

Thanks for reading this article, if you like these reasons for developing RESTful web services using Spring then please share with your friends and colleagues. If you have any questions or feedback, then please drop a note