使用@RequestMapping进行Restful api调用时如何处理404

时间:2015-03-25 16:11:00

标签: spring

我是春天mvc的@RequestMapping和@Pathvariable。我在控制器中有以下代码

@Controller
@RequestMapping("/api/addOwners")
@ResponseBody
public class MyLibraryController {
    @RequestMapping(value="`enter code here`/owners/{ownerId}", method=RequestMethod.GET)
    public String findOwner(@PathVariable String ownerId) {
    String owner = ownerService.addOwner(ownerId);
    return "successful";
}

使用此网址http://localhost:8080/api/addOwners/owners/7时,代码可以正常工作,但会为此http://localhost:8080/api/addOwners/owners/返回404。基本上价值没有通过。

如何处理此错误?

1 个答案:

答案 0 :(得分:0)

您需要在Spring控制器中明确处理异常。有不同的方法来处理它,但我更喜欢使用自定义方法来处理它     控制器在 @ExceptionHandler @ResponseStatus 注释的帮助下出错。请仔细阅读以下链接

Spring REST: Exception handling vol. 1

希望这些信息能为您提供帮助:)