当前位置: 代码迷 >> Web前端 >> 简略的WebService
  详细解决方案

简略的WebService

热度:399   发布时间:2013-09-11 18:00:00.0
简单的WebService

最近参考JE论坛上thh_r的《Web Service开发指南》,学习了一下WebService,做了一个简单的例子,废话不多说了。进入正题……

?

?

?

第一步:我使用的是xfire,所以请下载xfire发布服务的相关jar包(我已经把jar包用附件的形式上传了)

?

第二步:然后从最简单的Say Hello开始……首先建立一个Web工程,我的工程名是ws_service,用来发布服务。这个服务是可以提供给调用服务的人传递一个String类型的参数-name,然后返回一个形式为:${name} Say Hello!的字符串。

?

请将第一步下载的jar包全部放入本web工程的lib目录下,然后修改web.xml,添加以下这段代码,添加对xfire的监控

?

<!-- xfire config start -->
	<servlet>
		<servlet-name>XFireServlet</servlet-name>
		<servlet-class>org.codehaus.xfire.transport.http.XFireConfigurableServlet</servlet-class>
		<load-on-startup>0</load-on-startup>
	</servlet>
	<servlet-mapping>
		<servlet-name>XFireServlet</servlet-name>
		<url-pattern>/servlet/XFireServlet/*</url-pattern>
	</servlet-mapping>
	<servlet-mapping>
		<servlet-name>XFireServlet</servlet-name>
		<url-pattern>/service/*</url-pattern>
	</servlet-mapping>
	<!-- xfire config end -->

?

?

第三步:编写服务代码,例子很简单,一个名字为 IHelloWebService.java 的接口和他的实现类:HelloWebService.java

?

package com.ws.service;

public interface IHelloWebService {

	public String sayHello(String name);
	
}

?

package com.ws.service;

public class HelloWebService implements IHelloWebService {

	public String sayHello(String name) {

		return "[ " + name + " ] say : hello!!";

	}

}

??

?

?这就是本例子中发布的服务,十分简单,调用服务的人传递一个name的String类型参数,服务器返回给用户一段字符串

?

?

?第四步:在src文件夹下建立 \META-INF\xfire\?? 文件夹,请注意,不是WebRoot下的META-INF,否则xfire无法找到配置文件。在新建立的xfire文件夹下建立services.xml文件。代码如下

?

<?xml version="1.0" encoding="UTF-8"?>
<beans>
	<service xmlns="http://xfire.codehaus.org/config/1.0">
		
		<name>HelloWebService</name>
		
		<namespace>http://simple.xfire.webservice/HelloWebService</namespace>
		
		<serviceClass>com.ws.service.IHelloWebService</serviceClass>
		
		<implementationClass>com.ws.service.HelloWebService</implementationClass>
		
		<scope>request</scope>
		
	</service>

</beans>

?

?

<name></name>中的值就是本服务的名称

<namespace></namespace>为服务的命名空间

<serviceClass></serviceClass>是服务的接口,如果没有接口的话,这里就填写实现类的全路径

<implementationClass></implementationClass>中是实现类的路径,如果没有接口,则本属性可以不写

<scope></scope>为存活的范围

?

到现在为止,我们的服务就已经算发布成功了。请把工程部署并启动服务器。现在打开浏览器,输入地址

?

?http://localhost:8080/ws_service/service/HelloWebService?wsdl

?

会打开wsdl的描述页面,证明发布成功

?

第五步:建立另外一个工程ws_client,作为客户端,来调用上面的工程发布的服务。仍然把全部jar包添加进客户端工程。我们需要使用ant来帮助我们生成代码,如果没有看过ant的朋友请查阅相关内容,请在本工程的根目录下建立build.xml文件,代码如下

?

<?xml version="1.0" encoding="UTF-8"?>
<project name="create client" default="wsgen" basedir=".">
	
	<property name="lib.dir" value="${basedir}\WebRoot\WEB-INF\lib\"/>
	
	<path id="devlib.classpath">
		<fileset dir="${lib.dir}">
			<include name="**/*.jar" />
		</fileset>
	</path>
	<taskdef classpathref="devlib.classpath" name="wsgen" classname="org.codehaus.xfire.gen.WsGenTask" />
	<target name="wsgen" description="generate client">
		<wsgen outputDirectory="./src/" wsdl="http://localhost:8080/ws_service/service/HelloWebService?wsdl" binding="jaxb" package="com.ws.client" overwrite="true" />
	</target>
</project>

?

这里做一下说明,devlib.classpath是本工程全部依赖jar包所在位置,wsdl的值是调用的webservice的服务地址(因为我都是在本机进行测试,所以上面地址中使用的是localhost,如果调用其他服务,请进行相应修改),package是将自动生成的代码所放的位置,overwrite是如果存在重名文件,则覆盖之

?

还有一点要说明,那就是作为服务的webservice一定要处于发布状态(群众纷纷表示:靠,这不是废话嘛…………)

?

最后,运行build.xml,成功后,刷新工程,会发现多了一些文件,那么这些就是xfire帮助我们自动生成的客户端代码。

?

那么我做一下说明,一般都会在上面build.xml中定义的package包中生成三个类,根据名字我们就可以判断出一个是客户端,一个是接口,一个是实现类。

?

然后,我们现在就可以调用上面发布的服务了,新建一个Client.java来进行测试,代码如下

?

package com.ws.client.test;

import com.ws.client.HelloWebServiceClient;
import com.ws.client.HelloWebServicePortType;

public class Client {

	public static void main(String[] args) {
		
		HelloWebServiceClient client = new HelloWebServiceClient();
		
		HelloWebServicePortType soap = client.getHelloWebServiceHttpPort();
		
		System.out.println(soap.sayHello("Saeba Lee"));

	}

}

?

?

?

运行后,控制台输出:[ Saeba Lee ] say : hello!!

?

好了,我们的工作已经做完了,这就是一个简单的webservice的发布与调用,菜鸟,请轻拍…………

?

?

最后呢,如果大家掌握了以上的内容,那么可以拿这个帖子中提供的测试地址练练手,有几个还是比较好玩的,步骤与我上面所做的只是修改一下build.xml中的wsdl的值^^

?

如果作webService测试,请猛击这里

1 楼 shaoping 2011-09-28  
[code='html']
HTTP Status 500 -

type Exception report

message

description The server encountered an internal error () that prevented it from fulfilling this request.

exception

javax.servlet.ServletException: Error initializing XFireServlet.
org.codehaus.xfire.transport.http.XFireServlet.init(XFireServlet.java:52)
javax.servlet.GenericServlet.init(GenericServlet.java:160)
org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:498)
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:562)
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:394)
org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:243)
org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:188)
org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:166)
org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:302)
java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
java.lang.Thread.run(Thread.java:662)

root cause

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.codehaus.xfire.spring.ServiceBean' defined in class path resource [META-INF/xfire/services.xml]: Error setting property values; nested exception is org.springframework.beans.PropertyBatchUpdateException; nested PropertyAccessExceptions (1) are:
PropertyAccessException 1: org.springframework.beans.TypeMismatchException: Failed to convert property value of type [java.lang.String] to required type [java.lang.Class] for property 'serviceClass'; nested exception is java.lang.IllegalArgumentException: Cannot find class [com.ws.service.IHelloWebService]. Root cause: java.lang.ClassNotFoundException: com.ws.service.IHelloWebService
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:975)
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:729)
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:416)
org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:245)
org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:141)
org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:242)
org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:156)
org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:290)
org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:348)
org.codehaus.xfire.spring.XFireConfigLoader.getXFireApplicationContext(XFireConfigLoader.java:107)
org.codehaus.xfire.spring.XFireConfigLoader.loadContext(XFireConfigLoader.java:41)
org.codehaus.xfire.transport.http.XFireConfigurableServlet.loadConfig(XFireConfigurableServlet.java:86)
org.codehaus.xfire.transport.http.XFireConfigurableServlet.createXFire(XFireConfigurableServlet.java:54)
org.codehaus.xfire.transport.http.XFireServlet.init(XFireServlet.java:45)
javax.servlet.GenericServlet.init(GenericServlet.java:160)
org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:498)
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:562)
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:394)
org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:243)
org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:188)
org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:166)
org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:302)
java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
java.lang.Thread.run(Thread.java:662)

root cause

org.springframework.beans.PropertyBatchUpdateException; nested PropertyAccessExceptions (1) are:
PropertyAccessException 1: org.springframework.beans.TypeMismatchException: Failed to convert property value of type [java.lang.String] to required type [java.lang.Class] for property 'serviceClass'; nested exception is java.lang.IllegalArgumentException: Cannot find class [com.ws.service.IHelloWebService]. Root cause: java.lang.ClassNotFoundException: com.ws.service.IHelloWebService
org.springframework.beans.AbstractPropertyAccessor.setPropertyValues(AbstractPropertyAccessor.java:97)
org.springframework.beans.AbstractPropertyAccessor.setPropertyValues(AbstractPropertyAccessor.java:57)
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:966)
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:729)
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:416)
org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:245)
org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:141)
org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:242)
org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:156)
org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:290)
org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:348)
org.codehaus.xfire.spring.XFireConfigLoader.getXFireApplicationContext(XFireConfigLoader.java:107)
org.codehaus.xfire.spring.XFireConfigLoader.loadContext(XFireConfigLoader.java:41)
org.codehaus.xfire.transport.http.XFireConfigurableServlet.loadConfig(XFireConfigurableServlet.java:86)
org.codehaus.xfire.transport.http.XFireConfigurableServlet.createXFire(XFireConfigurableServlet.java:54)
org.codehaus.xfire.transport.http.XFireServlet.init(XFireServlet.java:45)
javax.servlet.GenericServlet.init(GenericServlet.java:160)
org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:498)
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:562)
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:394)
org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:243)
org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:188)
org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:166)
org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:302)
java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
java.lang.Thread.run(Thread.java:662)



新手啥都不会求指教

我的异常网推荐解决方案:The server encountered an internal error () that prevented it from fulfilling this request.,http://www.myexception.cn/java-web/317.html
  相关解决方案