【问题标题】:Why can't I make a webserviceprovider base class?为什么我不能创建 webserviceprovider 基类?
【发布时间】:2019-07-24 14:33:02
【问题描述】:

我正在尝试为我的各种 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 中的异常:类 SpecificRestAPI 既没有 @WebService 也没有 @WebServiceProvider 注释

基于这个错误,然后我尝试将该注释移动到SpecificRestAPI 类中,而将 Base 类的其余部分保留在上面;但后来我得到一个 Eclipse 编译器错误,我没有实现 Provider - 但我只是在基类中......

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

【问题讨论】:

    标签: java eclipse rest jax-ws


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 2021-03-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-24
      • 2019-12-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多