webservice之xfire服务搭建及客户端请求(maven搭建项目)

Myth丶恋晨 2024-02-17 23:57 118阅读 0赞
  1. 运行环境

(1) 操作系统:windows10

(2) JDK : 1.7

(3) IDE : Myeclipse10

(4) xfire : xfire1.2.6

(5) 服务器:tomcat7

  1. 服务端环境搭建步骤

(1) 配置maven的配置文件,下载xfire


4.0.0
com.dh
TestXfireService
war
0.0.1-SNAPSHOT
TestXfireService Maven Webapp
http://maven.apache.org


1.2.6






org.codehaus.xfire
xfire-all
${xfire-all.version}





TestXfireService

(2) 配置web.xml

<?xml version=”1.0” encoding=”UTF-8”?>

XFireServlet


org.codehaus.xfire.transport.http.XFireConfigurableServlet

XFireServlet

/services/*

(3) 创建接口

在src/main/java目录下,创建自定义目录(如:com.test.xfire),在自定义目录下创建接口:IHelloService 及实现类 HelloServiceImpl

A. IHelloService.java

package com.test.service.xfire;
public interface IHelloService {
void hello(String name);
}

B. HelloServiceImpl.java

package com.test.service.xfire.impl;
import com.test.service.xfire.IHelloService;

public class HelloServiceImpl implements IHelloService {

@Override
public void hello(String name) {
System.out.println(“—-xfire—hello —“ + name);
}
}

(4) 配置xfire 接口文件services.xml

在WEB-INF目录下创建文件 \META-INF\xfire\services.xml ,完整路径如:src\main\webapp\WEB-INF\META-INF\xfire\services.xml

注: \META-INF\xfire\services.xml 中的xfire为自定义文件夹名称

services.xml的配置方式如下:

<?xml version=”1.0” encoding=”UTF-8”?>



HelloService
com.test.xfire.IHelloService
com.test.xfire.impl.HelloServiceImpl

(5) 将项目发布到tomcat服务器,在浏览器地址栏输入:http://192.168.1.133:8080/TestXfireService/services/HelloService?wsdl ,显示接口信息

  1. 在同一应用中创建xfire客户端进行调用

    package com.test.client;

import java.net.MalformedURLException;

import org.codehaus.xfire.XFireFactory;
import org.codehaus.xfire.client.XFireProxyFactory;
import org.codehaus.xfire.service.Service;
import org.codehaus.xfire.service.binding.ObjectServiceFactory;

import com.test.service.xfire.IHelloService;

public class TestClientMain {

/**
* @param args
* @throws MalformedURLException
*/
public static void main(String[] args) throws MalformedURLException {
Service service = new ObjectServiceFactory().create(IHelloService.class);
XFireProxyFactory factory = new XFireProxyFactory(XFireFactory
.newInstance().getXFire());
String url = “http://192.168.1.133:8080/TestXfireService/services/HelloService“;
IHelloService helloService = (IHelloService) factory.create(service,url);
helloService.hello(“张三”);
}

}

注:客户端调用时,url路径中不能 带 “?wsdl”

发表评论

表情:
评论列表 (有 0 条评论,118人围观)

还没有评论,来说两句吧...

相关阅读