JBoss 4.0 中使用web service

类别:Java 点击:0 评论:0 推荐:

大致过程

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);

         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();
         QName    qn      = new QName( "/hello/types", "Dto" ); //refer to .wsdd
         call.registerTypeMapping(service.Dto.class, qn,
                       new org.apache.axis.encoding.ser.BeanSerializerFactory(service.Dto.class, qn),       
                       new org.apache.axis.encoding.ser.BeanDeserializerFactory(service.Dto.class, qn));       

            call.setTargetEndpointAddress( url );
            call.setOperationName( operation );
            call.addParameter( "arg0", qn, ParameterMode.IN);
           
            call.setReturnType( XMLType.XSD_BOOLEAN);
            service.Dto dto = new Dto();
            dto.setParam("hello, Dancy".getBytes());
         Boolean ret = (Boolean)call.invoke(new Object[]{dto});

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


本文地址:http://com.8s8s.com/it/it9749.htm