Search code examples
javaeclipserestjax-ws

Why can't I make a webserviceprovider base class?


I am trying to make a base class for my various REST APIs to be based on.

If I create a class as follows, without a base class, then this works fine (and so is my starting point for the refactoring):

@WebServiceProvider
@ServiceMode(value = javax.xml.ws.Service.Mode.MESSAGE)
@BindingType(value = HTTPBinding.HTTP_BINDING)
public class SpecificRestAPI implements Provider<Source>
{
    // arg 0: url including port, e.g. "http://localhost:9902/specificrestapi"
    public static void main(String[] args)
    {
        String url = args[0];
        // Start
        Endpoint.publish(url, new SpecificRestAPI());       
    }
    
    @Resource
    private WebServiceContext wsContext;
    
    
    @Override
       public Source invoke(Source request)
       {
          if (wsContext == null)
             throw new RuntimeException("dependency injection failed on wsContext");
          MessageContext msgContext = wsContext.getMessageContext();
          switch (((String) msgContext.get(MessageContext.HTTP_REQUEST_METHOD)).toUpperCase().trim())
          {
             case "DELETE": 
                 return processDelete(msgContext);
//etc...
          }
    }
}

However, if I make that class extend BaseRestAPI and try and move all annotations and annotated-objects and methods into the base class, I get an error:

@WebServiceProvider
@ServiceMode(value = javax.xml.ws.Service.Mode.MESSAGE)
@BindingType(value = HTTPBinding.HTTP_BINDING)
public abstract class BaseRestAPI  implements Provider<Source>
{
    @Resource
    private WebServiceContext wsContext;
    
    
    @Override
       public Source invoke(Source request)
       {
//etc...
       }
}

public class SpecificRestAPI extends BaseRestAPI
{
    // arg 0: url including port, e.g. "http://localhost:9902/specificrestapi"
    public static void main(String[] args)
    {
        String url = args[0];
        // Start
        Endpoint.publish(url, new SpecificRestAPI());           
    }

This gives me no compilation errors, but at run-time:

Exception in thread "main" java.lang.IllegalArgumentException: class SpecificRestAPI has neither @WebService nor @WebServiceProvider annotation

Based on this error, I then tried moving that annotation into the SpecificRestAPI class, leaving the rest of the Base class as above; but then I get an eclipse compiler error that I'm not implementing the Provider - but I am, just in the base class...

Is this something anyone's done before; and if so how?


Solution

  • Annotations aren't inherited by child classes from parent classes - so the annotations need to be repeated in the child class.