首页 > 解决方案 > java webservice jax-ws 什么时候需要@webmethod

问题描述

我正在尝试理解JAX-WS并且很难理解@WebMethodAnnotation 的目的。

这是示例代码:

@WebService
@SOAPBinding(style = SOAPBinding.Style.DOCUMENT)
public  class TestServiceImpl  {

  public String greet1(String msg) {
    return "greet1";
  }

  @WebMethod
  public String greet3(String msg) {
    return "greet3";
  }

  public String greet5(String msg) {
    return "greet5";
  }

}

当我查看时,WSDL我可以看到所有 3 种方法'greet1'并且'greet3'没有@WebMethod注释。只有 'greet2'有注释。

这是WSDL

<binding name="TestServiceImplPortBinding" type="tns:TestServiceImpl">   
<soap:binding transport="http://schemas.xmlsoap.org/soap/http"  
 style="document"/>
<operation name="greet5"></operation>
<operation name="greet3"></operation>
<operation name="greet1"></operation>
</binding>

所以不确定 WebMethod 注释的用途是什么?

标签: web-services

解决方案


@WebMethod注释由 Web 服务引擎在内部使用,以将任何公共方法公开为 Web 服务操作。正如 JEE 规范中所说:

@WebMethod 指定该方法作为 Web 服务的公共操作公开。你必须显式地使用这个注解来暴露一个方法;

如果不指定此注解,则默认不公开该方法。

但是,根据我的实践,一些应用程序服务器(例如 IBM WebSphere 和一些 WS 引擎)将您的方法公开为 Web 服务操作,即使您没有在方法签名之前放置此类注释。我想这取决于 JAX-WS 实现。

这意味着使用注释注释public的类中的任何方法@WebService都将作为 Web 服务操作公开。为避免这种行为,您应该使用 显式注释方法@WebMethod(exclude=true)


推荐阅读