2

在升级我的应用程序以使用我可用的最新版本的 Spring (3.1.1) 时,我发现我的一个 REST 调用抛出 404 错误(当它曾经非常成功时)。我已经验证切换回旧库 (3.0.3) 可以正常工作,并且切换回新库失败,所有这些都没有更改我的代码。

@Controller
@RequestMapping("/group/{groupId}/template")
public class TemplateController extends AbstractController {
    ...

    @RequestMapping(value="/{templateId}", method=RequestMethod.GET) @ResponseBody
    public Template getTemplate(ServletWebRequest request, 
            @PathVariable("groupId") int groupId, 
            @PathVariable("templateId") int templateId) throws Exception {
        ...
    }

    ...

    @RequestMapping(value="/{templateId}", method=RequestMethod.DELETE) @ResponseBody
    public Task getTemplate(ServletWebRequest request, 
            @PathVariable("groupId") int groupId, 
            @PathVariable("templateId") int templateId) throws Exception {
        ...
    }

    ...
}

我把 GET 方法放在那里只是为了比较,它可以工作。但是,当我请求 DELETE 方法时(同样,它曾经可以工作),我在日志中收到一条错误消息:

WARNING: No mapping found for HTTP request with URI [/*appname*/group/1/template/group/1/template/1] in DispatcherServlet with name '*appname*'

现在,错误显然是正确的,我没有该映射的任何 URI,但为什么它试图找到该映射而不是指定的映射 ( /*appname*/group/1/template/1)?

4

2 回答 2

0

它将值中的 / 视为根尝试通过删除方法 @RequestMapping 的值中的 / 来进行映射。

@RequestMapping(value="{templateId}", method=RequestMethod.DELETE) @ResponseBody
    public Task getTemplate(ServletWebRequest request, 
            @PathVariable("groupId") int groupId, 
            @PathVariable("templateId") int templateId) throws Exception {
        ...
    }
于 2012-08-16T22:43:06.153 回答
0

我仍然不确定为什么会出现错误,因此对 Spring 的内部工作原理的深入了解将不胜感激,但我确实找出了导致它的原因。该方法与其他方法的唯一不同之处在于它抛出了异常,而其他方法则没有。在我注释掉我的 ExceptionHandler 后,该函数已正确注册。

@ExceptionHandler(Exception.class)
public Map<String, String> handleExceptions(Exception e) {
    ...
}

我仍然不知道为什么这会导致它出错,但具有讽刺意味的是,这就是我首先升级库的原因:以便异常处理能够正常工作。在我添加@ResponseBody到函数之后(我本来打算这样做),一切正常。

于 2012-08-17T15:06:05.310 回答