利用Java编写简单的WebService实例
使用Axis编写WebService比較简单,就我的理解,WebService的实现代码和编写Java代码事实上没有什么差别,主要是将哪些Java类公布为WebService。
以下是一个从编写測试样例到公布WebService,以及编写測试代码的过程介绍。
本样例的WebService提供了两个方法。各自是sayHello和sayHelloToPerson。第一个仅仅是返回一个"Hello"字符串,没有參数,第二个函数接受一个字符串作为參数。返回"Hello 參数值",该样例比較简单。可是清楚的说明了从编写代码到公布为WebService以及測试编写好的WebService全过程。
编写服务代码
服务代码提供了两个函数。分别为sayHello和sayHelloToPerson,源码例如以下:
- /*
- * File name: HelloService.java
- *
- * Version: v1.0
- *
- * Created on Aug 2, 2008 9:40:20 AM
- *
- * Designed by Stephen
- *
- * (c)Copyright 2008
- */
- package com.sinosoft.webservice;
- /** *//**
- * @author Stephen
- *
- * Test web service
- */
- public class HelloService {
- /** *//**
- * 不带參数的函数
- *
- * @return 返回Hello字符串
- */
- public String sayHello() {
- return "Hello";
- }
- /** *//**
- * 带參数的函数
- *
- * @param name
- * 名称
- * @return 返回加上名称的欢迎词
- */
- public String sayHelloToPerson(String name) {
- if (name == null || name.equals("")) {
- name = "nobody";
- }
- return "Hello " + name;
- }
- }
公布WebService
要将上边写的HelloService类公布为WebService。须要先搭建Web应用。以下是在Tomcat下使用Axis(http://ws.apache.org/axis/)创建WebService服务的样例。
在Tomcat下创建Web应用
在该样例中,在Tomcat下创建了一个context path为ws的WEB应用。
1. 在myeclipse中创建WebServiceTest的webproject
2. 在WEB-INF目录下web.xml文件,该文件的内容例如以下:
- <?xml version="1.0" encoding="ISO-8859-1"?
>
- <!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的相关配置。
axis的相关配置
在上述的web.xml文件里已经对axis进行了配置,可是还须要进行额外的配置。
复制axis相关的jar文件(官网下载axis-bin-1_4.zip 就有例如以下jar包)
将axis的相关jar文件拷贝到WEB-INF\lib目录下。这些文件包含:
activation.jar
axis.jar
axis-ant.jar
commons-discovery-0.2.jar
commons-logging-1.0.4.jar
jaxrpc.jar
log4j-1.2.8.jar
saaj.jar
wsdl4j-1.5.1.jar
測试公布的Web应用
启动Tomcat服务,打开IE浏览器,訪问地址http://127.0.0.1:8080/WebServiceTest/services,假设看到例如以下界面就说明AXIS部署成功了。
注意点:本人当时使用的时候 新建的web项目名字为webservice
公布WebService
公布WebService须要使用现有的AdminService来实现。这里我写了一个批处理文件来公布WebService。以后假设须要公布其它文件,仅仅须要改动对应的參数就能够了。
创建deploy.wsdd文件
文件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>
创建公布WebService服务的批处理文件
批处理文件deploywebservice.bat内容例如以下:
- java -cp axis-ant.jar;axis-schema.jar;axis.jar;commons-discovery-0.2.jar;commons-logging-1.0.4.jar;jaxrpc.jar;log4j-1.2.8.jar;saaj.jar;wsdl4j-1.5.1.jar;xmlsec-1.3.0.jar org.apache.axis.client.AdminClient -lhttp://localhost:8080/WebServiceTest/services/AdminService deploy.wsdd
- pause
当中上面的jar包我都拷到和bat文件在同一个文件夹。如今将全部的jar文件都增加到classpath中进行运行。
-l后的參数是本地要公布WebService的AdminService相应的訪问地址。
最后deploy.wsdd是相应的配置文件名。
公布WebService服务
将deploy.wsdd文件和deploywebservice.bat文件拷贝到同一个文件夹下。运行deploywebservice.bat批处理文件,就能够将deploy.wsdd中描写叙述的Java类公布为WebService。
项目文件文件夹结构
公布完毕之后在訪问http://127.0.0.1:8080/WebServiceTest/services例如以下图所看到的:
从上图能够看出。公布成功后。多了一个HelloServices的服务。这样就说明HelloService公布成功了。
查看HelloServices的wsdl
訪问 wsdl" style="color:rgb(16,138,198)">http://127.0.0.1:8080/WebServiceTest/services/HelloServices? wsdl
- <?
xml version="1.0" encoding="UTF-8"?>
- <wsdl:definitions targetNamespace="http://127.0.0.1:8080/WebServiceTest/services/HelloServices" xmlns:apachesoap="http://xml.apache.org/xml-soap" xmlns:impl="http://127.0.0.1:8080/WebServiceTest/services/HelloServices" xmlns:intf="http://127.0.0.1:8080/WebServiceTest/services/HelloServices" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:wsdlsoap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
- <!--WSDL created by Apache Axis version: 1.4
- Built on Apr 22, 2006 (06:55:48 PDT)-->
- <wsdl:message name="sayHelloToPersonRequest">
- <wsdl:part name="name" type="soapenc:string"/>
- </wsdl:message>
- <wsdl:message name="sayHelloRequest">
- </wsdl:message>
- <wsdl:message name="sayHelloToPersonResponse">
- <wsdl:part name="sayHelloToPersonReturn" type="soapenc:string"/>
- </wsdl:message>
- <wsdl:message name="sayHelloResponse">
- <wsdl:part name="sayHelloReturn" type="soapenc:string"/>
- </wsdl:message>
- <wsdl:portType name="HelloService">
- <wsdl:operation name="sayHello">
- <wsdl:input message="impl:sayHelloRequest" name="sayHelloRequest"/>
- <wsdl:output message="impl:sayHelloResponse" name="sayHelloResponse"/>
- </wsdl:operation>
- <wsdl:operation name="sayHelloToPerson" parameterOrder="name">
- <wsdl:input message="impl:sayHelloToPersonRequest" name="sayHelloToPersonRequest"/>
- <wsdl:output message="impl:sayHelloToPersonResponse" name="sayHelloToPersonResponse"/>
- </wsdl:operation>
- </wsdl:portType>
- <wsdl:binding name="HelloServicesSoapBinding" type="impl:HelloService">
- <wsdlsoap:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/http"/>
- <wsdl:operation name="sayHello">
- <wsdlsoap:operation soapAction=""/>
- <wsdl:input name="sayHelloRequest">
- <wsdlsoap:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="http://webservice.sinosoft.com" use="encoded"/>
- </wsdl:input>
- <wsdl:output name="sayHelloResponse">
- <wsdlsoap:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="http://127.0.0.1:8080/WebServiceTest/services/HelloServices" use="encoded"/>
- </wsdl:output>
- </wsdl:operation>
- <wsdl:operation name="sayHelloToPerson">
- <wsdlsoap:operation soapAction=""/>
- <wsdl:input name="sayHelloToPersonRequest">
- <wsdlsoap:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="http://webservice.sinosoft.com" use="encoded"/>
- </wsdl:input>
- <wsdl:output name="sayHelloToPersonResponse">
- <wsdlsoap:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="http://127.0.0.1:8080/WebServiceTest/services/HelloServices" use="encoded"/>
- </wsdl:output>
- </wsdl:operation>
- </wsdl:binding>
- <wsdl:service name="HelloServiceService">
- <wsdl:port binding="impl:HelloServicesSoapBinding" name="HelloServices">
- <wsdlsoap:address location="http://127.0.0.1:8080/WebServiceTest/services/HelloServices"/>
- </wsdl:port>
- </wsdl:service>
- </wsdl:definitions>
用Java调用WebService实例
以下是用Java调用刚公布的WebService样例。

- /*
- * File name: TestHelloService.java
- *
- * Version: v1.0
- *
- * Created on Aug 2, 2008 9:54:10 AM
- *
- * Designed by Stephen
- *
- * (c)Copyright 2008
- */
- package test.com.sinosoft.webservice;
- import java.io.IOException;
- import java.net.MalformedURLException;
- 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;
- /**
- * @author Stephen
- *
- * 測试调用WebService
- */
- public class TestHelloService {
- private static final Log log = LogFactory.getLog(TestHelloService.class);
- private static final String HELLO_SERVICE_ENDPOINT = "http://127.0.0.1:8080/WebServiceTest/services/HelloServices?
wsdl";
- public static void main(String[] args) {
- TestHelloService tester = new TestHelloService();
- // tester.callSayHello();
- tester.callSayHelloToPerson();
- }
- public void callSayHello() {
- try {
- Service service = new Service();
- Call call = (Call) service.createCall();
- call.setTargetEndpointAddress(new java.net.URL(
- HELLO_SERVICE_ENDPOINT));
- //以下名字查询的http://127.0.0.1:8080/WebServiceTest/services/HelloServices?wsdl文件中有
- 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 (IOException e) {
- e.printStackTrace();
- }
- } catch (MalformedURLException e) {
- e.printStackTrace();
- } catch (ServiceException e) {
- e.printStackTrace();
- }
- log.error("call sayHello service error!");
- }
- 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!");
- }
- }
Call call = (Call) service.createCall();
call.setTargetEndpointAddress(new java.net.URL(
HELLO_SERVICE_ENDPOINT));
System.out.println("The return value is:" + ret);
利用Java编写简单的WebService实例的更多相关文章
- 利用Java编写简单的WebService实例-转载
使用Axis编写WebService比较简单,就我的理解,WebService的实现代码和编写Java代码其实没有什么区别,主要是将哪些Java类发布为WebService.下面是一个从编写测试例子到 ...
- Python 利用Python编写简单网络爬虫实例3
利用Python编写简单网络爬虫实例3 by:授客 QQ:1033553122 实验环境 python版本:3.3.5(2.7下报错 实验目的 获取目标网站“http://bbs.51testing. ...
- Python 利用Python编写简单网络爬虫实例2
利用Python编写简单网络爬虫实例2 by:授客 QQ:1033553122 实验环境 python版本:3.3.5(2.7下报错 实验目的 获取目标网站“http://www.51testing. ...
- 利用java编写的盲注脚本
之前在网上见到一个盲注的题目,正好闲来无事,便用java写了个盲注脚本,并记录下过程中的坑 题目源码: <?php header("Content-Type: text/html;ch ...
- 基于《仙剑奇侠传柔情版》利用Java的简单实现(一)
基于<仙剑奇侠传柔情版>利用Java的简单实现(一) 2018-12-01 23:55:36 by Louis 一,新建一个类GameFrame.class,具体代码如下: pack ...
- Java 使用Axis实现WebService实例
在上一篇WebService实例中,基于jdk1.6以上的javax.jws 发布webservice接口.这篇博文则主要用eclipse/myeclipse 使用axis插件进行发布和调用WebSe ...
- 一个简单的WebService实例
WebService在.NET平台下的作用是在不同应用程序间共享数据与数据交换. 要达到这样的目标,Web services要使用两种技术: XML(标准通用标记语言下的一个子集):XML是在web上 ...
- JAX-WS(一)之使用wsgen从Java创建简单的WebService
概念 JAX-WS2.0的全称Java API for XML-Based Web Service 2.0.JAX-WS2.0是对JAX-RPC1.0规范的扩展,是JAX-RPC1.1的后续版本,JA ...
- java编写简单的语法分析预测程序
编译原理课程中,编了一个简单的语法分析预测程序,这个程序时根据固定的文法得到预测分析表,然后编写程序来判断表达式是否会正确推到出来. 前提是程序没有左递归符合LL(1)文法: 文法如下: E→TE' ...
随机推荐
- 解决安装完Ubuntu系统后启动项中没有Ubuntu的问题
问题出现的原因是你没有把grub安装到硬盘的起始扇区里,按理说Ubuntu在安装的时候应该能很好的处理这个问题,但有个别电脑还是会出问题.不过我们可以通用命令解决 问题. 使用U盘进入Ubuntu系统 ...
- Nginx安装学习使用具体记录
前言:选择Nginx的长处:Nginx 能够在大多数 Unix like OS 上编译执行.并有 Windows 移植版. Nginx 的1.4.0稳定版已经于2013年4月24日公布.普通情况下,对 ...
- 面向对象知识点之statickeyword的使用
<?php /*由static定义的属性和方法称为静态成员和静态方法.static定义的属性和方法是属于类的,在对象之间共享.*/ /*比如能够通过定义一个静态变量来统计类实例化了多少个对象*/ ...
- HTML5学习笔记1 HTML5 新元素
自1999年以后html4.0已经改变了很我,今天,在html4.01中的几个已经被废弃,这些元素在html5中已经被删除或重新定义. 为了更好地处理今天的互联网应用,html5添加了很多新元素及功能 ...
- server2008系统修改3389远程端口
我给大家简单谈谈正确修改远程端口的方法 在开始-----运行菜单里,输入regedit,进入注册表编辑,按先面的路径进入修改端口的地方 HKEY_LOCAL_MACHINE\System ...
- django源码分析----Related继承结构
在django中关联关系大概可以分成many-to-one(foriegnkey).one-to-one.many-to-many 这三种.它们有如下的类结构 class RelatedField(F ...
- 数据库出错提示Duplicate entry * for key *的解决方法
错误编号:1062 错误提示: 查询语句错误] ERR: Duplicate entry ' for key 'PRIMARY' SQL: ' PHP: misc.php: ; IP 问题分析: 向唯 ...
- ubuntu下ssh设置firefox用的反向代理
mark一下: ssh -D 127.0.0.1:8080 -l root MyIp
- 详解Java中格式化日期的DateFormat与SimpleDateFormat类
DateFormat其本身是一个抽象类,SimpleDateFormat 类是DateFormat类的子类,一般情况下来讲DateFormat类很少会直接使用,而都使用SimpleDateFormat ...
- poj Squares n个点,共能组成多少个正方形 二分 + 哈希
题目链接:http://poj.org/problem?id=2002 测试数据: 41 00 11 10 090 01 02 00 21 22 20 11 12 14-2 53 70 05 20 有 ...