Spring MVC Abstract Controller :: Cómo obtener la firma exacta del método al implementar una interfaz de controlador abstracto

I am using STS 2.8.1 and Spring 3.1.1.RELEASE.

I have an Abstract controller like below

public abstract class SuperController {

@RequestMapping(value = "/Entity", method = RequestMethod.POST, headers = "Accept=application/xml,application/json")
    public abstract @ResponseBody OAuthEntity setOAuthEntity(@RequestBody OAuthEntity oAuthEntity);

@RequestMapping(value = "/Entity/{id}", method = RequestMethod.GET, headers = "Accept=application/xml,application/json")
    public abstract @ResponseBody OAuthEntity getOAuthEntity(@PathVariable("id") Long id);
}

When I extend the controller in STS it gives me method signature like below

    @Override
public OAuthEntity setOAuthEntity(OAuthEntity oAuthEntity) {
    // TODO Auto-generated method stub
    return null;
}

@Override
public OAuthEntity getOAuthEntity(Long id) {
    // TODO Auto-generated method stub
    return null;
}

I understand that the @RequestMapping are inherited fine and I have also tested the code (it works!!). What I want is that when I extend the super class I should get the @ResponseBody, @PathVariable("id") and @RequestBody as in abstract class.

Of course I could edit it manually (that's what I am doing, but would like a more elegant way) So that the Sub class has method signature like below

    @Override
public @ResponseBody OAuthEntity setOAuthEntity(@RequestBody OAuthEntity oAuthEntity) {

    @Override
public @ResponseBody OAuthEntity getOAuthEntity(@PathVariable("id") Long id) {

It is not killing me but frustrating to have to manually write it. Any help is greatly appreciated.

preguntado el 09 de marzo de 12 a las 13:03

1 Respuestas

Spring 3 did away with the notion of interitance for Controllers. My advice would be to ditch the abstract Controller idea and simply add those annotations to controllers.

Inheritance doesn't make any sense in this context. You want one and only one controller method to be handling a given request mapping in your application. There's no need for a parent/child relationship, in my opinion. I'd advise that you move those annotations into appropriate controller implementations and remove your abstract class.

respondido 09 mar '12, 13:03

I wanted to have the same method signatures across multiple controllers hence the parent/child relationship. In that way I can have different implementations for different contexts, e.g. super controller is OauthController and children are /google, /facebook etc.. - user1259352

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.