java web service

1.编写服务代码

服务代码提供了两个函数,分别为sayHello和sayHelloToPerson,源代码如下:

 1 /*
 2  * File name: HelloService.java
 3  * 
 4  * Version: v1.0
 5  * 
 6  * Created on Aug 2, 2008 9:40:20 AM
 7  * 
 8  * Designed by Stephen
 9  * 
10  * (c)Copyright 2008
11  */
12 package com.sinosoft.webservice;
13 
14 /**
15  * @author Stephen
16  * 
17  * Test web service
18  */
19 public class HelloService {
20     /**
21      * 不带参数的函数
22      * 
23      * @return 返回Hello字符串
24      */
25     public String sayHello() {
26         return "Hello";
27     }
28 
29     /**
30      * 带参数的函数
31      * 
32      * @param name
33      *            名称
34      * @return 返回加上名称的欢迎词
35      */
36     public String sayHelloToPerson(String name) {
37         if (name == null || name.equals("")) {
38             name = "nobody";
39         }
40         return "Hello " + name;
41     }
42 }

 

2.发布webservice

要将上边写的HelloService类发布为WebService,需要先搭建Web应用。下面是在Tomcat下使用Axis创建WebService服务的例子。

在Tomcat下创建Web应用:

注释:免安装版tomcat的启动方式:

添加一个CATALINA_HOME变量 变量值为 D:\Program Files\apache-tomcat-5.5.20

然后点击 tomcat的bin下的startup.bat 就应该可以启动tomcat了

1. 在Tomcat的webapps下创建如下文件系统

          ws

               WEB-INF

                    lib

                    classes

WEB-INF文件目录结构:

 

2. 在WEB-INF文件夹下创建web.xml文件,该文件的内容如下:

 <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web
 Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd">
 
 <web-app>
   <display-name>Apache-Axis</display-name>
     
     <listener>
         <listener-class>org.apache.axis.transport.http.AxisHTTPSessionListener</listener-class>
     </listener>
     
   <servlet>
     <servlet-name>AxisServlet</servlet-name>
     <display-name>Apache-Axis Servlet</display-name>
     <servlet-class>
         org.apache.axis.transport.http.AxisServlet
     </servlet-class>
   </servlet>
 
   <servlet>
     <servlet-name>AdminServlet</servlet-name>
     <display-name>Axis Admin Servlet</display-name>
     <servlet-class>
         org.apache.axis.transport.http.AdminServlet
     </servlet-class>
     <load-on-startup>100</load-on-startup>
   </servlet>
 
   <servlet>
     <servlet-name>SOAPMonitorService</servlet-name>
     <display-name>SOAPMonitorService</display-name>
     <servlet-class>
         org.apache.axis.monitor.SOAPMonitorService
     </servlet-class>
     <init-param>
       <param-name>SOAPMonitorPort</param-name>
       <param-value>5001</param-value>
     </init-param>
     <load-on-startup>100</load-on-startup>
   </servlet>
 
   <servlet-mapping>
     <servlet-name>AxisServlet</servlet-name>
     <url-pattern>/servlet/AxisServlet</url-pattern>
   </servlet-mapping>
 
   <servlet-mapping>
     <servlet-name>AxisServlet</servlet-name>
     <url-pattern>*.jws</url-pattern>
   </servlet-mapping>
 
   <servlet-mapping>
     <servlet-name>AxisServlet</servlet-name>
     <url-pattern>/services/*</url-pattern>
   </servlet-mapping>
 
   <servlet-mapping>
     <servlet-name>SOAPMonitorService</servlet-name>
     <url-pattern>/SOAPMonitor</url-pattern>
   </servlet-mapping>
 
  <!-- uncomment this if you want the admin servlet -->
  <!--
   <servlet-mapping>
     <servlet-name>AdminServlet</servlet-name>
     <url-pattern>/servlet/AdminServlet</url-pattern>
   </servlet-mapping>
  -->
 
     <session-config>
         <!-- Default to 5 minute session timeouts -->
         <session-timeout>5</session-timeout>
     </session-config>
 
     <!-- currently the W3C havent settled on a media type for WSDL;
     http://www.w3.org/TR/2003/WD-wsdl12-20030303/#ietf-draft
     for now we go with the basic 'it's XML' response -->
   <mime-mapping>
     <extension>wsdl</extension>
      <mime-type>text/xml</mime-type>
   </mime-mapping>
   
 
   <mime-mapping>
     <extension>xsd</extension>
     <mime-type>text/xml</mime-type>
   </mime-mapping>
 
   <welcome-file-list id="WelcomeFileList">
     <welcome-file>index.jsp</welcome-file>
     <welcome-file>index.html</welcome-file>
     <welcome-file>index.jws</welcome-file>
   </welcome-file-list>
 
 </web-app>

 

在上面的web.xml中主要是配置了axis的相关配置。

3 .axis的相关配置

在上述的web.xml文件中已经对axis进行了配置,但是还需要进行额外的配置。

复制axis相关的jar文件

 将axis的相关jar文件复制到WEB-INF\lib文件夹下。这些文件包括:

 

复制WebService服务主文件

     将HelloService.java编译后的class文件复制到WEB-INF\classes文件夹下,也就是说在WEB-INF\classes文件夹下的文件夹结构为:com\sinosoft\webservice,在webservice文件夹下有一个helloservice.class文件。

测试发布的Web应用

启动Tomcat服务,打开IE浏览器 输入 http://localhost:8080/ws/services   如果看到如下界面就说明AXIS部署成功了。

                      

4.发布WebService服务

    创建deploy.wsdd文件

<?xml version="1.0" encoding="UTF-8"?>
<deployment xmlns="http://xml.apache.org/axis/wsdd/" xmlns:java="http://xml.apache.org/axis/wsdd/providers/java">
    <service name="HelloServices" provider="java:RPC">
        <parameter name="className" value="com.sinosoft.webservice.HelloService"/>
        <parameter name="allowedMethods" value="*"/>
    </service>
</deployment>

 

 

将此文件放置在WEB-INF目录下。

启动DOS,将目录切换到WEB-INF目录下,输入如下命令:

java -Djava.ext.dirs=lib org.apache.axis.client.AdminClient -l http://localhost:8080/services/AdminService deploy.wsdd

如下显示,则发布成功

 

  -l后的参数是本地要发布WebService的AdminService对应的访问地址。

     最后deploy.wsdd是对应的配置文件名称。

测试:输入 http://localhost:8080/ws/services 如果显示如下,则发布成功:

从上图可以看出,发布成功后,多了一个HelloServices的服务。这样就说明HelloService发布成功了。

5.用Java调用WebService实例

下面为用java编写的调用webservice的例子

需要引用的包为:

package com.test;

import java.io.IOException;
import java.net.MalformedURLException;
import java.rmi.RemoteException;

import javax.xml.namespace.QName;
import javax.xml.rpc.ServiceException;

import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class Test {
    private static final String HELLO_SERVICE_ENDPOINT = "http://localhost:8080/ws/services/HelloServices?wsdl";
    private static final Log log = LogFactory.getLog(Test.class);
     public static void main(String[] args) {
         Test tester = new Test();
            // tester.callSayHello();
         tester.callSayHelloToPerson();
        }
    public void callSayHello() throws ServiceException, MalformedURLException{
        Service service = new Service();
         Call call = (Call) service.createCall();
         call.setTargetEndpointAddress(new java.net.URL(HELLO_SERVICE_ENDPOINT));
         call.setOperationName(new QName("http://webservice.sinosoft.com/",
                 "sayHello"));
         call.setReturnType(org.apache.axis.Constants.XSD_STRING);
         try {
            String ret = (String) call.invoke(new Object[] {});
            System.out.println("The return value is:" + ret);
            return;
        } catch (RemoteException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    public void callSayHelloToPerson(){
         try {
                Service service = new Service();
                Call call = (Call) service.createCall();
                call.setTargetEndpointAddress(new java.net.URL(HELLO_SERVICE_ENDPOINT));
                call.setOperationName(new QName("http://webservice.sinosoft.com/",
                        "sayHelloToPerson"));
                call.addParameter("name", org.apache.axis.Constants.XSD_STRING,
                        javax.xml.rpc.ParameterMode.IN);
                call.setReturnType(org.apache.axis.Constants.XSD_STRING);
                try {
                    String ret = (String) call.invoke(new Object[] { "Stephen" });
                    System.out.println("The return value is:" + ret);
                    return;
                } catch (IOException e) {
                    e.printStackTrace();
                }
            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (ServiceException e) {
                e.printStackTrace();
            }
            log.error("call sayHello service error!");
        }
}

 

posted @ 2014-03-13 22:14  一弦一仙  阅读(708)  评论(0编辑  收藏  举报