第一个webservice案例

本文档记录了一个Web服务调用失败的原因,主要由于endpoint路径中的'webservice'小写错误导致。修复方法是将'http://localhost:8080/webservice/services/Hello'中的'webservice'改为小写。同时,文中提到了在新建的web工程中配置server-config.wsdd文件,并列举了客户端和服务端所需的jar包依赖。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

AxisFault
 faultCode: {http://xml.apache.org/axis/}HTTP
 faultSubcode: 
 faultString: (404)Not Found
 faultActor: 
 faultNode: 
 faultDetail: 
	{}:return code:  404
<html><head><title>Apache Tomcat/7.0.47 - Error report</title><style><!--H1 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:22px;} H2 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:16px;} H3 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:14px;} BODY {font-family:Tahoma,Arial,sans-serif;color:black;background-color:white;} B {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;} P {font-family:Tahoma,Arial,sans-serif;background:white;color:black;font-size:12px;}A {color : black;}A.name {color : black;}HR {color : #525D76;}--></style> </head><body><h1>HTTP Status 404 - /Webservice/services/Hello</h1><HR size="1" noshade="noshade"><p><b>type</b> Status report</p><p><b>message</b> <u>/Webservice/services/Hello</u></p><p><b>description</b> <u>The requested resource is not available.</u></p><HR size="1" noshade="noshade"><h3>Apache Tomcat/7.0.47</h3></body></html>
	{http://xml.apache.org/axis/}HttpErrorCode:404

(404)Not Found
	at org.apache.axis.transport.http.HTTPSender.readFromSocket(HTTPSender.java:744)
	at org.apache.axis.transport.http.HTTPSender.invoke(HTTPSender.java:144)
	at org.apache.axis.strategies.InvocationStrategy.visit(InvocationStrategy.java:32)
	at org.apache.axis.SimpleChain.doVisiting(SimpleChain.java:118)
	at org.apache.axis.SimpleChain.invoke(SimpleChain.java:83)
	at org.apache.axis.client.AxisClient.invoke(AxisClient.java:165)
	at org.apache.axis.client.Call.invokeEngine(Call.java:2784)
	at org.apache.axis.client.Call.invoke(Call.java:2767)
	at org.apache.axis.client.Call.invoke(Call.java:2443)
	at org.apache.axis.client.Call.invoke(Call.java:2366)
	at org.apache.axis.client.Call.invoke(Call.java:1812)
	at com.webservice.client.ServiceClient.main(ServiceClient.java:18)


原因:

String endpoint = "http://localhost:8080/webservice/services/Hello";

路径写错了

String endpoint = "http://localhost:8080/webservice/services/Hello";//webservice是w小写的,W -> w


新建工程web工程 (weservice)

1.在WEB-INF目录下新建server-config.wsdd( /webservice/WebContent/WEB-INF/server-config.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">
	<handler type="java:org.apache.axis.handlers.http.URLMapper"
		name="URLMapper" />
	<service name="Hello" provider="java:RPC">
		<parameter name="className" value="com.webservice.SayHello" />
		<parameter name="allowedMethods" value="sayHello" />
	</service>
	<transport name="http">
		<requestFlow>
			<handler type="URLMapper" />
		</requestFlow>
	</transport>
</deployment>

2.在WEB-INF目录下新建web.xml(/webservice/WebContent/WEB-INF/web.xml)

内容如下:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	id="WebApp" version="2.5"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
	<servlet>  
        <servlet-name>AxisServlet</servlet-name>  
        <servlet-class>org.apache.axis.transport.http.AxisServlet</servlet-class>  
    </servlet>  
    <servlet-mapping>  
        <servlet-name>AxisServlet</servlet-name>  
        <url-pattern>/services/*</url-pattern>  
    </servlet-mapping>  
</web-app>

3.在src目录下新建SayHello.java(/webservice/src/com/webservice/SayHello.java)

内容如下:

package com.webservice;

public class SayHello {
	 public void sayHello(String strName)
	 {
		 System.out.println("Hello,"+strName+" , Welcome to webservice world!");
	 }
}

4.建立客户端ServiceClient(java project)

建立客户端类ServiceClient.java(ServiceClient/src/com/webservice/client/ServiceClient.java)

内容如下:

package com.webservice.client;

import java.net.URL;

import org.apache.axis.client.Call;
import org.apache.axis.client.Service;

public class ServiceClient {
	public static void main(String[] args) {
		String endpoint = "http://localhost:8080/webservice/services/Hello";
		Service service = new Service();
		try {
			Call call = (Call) service.createCall();
			URL url = new URL(endpoint);
			call.setTargetEndpointAddress(url);
			call.setOperationName("sayHello");
			call.invoke(new Object[] { "sunny" });
		} catch (Exception exp) {
			exp.printStackTrace();
		}
	}
}


记得加jar包:

/ServiceClient/lib/activation.jar
/ServiceClient/lib/axis-ant.jar
/ServiceClient/lib/axis.jar
/ServiceClient/lib/commons-discovery-0.2.jar
/ServiceClient/lib/commons-logging-1.0.4.jar
/ServiceClient/lib/jaxrpc.jar
/ServiceClient/lib/log4j-1.2.8.jar
/ServiceClient/lib/mailapi_1_3_1.jar
/ServiceClient/lib/saaj.jar
/ServiceClient/lib/wsdl4j-1.5.1.jar
/ServiceClient/lib/xml-apis-2.6.2.jar
/ServiceClient/lib/xmlsec-1.4.2.jar



/webservice/WebContent/WEB-INF/lib/activation.jar
/webservice/WebContent/WEB-INF/lib/axis-ant.jar
/webservice/WebContent/WEB-INF/lib/axis.jar
/webservice/WebContent/WEB-INF/lib/commons-discovery-0.2.jar
/webservice/WebContent/WEB-INF/lib/commons-logging-1.0.4.jar
/webservice/WebContent/WEB-INF/lib/jaxrpc.jar
/webservice/WebContent/WEB-INF/lib/log4j-1.2.8.jar
/webservice/WebContent/WEB-INF/lib/mailapi_1_3_1.jar
/webservice/WebContent/WEB-INF/lib/saaj.jar
/webservice/WebContent/WEB-INF/lib/wsdl4j-1.5.1.jar
/webservice/WebContent/WEB-INF/lib/xml-apis-2.6.2.jar
/webservice/WebContent/WEB-INF/lib/xmlsec-1.4.2.jar






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值