views:

476

answers:

1

I have mapped one of my method in one Controller to return JSON object by @ResponseBody.

@RequestMapping("/{module}/get/{docId}")
public @ResponseBody Map<String, ? extends Object> get(@PathVariable String module,
        @PathVariable String docId) {
    Criteria criteria = new Criteria("_id", docId);
    return genericDAO.getUniqueEntity(module, true, criteria);
}

However, it redirects me to the JSTLView instead. Say, if the {module} is product and {docId} is 2, then in the console I found:

DispatcherServlet with name 'xxx' processing POST request for [/xxx/product/get/2] Rendering view [org.springframework.web.servlet.view.JstlView: name 'product/get/2'; URL [/WEB-INF/views/jsp/product/get/2.jsp]] in DispatcherServlet with name 'xxx'

How can that be happened? In the same Controller, I have another method similar to this but it's running fine:

@RequestMapping("/{module}/list")
public @ResponseBody Map<String, ? extends Object> list(@PathVariable String module,
        @RequestParam MultiValueMap<String, String> params,
        @RequestParam(value = "page", required = false) Integer pageNumber,
        @RequestParam(value = "rows", required = false) Integer recordPerPage) {
...

    return genericDAO.list(module, criterias, orders, pageNumber, recordPerPage);
}

Above do returns correctly providing me a list of objects I required. Anyone to help me solve the mystery?

+1  A: 

If a controller method returns null, Spring interprets that as saying that you want the framework to render the "default view".

It would be better, I think, that when the method is @RequestBody-annotated, this logic should not apply, but perhaps that's hard to implement - how would it handle a null return from a method that normally returns XML, for example?

Anyway, to stop this from happening, you need to make sure you return something, like an empty Map.

skaffman