我正在努力理解JAX-WS
,但很难理解@WebMethod
注释的目的。
这是示例代码:
@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
时,我可以看到'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注释的用途是什么?
@WebMethod
注释由web服务引擎内部使用,以将任何公共方法公开为web服务操作。正如JEE规范中所说:
@WebMethod指定该方法公开为Web的公共操作服务必须显式使用此注释来公开方法;
如果未指定该注释,则默认情况下该方法不是暴露。
但是,根据我的实践,一些应用程序服务器(例如IBM WebSphere和一些WS引擎(将您的方法公开为web服务操作,即使您没有在方法签名之前放置这样的注释。我想这取决于JAX-WS的实现。
这意味着用@WebService
注释注释的类中的任何public
方法都将作为web服务操作公开。为了避免这种行为,您应该使用@WebMethod(exclude=true)
显式注释方法。