文章目录

依赖名称

依赖版本

spring

4.3.10.RELEASE

xfire

1.26

一、idea快速搭建web项目

IDEA快速 实现 SpringMVC 整合xfire 发布 WebService 服务_json

二、xfire 服务方搭建
1. pom依赖
<properties>
<spring.version>4.3.10.RELEASE</spring.version>
<xfire-all.version>1.2.6</xfire-all.version>
</properties>

<!--spring start-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>${spring.version}</version>
</dependency>
<!--spring end-->


<dependency>
<groupId>org.codehaus.xfire</groupId>
<artifactId>xfire-all</artifactId>
<version>${xfire-all.version}</version>
</dependency>
2. web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>

<!-- webservices 接口 XFire 需要引入的 Servlet Start -->
<servlet>
<servlet-name>XFireServlet</servlet-name>
<servlet-class>org.codehaus.xfire.transport.http.XFireConfigurableServlet</servlet-class>
</servlet>
<!-- 区配前缀 Start -->
<servlet-mapping>
<servlet-name>XFireServlet</servlet-name>
<url-pattern>/servlet/XFireServlet/*</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>XFireServlet</servlet-name>
<url-pattern>/xfireUser/services/*</url-pattern>
</servlet-mapping>
<!-- 区配前缀 End -->
<!-- webservices 接口 XFire 需要引入的 Servlet End -->
</web-app>
3. 创建一个entity
@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class User implements Serializable {
private Long id;
private String name;
private Integer age;
private String email;
}
4. 创建一个接口
package com.gblfy.service;

public interface IXfireService {

/**
* 测试传递json类型 的User对象 类型的信息 获取户信息接口
*
* @param jsonUser
* @return
*/
public String getUser(String jsonUser);

}
5. 创建接口实现类
package com.gblfy.service.impl;

import com.gblfy.service.IXfireService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class IXfireServiceImpl implements IXfireService {

/**
* 测试传递User对象 类型的信息 获取户信息接口
*/
@Override
public String getUser(String jsonUser) {
log.info("接收的到 纽约 发送的报文 {}", jsonUser);
return jsonUser;
}
}
6. 在WEB-INF创建一个META-INF的目录
7. 在META-INF创建一个xfire的目录
8. 在xfire创建一个services.xml的目录

services.xml 配置内容如下:

<?xml version="1.0" encoding="UTF-8"?>
<beans>
<service xmlns="http://xfire.codehaus.org/config/1.0">
<!--
1. 暴露的服务名
2. 自定义命名空间
3. 接口路径
4. 接口实现类路径 -->
<name>IXfireService</name>
<namespace>CustomNamespaces</namespace>
<serviceClass>com.gblfy.service.IXfireService</serviceClass>
<implementationClass>com.gblfy.service.impl.IXfireServiceImpl
</implementationClass>
</service>
</beans>

项目截图:

IDEA快速 实现 SpringMVC 整合xfire 发布 WebService 服务_WebService_02

启动项目验证测试:
浏览器访问:

http://localhost:8081/XFireTest/services/HelloService?wsdl

注:

格式:http://ip地址:端口号/项目名/区配前缀/暴露的服务名

名称

说明

协议

http://

ip地址

localhost

端口号

8081

项目名

XFireTest

区配前缀

/services/和/servlet/XFireServlet/

暴露的服务名

HelloService

服务后缀

?wsdl

部署服务器

tomcat9

命名空间和方法名已标注:

IDEA快速 实现 SpringMVC 整合xfire 发布 WebService 服务_WebService_03


浏览器访问:

http://localhost:8081/XFireTest/servlet/XFireServlet/HelloService?wsdl

IDEA快速 实现 SpringMVC 整合xfire 发布 WebService 服务_xml_04

这样服务端就用哪个xfire发布服务成功了。

二、xfire客户端搭建
1. 创建xfire客户端(企业版本)
package com.gblfy.utils;

import lombok.extern.slf4j.Slf4j;
import org.codehaus.xfire.client.Client;
import org.springframework.stereotype.Repository;

import java.net.MalformedURLException;
import java.net.URL;

@Repository
@Slf4j
public class WSXfireClientUtils {
/**
* WSXfire 客户端
*
* @param url 代用地址
* @param nameSpace 命名空间
* @param method 方法名
* @param tReqXml 请求报文
* @return tResXml 响应报文
* @throws MalformedURLException
* @throws Exception
*/
public static String wSXfireClient(String url, String nameSpace, String method, String tReqXml) throws Exception {
long forStrTime = 0L;
long endToStart = 0L;

Client client = new Client(new URL(url));
client.setProperty(nameSpace, method);
log.info("开始转发请求报文******************************* {}", tReqXml);
forStrTime = System.currentTimeMillis();
Object[] result = client.invoke(method, new Object[]{tReqXml});
log.info("响应报文: {}", result[0]);

long forEndTime = System.currentTimeMillis();
endToStart = (long) (forEndTime - forStrTime);
log.info("转发消耗的时间: {} 毫秒", endToStart);

String tResXml = (String) result[0];
return tResXml;
}
}
2. xfire客户端调用
package utils;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.gblfy.entity.User;
import com.gblfy.utils.WSXfireClientUtils;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
@Slf4j
public class WSXfireClientTest {

//引入json工具类
private static final ObjectMapper MAPPER = new ObjectMapper();

@Autowired
private WSXfireClientUtils wsXfireClient;

@Test
public void testXfireStrType() throws Exception {
String url = "http://localhost:8080/xfireUser/services/IXfireService?wsdl";
String nameSpace = "CustomNamespaces";
String method = "getUser";
String tReqXml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n" + "<bookstore>\r\n"
+ " <book id=\"1\">\r\n" + " <name>方国军</name>\r\n" + " <author>乔治马丁</author>\r\n"
+ " <year>2014</year>\r\n" + " <price>89</price>\r\n" + " </book>\r\n"
+ " <book id=\"2\">\r\n" + " <name>安徒生童话</name>\r\n" + " <year>2004</year>\r\n"
+ " <price>77</price>\r\n" + " <language>English</language>\r\n" + " </book> \r\n"
+ "</bookstore>";
// String tReqXml = "Xfire客户端调用成功";
String message = wsXfireClient.wSXfireClient(url, nameSpace, method, tReqXml);
log.info("返回报文 {}", message);
}
@Test
public void testXfireObjToJsonStrType() throws Exception {
String url = "http://localhost:8080/xfireUser/services/IXfireService?wsdl";
String nameSpace = "CustomNamespaces";
String method = "getUser";
User user = new User().builder()
.id(1l)
.age(2)
.email("gblfy@163.com")
.name("gblfy").build();
// 1. 数据对象转jsonStr处理 易快速解析处理
String tReqXml = MAPPER.writeValueAsString(user);
String message = wsXfireClient.wSXfireClient(url, nameSpace, method, tReqXml);
log.info("返回报文 {}", message);
}
}

本文项目源码:​​https://gitee.com/gb_90/webserice-column​