一 . 开发一个 JSR-181 POJO Endpoint 的 Web Service 应遵守下面几个步骤:
1> 建立一个 POJO endpoint
import javax.jws.WebMethod;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
@WebService(name = "HelloWorld", targetNamespace = "http://webservice", serviceName = "HelloWorldService")
//targetNamespace , 与包名相同
@SOAPBinding(style = SOAPBinding.Style.RPC)
public class HelloWorldService {
@WebMethod
public String SayHello(String name) {
return name + " 说 : 这是我的第一个 web 服务 ";
}
}
可能有错误,则加入: jboss-jaxws.jar
@WebService 这个注释放置在 Java 类的前面,声明这个类的部分方法可以被发布为 Web 服务。
@WebService 的属性用于设置 Web 服务被发布时的一些配置信息,常用的属性说明如下
1. name
Web 服务的名字, WSDL 中 wsdl:portType 元素的 name 属性和它保持一致,默认是 Java 类或者接口的名字。
2. serviceName
Web 服务的服务名, WSDL 中 wsdl:service 元素的 name 属性和它保持一致,默认是 Java 类的名字 + ” Service ” 。
3. targetNamespace
WSDL 文件所使用的 namespace ,该 Web 服务中所产生的其他 XML 文档同样采用这个作为 namespace 。
@SOAPBinding() 表示这个服务可以映射到一个 SOAP 消息中。 Style 用于指定 SOAP 消息请求和回应的编码方式。
@WebMethod 这个注释放在需要被发布成 Web 服务的方法前面。
2> 把 endpoint 定义成一个 servlet
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<servlet>
<servlet-name>HelloWorldService</servlet-name>
<servlet-class>webservice.HelloWorldService</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>HelloWorldService</servlet-name>
<url-pattern>/HelloWorldService/*</url-pattern>
</servlet-mapping>
</web-app>
3> 把 endpoint 打包成一个 Web 应用 (war 文件 )
进入所在目录:打成 war 包 : jar cvf TestWS.war *
4> 测试
输入: http://localhost:8080/jbossws/ 进入 JbossWS 的查看界面
二 .web service 客户端调用
我们采用 Xfire 进行 webservice 客户端调用 详见: http://blog.csdn.net/pengchua/archive/2007/08/02/1722156.aspx
三.问题解决:
1. 问题
2008-7-25 8:56:37 org.apache.axis.utils.JavaUtils isAttachmentSupported
警告 : Unable to find required classes (javax.activation.DataHandler and javax.mail.internet.MimeMultipart). Attachment support is disabled.
AxisFault
faultCode: { http://schemas.xmlsoap.org/soap/envelope/}Server.userException
faultSubcode:
faultString: org.xml.sax.SAXParseException : Premature end of file.
faultActor:
faultNode:
faultDetail:
{ http://xml.apache.org/axis/}stackTrace:org.xml.sax.SAXParseException : Premature end of file.
at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(Unknown Source)
at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.fatalError(Unknown Source)
解决:放入 activation.jar 和 mail.jar
2 . 问题: : jboss 控制台上出现错误 Caused by: java.lang.UnsupportedOperationException: setProperty must be overridden by all subclasses of SOAPMessage
| at javax.xml.soap.SOAPMessage.setProperty(SOAPMessage.java:424)
解决: This usually shows up when running with JDK 6. If that is the case, either use JDK 5, 也就是将 %JAVA_HOME% 变成 jdk1.5. or see http://wiki.jboss.org/wiki/JBossWithJDK6?action=e&windowstate=normal&mode=view