为什么我不能创建 Web 服务提供者基类?



我正在尝试为我的各种 REST API 创建一个基类。

如果我创建一个没有基类的类,如下所示,那么这工作正常(我的重构起点也是如此(:

@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...
          }
    }
}

但是,如果我使该类扩展BaseRestAPI并尝试将所有注释和注释对象和方法移动到基类中,则会出现错误:

@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());           
    }

这没有给我编译错误,但在运行时:

线程"main"中的异常 java.lang.IllegalArgumentException: class SpecificRestAPI 既没有@WebService也没有@WebServiceProvider注释

基于这个错误,我随后尝试将该注释移动到 SpecificRestAPI 类中,保留 Base 类的其余部分如上; 但随后我收到一个 eclipse 编译器错误,指出我没有实现Provider - 但我只是在基类中......

这是以前有人做过的事情吗?如果是的话,怎么做?

子类不会从父类继承注释 - 因此需要在子类中重复注释。

最新更新