复制Spring Controller方法@RequestMapping for Restful

时间:2015-10-09 14:25:31

标签: ajax spring spring-mvc controller

如何为普通和休息/ ajax请求提供相同的网址。我有以下代码,我的Ajax调用总是接收普通的html而不是JSON。

@RequestMapping(value="/customer/{customerID}/site/{siteID}", method=RequestMethod.GET, headers="Accept=application/json")
public @ResponseBody Site getSiteJson(HttpServletResponse response, Model model, @PathVariable("customerID") String customerID, @PathVariable("siteID") String siteID) throws IOException, InterruptedException
{
    try
    {
        return dao.getSite(customerID, siteID);
    }
    catch (NuviaError nuviaError)
    {
        response.sendError(500, nuviaError.getMessage());
        return null;
    }
}

@RequestMapping(value="/customer/{customerID}/site/{siteID}", method=RequestMethod.GET)
public String getSite(Model model, @PathVariable("customerID") String customerID, @PathVariable("siteID") String siteID) throws IOException, InterruptedException
{
    Site s;
    try
    {
        s = dao.getSite(customerID, siteID);
    }
    catch (NuviaError e)
    {
        ControllerUtils.addError(model, "Error fetching site details: " + e.getMessage());
        return getCustomer(model, customerID);
    }
    return showSite(model, customerID, s, "", "PUT", "Save site details", true);
}

即使禁用第二种方法也无效。

1 个答案:

答案 0 :(得分:0)

在我看来,您没有在回复中设置内容类型

@RequestMapping(value="/customer/{customerID}/site/{siteID}", method=RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE )
public @ResponseBody Site getSiteJson(HttpServletResponse response,  @PathVariable("customerID") String customerID, @PathVariable("siteID") String siteID) throws IOException, InterruptedException

无论如何在这里复制模型模型有点令人困惑,你甚至没有使用它,所以我也会摆脱它