Spring MVC控制器继承覆盖处理程序方法

时间:2012-10-03 18:34:45

标签: spring inheritance spring-mvc controller

我有一个配置了/ session_auth_uri会话身份验证的控制器,如下所示:

@Controller
@RequestMapping("/session_auth_uri")
public class BaseController {

  @RequestMapping(value = "/do", method = RequestMethod.POST, consumes =    "application/json", produces = "application/json")
  @ResponseBody
  public Result do(@RequestBody Query query) throws SomeException {
    return new Result();
  }

  @RequestMapping(value = "/exec", method = RequestMethod.POST, consumes =    "application/json", produces = "application/json")
  @ResponseBody
  public Result exec(@RequestBody Query query) throws SomeException {
    return new Result();
  }
}

我想扩展BaseController以重用1 handler method => do(...)(基本认证用于/ basic_auth_uri):

@Controller
@RequestMapping("/basic_auth_uri")
public class SubclassController extends BaseController {
}

问题是我想在访问/ basic_auth_uri / exec时为exec(...)处理程序方法返回HttpStatus.NOT_IMPLEMENTED。

感谢您的任何建议。

1 个答案:

答案 0 :(得分:1)

除了在SubclassController中覆盖它之外,别想其他任何方式:

@Controller
@RequestMapping("/basic_auth_uri")
public class SubclassController extends BaseController {
  @RequestMapping(value = "/exec", method = RequestMethod.POST, consumes =    "application/json", produces = "application/json")
  @ResponseStatus(HttpStatus.NOT_IMPLEMENTED)
  public Result exec(@RequestBody Query query) throws SomeException {
    return null;
  }

}