• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

Writing a Restful API as well as Returning Model and View in Spring MVC

 
Greenhorn
Posts: 5
Spring Redhat Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well my concern goes like this,
The basics of Restful API's say that whenever you use CRUD operations you need to use GET for Create, POST for Save, PUT for Edit and DELETE for Delete.
Now suppose a programmer is writing CRUD Operations methods in Controller and wants output as JSON Strings for that particular URI then he has to return JSON.
But incase of Spring MVC we could return ModelAndView object which in my case when i see through a browser gives an HTML Output for a sample CRUD URI.

How could both the ways be achieved such that i could return a ModelAndView for binding variables onto the JSP Elements and also when the same URI is accessed then it would return a JSON/XML response.

i was reading the same on a very useful blog (I''m not sure whether pasting external links are allowed here.) http://www.javatechtipssharedbygaurav.com/2013/04/rest-web-service-with-crud-operations.html
The motive here is achieved but i failed to understand the logic behind it.

Also what are the corresponding Json frameworks packages like we have for XML in javax.xml.bing.annotations

Thank You !
 
Bartender
Posts: 1210
25
Android Python PHP C++ Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

The basics of Restful API's say that whenever you use CRUD operations you need to use GET for Create, POST for Save, PUT for Edit and DELETE for Delete.


GET is to read or fetch.
POST is for create or update if operation is not idempotent.
PUT is for create or update if operation is idempotent.

How could both the ways be achieved such that i could return a ModelAndView for binding variables onto the JSP Elements and also when the same URI is accessed then it would return a JSON/XML response.


By using RequestMapping annotations with same URL, but different values for the HTTP method, request content type and response type. See the example below, where all 3 handlers have the same URL
, but Spring routes them differently based on request method and content types.


For this to work, ensure that Jackson JAR is in your WEB-INF/lib.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic