Spring MVC Controllers

Spring MVC Controller provides simple but great way for creating Web Application using Java technology. Spring gives developer to ease of development using MVC design pattern.

MVC stands for Mode-View-Controller, in this approach we can divide out software development into 3 layers namely Model, View, and Controller.

Model layer is responsible for data related works, View layer is responsible for presentation logic and Controller synchronizes both View and Model part. Controller plays very important part in MVC. It takes care how to handle request or events, how to handle Security related stuff, how to send response? All this is decided by controller. There are many ways to create Spring Base Web application project. We choose SpringBoot with gradle build tool particularly.

Spring MVC offers two type of Controllers classes:

  1. Annotated with @Controller
  2. Annotated with @RestController

If we simply want to create a website using Spring MVC in which use a view technology (i.e. JSP/HTML pages), then first approach is better because @Controller provide both data as well as view.

But If want to create RESTful web services, in which we only want data independent from view, then Second approach is best because @RestController only deals in term of data. Suppose a client want to a web application for his business, then we should go for RESTful services. The reason behind this is that in future that particular client wants to make a mobile app (android app or iOS app), then we only need data from server not UI(view), because this data will show in native UIs.

We can also use @Controller for returning data only by using @ResponseBody annotation.

So we can say mathematically:

@RestController =@Controller + @ResponseBody on every method

Note that we use often as

@RequestMapping (value = "<url-pattern>", method = RequestMethod.GET)

In Spring we do this with newer way as:

                @GetMapping(value="<url-patten>")

It will work for all method type i.e. POST, PUT, DELETE and so on…

We will cover both @Controller and @RestController below:

1. Making Controller using @Controller:

Now we are going to create a SpringMVC project using SpringBoot with Gradle build tool.

spring mvc app

This is our project structure-

Project structure spring mvc

Gradle build Script:

SpringMVC File

This the starting place of our application. Its looks very simple and similar to normal java main method, but @SpringBootApplication contains all spring magic behind the scene. No Server configuration, no xml file configuration, SpringBoot will handle all things behind the scene.

SpringMVCApp.java

Employee.java File

This a model or POJO class which represents the employee and contains all employee related data. Each object of this class will represent an employee of real world.

EmployeeController.java File

This is our SpringMVC controller class about which we discussed above. Any class annotated with @Controller with in the class path of Spring boot will treat as SpringMVC controller.

You will notice here that all the methods are dealing with View.

Adding new Employee:

Add employee spring mvc

add employee spring mvc

Showing all Employees:

showing employees spring mvc

Updating Employee:

updating employees spring mvc

updating employees spring mvc 2

Now, let us see how to use RestController.

2. Making Controller using @RestController:

Now we are going to create a Rest based SpringMVC project using SpringBoot with Gradle build tool.

This is our project structure-

project structure rest based

This is how we will be getting the JSON data:

spring mvc json data

This is how we will be getting the XML data:

spring mvc xml data

This is how we will be sending the data:

spring mvc sending data

SpringRestApp.java

This the starting place of our application. It contains the main() method.

EmployeeController.java

This is the SpringMVC Controller class having @RestController which is created in order to save the new employee information, update the existing data for the employee, and deleting any information of an employee. It has the

It can also easily retrieve the details of the employees in any format you want like JSON Format or XML format. Employee details can be searched easily using his employee id.

Employee.java

This a model or POJO class which represents the employee and contains all employee related data. Each object of this class will represent an employee of real world.

Conclusion

So, this is how you can implement Spring MVC Controllers. I hope you find them useful.  Suggestions are welcome in comments.

Leave a Reply