Sandeep Manne
Sandeep Manne

Reputation: 6092

Spring MVC Send HTML & JSON

I need to develop an web application in spring mvc, which can respond to client in html, json and xml by taking input parameters, how can I design this using spring mvc, I know using @ResponseBody I can get xml or json response, but how to get html response, I need to give different methods for html and json or with same method it is possible?

Example:
http://blip.tv/file/6213507?skin=json - Gives json o/p

http://blip.tv/file/6213507?skin=api - Gives xml o/p

http://blip.tv/file/6213507 - Gives html o/p

Upvotes: 4

Views: 4852

Answers (2)

Sandeep Manne
Sandeep Manne

Reputation: 6092

I found a great example on doing this using ContentNegotiatingViewResolver

Upvotes: 5

Avinash T.
Avinash T.

Reputation: 2349

You can write your own view class by extending org.springframework.web.servlet.view.AbstractView

And in this class override following method -

protected void renderMergedOutputModel(Map model,
                                   HttpServletRequest request,
                                   HttpServletResponse response)
                            throws Exception 

And set content type like response.setContentType("text/plain; charset=ISO-8859-1"); as per skin parameter

Upvotes: 0

Related Questions