mardi 3 mars 2015

Java Spring: How to Get JSON/XML View from Controller on Forward or Redirect?

What I'm Trying to Do


I have a ControllerA that needs to either forward or redirect (redirect preferred) a request with parameters to ControllerB. ControllerB is in a completely different webapp, so aside from the redirect, neither of these controllers know the other exists.


ControllerB returns a view in either JSON or XML. What I want to happen is ControllerA forwards/redirects to ControllerB, and displays the view generated from ControllerB.


I'm doing things this way because requirements given to me state that I should try to avoid integrating anything from ControllerB's app into ControllerA's app. Otherwise I would use a RestTemplate and simply copy ControllerB's view into ControllerA's app.


What I Have So Far


This is ControllerA:



@RequestMapping("/ControllerA")
public ModelAndView ControllerA(
@RequestParam(value = "A", required = true) String A,
@RequestParam(value = "B", required = false) Boolean B)
{
RedirectAttributes redirectAttributes = new RedirectAttributesModelMap();
redirectAttributes.addAttribute("A", A);
redirectAttributes.addAttribute("B", B);

String obj = "redirect:http://{host}:{port}/{appName}/ControllerB?A=A&B=B";
return new ModelAndView(obj);
}


This is ControllerB:



@RequestMapping("/ControllerB")
public ModelAndView ControllerB(
@RequestParam(value = "A") String A,
@RequestParam(value = "B", required = false) Boolean B
)
{
ControllerView cView
= serviceService.service(A, B);

return new ModelAndView("viewResolver", "object", cView);
}


An Example of What I'm Trying to Get Back



<Results A="A" B="B" numResultsReturned="1">
<line data1="ABC" />
</Results>


What's Not Working


The view never gets returned on redirect or forward. Spring reports a forwarded or redirected URL with Status 200, and then tries to resolve to a JSP. What could I be doing wrong?


Most of the questions I've seen on SO and other places on the web seem to use redirect/forward for JSP or HTML pages. I'm just trying to get XML or JSON.


I understand that I may be asking Spring here to do something that it's not meant to do. If that's the case, what are my other options if I want to keep ControllerA and ControllerB as separate from each other as possible?


Aucun commentaire:

Enregistrer un commentaire