JBoss 4.0 中使用web service

2008-02-23 09:45:37来源:互联网 阅读 ()

新老客户大回馈,云服务器低至5折

大致过程

1. 安装Jboss
2. 将Webservice endpoint当成Servlet
3. 写一个远端的interface
4. 在Eclipse中配置一个external tool:wscompile
4. 写一个config.XML, 然后用JWSDP的JAXRPC中的wscompile生成mapping.xml和wsdl
5. 必须存在4个文件:web.xml,webservices.xml,mapping.xml和.wsdl
6. webservices 中的port name 要和.wsdl中的一致
7. 使用lomboz或jbossIDE deploy

详细实例:

1. create a class

package service;


public class Hello {
public boolean helloDancy(String hello){
System.out.println(hello);
return true;
}

}

2. create corresponding remote interface

package remote;

import Java.rmi.Remote;
import java.rmi.RemoteException;

public interface HelloRemote extends Remote {
public boolean helloDancy(String hello) throws RemoteException;
}

3. create web.xml in WEB-INF folder

<?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>helloServlet</servlet-name>
<servlet-class>service.Hello</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>helloServlet</servlet-name>
<url-pattern>/hello</url-pattern>
</servlet-mapping>
</web-app>

4. create config.xml

<?xml version="1.0" encoding="UTF-8"?>
<configuration xmlns="http://java.sun.com/xml/ns/jax-rpc/ri/config">
<service name="helloservice"
targetNamespace="/hello"
typeNamespace="/hello/types"
packageName="service">
<interface name="remote.HelloRemote" />
</service>
</configuration>

5. create folder wsdl in WEB-INF/ and run wscompile from jaxrpc folder in sun's jwsdp1.5 package

wscompile -cp output/classes -gen:server -f:documentliteral -mapping jaxrpc-mapping.xml -keep config.xml

-keep: keep the generated source file

6. After executing wscompile.bat, a mapping.xml and helloservice.wsdl are generated

7. create webservice.xml

<?xml version="1.0" encoding="UTF-8"?>

<webservices 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://www.ibm.com/webservices/xsd/j2ee_web_services_1_1.xsd" version="1.1">

<webservice-description>
<webservice-description-name>hello world</webservice-description-name>
<wsdl-file>WEB-INF/wsdl/helloservice.wsdl</wsdl-file>
<jaxrpc-mapping-file>WEB-INF/mapping.xml</jaxrpc-mapping-file>
<port-component>
<port-component-name>hello-dancy</port-component-name>
<wsdl-port>HelloRemotePort</wsdl-port>
<service-endpoint-interface>remote.HelloRemote</service-endpoint-interface>
<service-impl-bean>
<servlet-link>helloServlet</servlet-link>
</service-impl-bean>
</port-component>
</webservice-description>
</webservices>

8. create hello.war and deploy it to jboss4.0.1

9. create test client

public void testHelloDancy() {
try{
String urlstr = "http://w20251-01:8080/hellows/hello?wsdl";

java.net.URL url = new java.net.URL(urlstr);

String ns = "/hello";
QName qname = new QName(ns, "Helloservice");
QName port = new QName(ns, "HelloRemotePort");
QName operation = new QName(ns, "helloDancy");

Service service = new Service();
Call call = (Call)service.createCall();

call.setTargetEndpointAddress( url );
call.setOperationName( operation );
call.addParameter( "String_1", XMLType.XSD_BASE64, ParameterMode.IN);

call.setReturnType( XMLType.XSD_BOOLEAN);
Boolean ret = (Boolean)call.invoke(new Object[]{"hello, Dancy".getBytes()});

} catch(Exception e){
e.printStackTrace();
}


}

10. set the classpath to lead to jars in axis's lib folder

11. customized object which involve byte[] as the input parameter
client code:

public void testHelloDancy() {
try{
String urlstr = "http://w20251-01:8080/hellows/hello?wsdl";

java.net.URL url = new java.net.URL(urlstr);

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:Java模式--工厂模式的简单用例

下一篇:j2ee-j2me tips