Announcement Announcement Module
Collapse
No announcement yet.
How to return JSON/XML using MultiActionController? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • How to return JSON/XML using MultiActionController?

    here's the MultiActionController
    Code:
    public class SimulationController extends MultiActionController {
    
    	private SimulationService simulationService;
    
    	/**
    	 * @param simulationService the simulationService to set
    	 */
    	@Autowired
    	public void setSimulationService(SimulationService simulationService) {
    		this.simulationService = simulationService;
    	}
    	
    	public ModelAndView changeSimulationMode(HttpServletRequest request, HttpServletResponse response) 
    		throws Exception {
    		
    		Object objMode = request.getParameter("mode"); 
    		String strMode = (String) objMode;
    		
    		SimulationMode simulationMode = new SimulationMode();
    		simulationMode.setSimulationMode(strMode);
    		
    		simulationService.execChangeSimulationMode(simulationMode);
    		
            String json = "";
            try {
            	
            	json = "{" +
    					   "\"success\":true" +
    					"}";
    
            } catch (Exception e) {
                response.setHeader("Errored", "true");
                response.setHeader("ErrorType", "system");
                response.setHeader("ErrorMessage", e.getMessage());
                json = "{success:false,errors:{reason:'error'}}";
            }
            
            response.setContentType("text/json; charset=utf-8");
            response.setHeader("Cache-Control", "no-cache");
            PrintWriter pw = response.getWriter();
            pw.write(json);
            pw.close();
            return null;
    	}
    	
    }
    Successfully called the changeSimulationMode method but when it returns to browser... it will popup a download dialog to download the JSON content... like when downloading a file..

    I've heard that Spring MVC 2.5.6 and SWF2.0.8 provides JSON and XML view..

    Could you guide me on how to implement the above..
    thanks

  • #2
    set content type to make the response "browser aware" and it should be fine!

    Comment


    • #3
      I am setting the content type already,... that's why it looks strange...

      Comment


      • #4
        check the response using livehttpdheader firefox plugin. my guess is the response type is still not d right one!

        note that ur are setting content type in response obj like,
        response.setContentType("text/json; charset=utf-8");

        rather try setting it in json obj since json's obj new constructor sets ct to default "app/json"

        also try somethin like JsonView jv = new JsonView();
        Last edited by shivarajan; Mar 10th, 2010, 05:07 AM.

        Comment


        • #5
          another way -

          1. In MultiActionController you will have getJSON() which will return JSON string

          Code:
          public ModelAndView getJSON(HttpServletRequest request, HttpServletResponse response) throws Exception {
          		// following service return JSON string
          		String readContentString = contentService.readContent();
          		return new ModelAndView("ContentJsonPage","contentJson", readContentString);
          		
          	}
          2. In contentJSONPage.jsp

          Code:
          <%@ taglib uri="http://java.sun.com/jstl/core" prefix="c" %>
          <c:out value="${contentJson}" escapeXml="false"></c:out>

          This way it will return JSON

          Comment


          • #6
            A simple response.setContentType("text/html"); does the trick.

            Best Regards,

            Ashish.

            Comment

            Working...
            X