Rahul Jain
Rahul Jain

Reputation: 658

Separation of Spring MVC Restful application & Rest web service

I have read some article regarding Spring MVC & Rest web services. But I have one confusion to implement web application using spring mvc.

My requirement is to create a spring based web application and creating Rest web services that will be used by device.

eg. I want to get list of objects.

  1. This list will be displayed on a jsp page (for web app)
  2. List of objects will be return as json (for remote device)

I am following this approach to achieve this.


    //For remote device - return json data
    public List getObjectsInJson() {
        return myService.getAllSomeObjects();
    }

And


    // Method for getting the view data
    public ModelAndView getObjectsInModelNAndView() {
         // Return ModelAndView
    }

Please suggest this approach is right or should I create separate controller for both purpose ? or any other approach ?

Upvotes: 3

Views: 1935

Answers (4)

stackh34p
stackh34p

Reputation: 9009

From my experience in ASP.NET MVC (wich is compareble to what you are using) and also with the Spring's restful API, I would recommend you to use separate controllers.

The Web API (referring to the API that you expose with JSON to devices and so) is actually a standard form of data representation and transfer. It is not limited to certain devices and any web application may benefit from such an API. For instance, you can create a pure html5 application and consume the API with javascript. In short, this is a frontend-agnostic approach to some extend.

On the other hand, the classic MVC approach, that uses controllers and view objects is more tightly coupled with the front-end technology of choice (Spring MVC + JSP in your case). The results and communication depend on that particular set of technoligies, and it is a good decision to keep these separate.

An example scenario to justify such a separation is, for instance, if you need to target a separate UI technology (like a native application on a mobile device) or you want to drop the JSP + Spring MVC frontend in favor of something else. If you maintain a single controller for your Web API and MVC, you will have to make changes to that controller, and potentially harm working logic of the Web API. Keeping these separate, would allow you to drop the MVC stuff without affecting the Web API. At best, you could even split these into two separate web projects.

Upvotes: 1

Serge Ballesta
Serge Ballesta

Reputation: 148965

It seems this is not the most common scenario, put provided your jsp views only show what will be in json(*), you can let your controller fully agnostic of the actual format.

Spring MVC ContentNegotiatingViewResolver will delegate to the relevant actual view resolver depending on the file extension in the URI (xxx.json instead of xxx) or on the Accept HTTP request header.

It is a strict application of separation of concern in which the controller fully delegates presentation to a (more complex) view layer. The interest is that adding other presentations (pdf, xls, etc) will require little modification to the application : add a view resolver (optionally a view class) and configure the ContentNegotiatingViewResolver to use it.

(*) this can be simpler to achieve with Tiles or other layout based frameworks

Upvotes: 0

Bart
Bart

Reputation: 17361

The application and service should be two separate projects and you initialize each with their own servlet. It's nice and all to have them combined but they have nothing in common. Your service provides the data. Your application simply consumes the API of the service. In your examples you consume the Java API for your service which makes having a REST web service rather pointless.

  1. Create your REST web service
  2. Create your application which consumes your service API over HTTP rather then using it's Java API.

Upvotes: 0

shazin
shazin

Reputation: 21883

Best method is creating a single service and having two different Controllers.

One is for standard ModelAndView Controller (@Controller annotation) and the Other one Rest Controller (@RestController annotation).

Or you can have Rest Controller alone and in your JSP can use AJAX call to get the list of JSON objects and process it and show.

Upvotes: 1

Related Questions