2016-12-01 00:00:00少芬 网页程序设计
XFire使得在JavaEE应用中发布Web服务变得轻而易举。和其他Web服务引擎相比,XFire的配置非常简单,可以非常容易地和Spring集成。那么XFire怎么创建WebService,下面yjbys小编为大家分享XFire创建WebService简单实例:
一. 使用XFire发布WebService
1. 创建service接口
Java代码
1. package com.test.service;
2. import com.test.service.bean.User;
3.
4. public interface IHelloService {
5. public String sayHello(String name);
6. public User getUser(User user);
7. }
8. }
2.创建Service接口的实现类
Java代码
1. package com.test.service;
2. import com.test.service.bean.User;
3. public class HelloService implements IHelloService{
4. public String sayHello(String name){
5. return "Hello, "+name;
6. }
7. public User getUser(User user) {
8. User userNew = new User();
9. userNew.setId("new:"+user.getId());
10. userNew.setName("new:"+user.getName());
11. return userNew;
12. }
13.}
Java代码
1. package com.test.service.bean;
2. public class User {
3. private String id;
4. private String name;
5. public String getId() {
6. return id;
7. }
8. public void setId(String id) {
9. this.id = id;
10. }
11. public String getName() {
12. return name;
13. }
14. public void setName(String name) {
15. this.name = name;
16. }
17.}
3.在web.xml文件中进行XFire拦截配置,可参照官网提供的sample。
Xml代码
1. <?xml version="1.0" encoding="UTF-8"?>
2. <web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4. xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
5. http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
6.
7. <servlet>
8. <servlet-name>XFireServlet</servlet-name>
9. <servlet-class>
10. org.codehaus.xfire.transport.http.XFireConfigurableServlet
11. </servlet-class>
12.</servlet>
13.<servlet-mapping>
14. <servlet-name>XFireServlet</servlet-name>
15. <url-pattern>/servlet/XFireServlet/*</url-pattern>
16.</servlet-mapping>
17.<servlet-mapping>
18. <servlet-name>XFireServlet</servlet-name>
19. <url-pattern>/services/*</url-pattern>
20.</servlet-mapping>
21.</web-app>
4.在class目录下建立META-INF目录,如(META-INF>xifre->services.xml),
在services.xml文件中进行webservice服务的发布,可参照官网提供的sample。
Xml代码
1. <?xml version="1.0" encoding="UTF-8"?>
2. <beans xmlns="http://xfire.codehaus.org/config/1.0">
3. <service>
4. <name>HelloService</name>
5. <serviceClass>com.test.service.IHelloService</serviceClass>
6. <implementationClass>com.test.service.HelloService</implementationClass>
7. </service>
8. </beans>
5.将xfire的Jar包和相关依赖包拷到系统应用中,在Tomcat下部署应用。
启动tomcat后访问服务(需在应用后加上/services,webservice才会进行拦截):
例:http://localhost:8080/ws2/services。(IE下有时无法显示列表,可用其它浏览器显示或指定接口名称)
界面如下:
点击后查看详细的wsdl文档,不同的浏览器下访问会有区别,展示效果不一致。
二.建立XFire客户端进行调用
1.本地客户端调用,与webservice服务在同一应用。
Java代码
1. package com.test.client;
2.
3. import java.net.MalformedURLException;
4. import org.codehaus.xfire.XFireFactory;
5. import org.codehaus.xfire.client.XFireProxyFactory;
6. import org.codehaus.xfire.service.Service;
7. import org.codehaus.xfire.service.binding.ObjectServiceFactory;
8. import com.test.service.IHelloService;
9. import com.test.service.bean.User;
10.
11.public class ClientTest {
12. public static void main(String args[]) throws MalformedURLException {
13. Service service = new ObjectServiceFactory().create(IHelloService.class);
14. XFireProxyFactory factory = new XFireProxyFactory(XFireFactory
15. .newInstance().getXFire());
16. String url = "http://localhost:8080/ws2/services/HelloService";
17. IHelloService helloService = (IHelloService) factory.create(service,url);
18. System.out.println(helloService.sayHello("张三"));
19. User user = new User();
20. user.setName("张三");
21. System.out.println(helloService.getUser(user).getName());
22. }
23.}
执行结果如下:
Java代码
1. Hello, 张三
2. new:张三
2. 利用XFire创建build.xml文件生成客户端代码调用webservice服务
2.1在src目录下创建build.properties文件,配置如下:
Java代码
1. src.dir=${basedir}
2. lib.dir=D:/myspace/ws2/WebRoot/WEB-INF/lib
3. wsdl.dir=http\://localhost\:8080/ws2/services/HelloService?wsdl
2.2在src目录下创建build.xml文件,配置如下:
Xml代码
1. <?xml version="1.0" encoding="UTF-8"?>
2. <project name="WebService" basedir="." default="gen-webservice">
3. <property file="build.properties"></property>
4. <path id="project-classpath">
5. <fileset dir="${lib.dir}">
6. <include name="**/*.jar" />
7. </fileset>
8. </path>
9.
10. <target name="gen-webservice">
11. <taskdef name="wsgen" classname="org.codehaus.xfire.gen.WsGenTask"
12. classpathref="project-classpath" />
13. <wsgen outputDirectory="${src.dir}" wsdl="${wsdl.dir}"
14. package="stub.test.client" overwrite="true"/>
15. </target>
16.</project>
用ant构建生成代码,生成后的目录如下:
2.3编写客户端代码进行调用
Java代码
1. package com.test.client;
2. import stub.test.client.HelloServiceClient;
3. import stub.test.client.HelloServicePortType;
4. import com.test.service.bean.ObjectFactory;
5. import com.test.service.bean.User;
6.
7. public class HelloServiceTest {
8.
9. public static void main(String args[]){
10. HelloServiceClient service = new HelloServiceClient();
11. HelloServicePortType portType = service.getHelloServiceHttpPort();
12. System.out.println(portType.sayHello("张三"));
13.
14. ObjectFactory factory = new ObjectFactory();
15. User user = factory.createUser();
16. user.setName(factory.createUserName("张三"));
17. System.out.println(portType.getUser(user).getName().getValue());
18. }
19.}
执行结果如下:
Java代码
1. Hello, 张三
2. new:张三
[网页程序设计]热门推荐
879
人