Reputation: 4266
I have a controller that extends the MultiActionController and that does not use annotation. Everything is configured by xml.
Is there a way to reproduce this method in a MultiActionController?
@RequestMapping(value = "/products", method = RequestMethod.GET)
public @ResponseBody List<Product> products() {
return product.getList();
}
The problem I'm facing is how to return the list in the @ResponeBody.
My methods looks like these:
public ModelAndView login(HttpServletRequest request,HttpServletResponse response) throws ServletRequestBindingException {
return new ModelAndView("login", model);
}
Upvotes: 1
Views: 3483
Reputation: 1
public ModelAndView products(HttpServletRequest request,HttpServletResponse response) {
final List<Product> products = product.getList();
return new ModelAndView(new AbstractView() {
@Override
protected void renderMergedOutputModel(Map<String, Object> model, HttpServletRequest request, HttpServletResponse response) throws Exception {
ByteArrayOutputStream baos = this.createTemporaryOutputStream();
byte[] b = generateBytes(products)
baos.write(b);
this.writeToResponse(response, baos);
};
private byte[] generateBytes(List<Product> products) {
// TODO Your codes.
}
});
}
Upvotes: 0
Reputation: 4960
Sort of an open ended question, what I mean is, what response type do you want, json, xml, plain text, etc.,
If you are not confined to using this specific controller you could setup a new controller specifically for RESTful responses such as using @ResponseMapping
as you are trying to achieve at the beginning of your question.
You could also go the straight servlet route if you are tied to this specific controller, and don't have the ability to use annotations. For example:
public void generateProductList(HttpServletResponse response) {
OutputStream os = response.getOutputStream();
// This is where you would massage the data into the response type you want
String responseBody = generateResponseBody(productList);
os.write(responseBody.getBytes());
os.flush();
}
I'm assuming if you're writing directly to the response body, this will be used in some sort of AJAX call. If this is the case, I would recommend converting the list into JSON using the Jackson (or similar) library, and using either of the methods I just mentioned to write the response. If JSON isn't an option, you could easily convert the list into a CSV response, then use javascript to parse the CSV to do something with.
Upvotes: 1
Reputation: 43
If you want to get the list back to where it is called from you can do something like this:
public ModelAndView login(HttpServletRequest request,HttpServletResponse response) throws ServletRequestBindingException {
ModelMap model=new ModelMap();
List<Product> productList=product.getList();
model.add("productList",productList);
return new ModelAndView("login", model);
}
For example you have called this from a jsp then you can retrieve the data inside jsp by writing this :
{productList}
Now by using jstl for loop you can iterate the list.
Upvotes: 0