网站图片水印,wordpress微博挂件,中国教育培训网,商城网站类建设哪家好文章目录一、集成XFIRE1. 版本选型2. 导入依赖3. 注入XFireSpringServlet4. 创建一个xml文件5. 使用ImportResource注入xml6. 创建WebService接口6. 创建实现类7. 添加配置类8. 工具类二、XFIRE 发布服务2.1. 运行项目2.2. 异常解决2.3. 测试验证三、XFIRE客户端开源源码.一、集…
文章目录一、集成XFIRE1. 版本选型2. 导入依赖3. 注入XFireSpringServlet4. 创建一个xml文件5. 使用ImportResource注入xml6. 创建WebService接口6. 创建实现类7. 添加配置类8. 工具类二、XFIRE 发布服务2.1. 运行项目2.2. 异常解决2.3. 测试验证三、XFIRE客户端开源源码.一、集成XFIRE
1. 版本选型
阿健/框架版本spring-boot2.5.4xfire-all1.2.6
2. 导入依赖
导入xfire的依赖包xfire-all会自动导入相关依赖包其中spring可能会与项目本身的spring冲突需要将其排除依赖 !--xfire start --dependencygroupIdorg.codehaus.xfire/groupIdartifactIdxfire-all/artifactIdversion1.2.6/version!--排除冲突的依赖包--exclusionsexclusiongroupIdjavax.activation/groupIdartifactIdactivation/artifactId/exclusionexclusiongroupIdorg.springframework/groupIdartifactIdspring/artifactId/exclusionexclusionartifactIdcommons-logging/artifactIdgroupIdcommons-logging/groupId/exclusionexclusionartifactIdstax-api/artifactIdgroupIdstax/groupId/exclusion/exclusions/dependency!--axis end --3. 注入XFireSpringServlet
注入XFireSpringServlet 创建XfireBootServlet类
package com.gblfy.ws.servlet;import org.codehaus.xfire.spring.XFireSpringServlet;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;/*** author gblfy* date 2021-09-17*/
Configuration
public class XfireBootServlet {Beanpublic ServletRegistrationBean registrationBean() {System.out.println(servletRegistrationBean----------);ServletRegistrationBean servletRegistrationBean new ServletRegistrationBean();servletRegistrationBean.setServlet(new XFireSpringServlet());servletRegistrationBean.addUrlMappings(/webservice/*);return servletRegistrationBean;}
}相当于web.xml中的
?xml version1.0 encodingUTF-8?
web-app version2.5 xmlnshttp://java.sun.com/xml/ns/javaee xmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsdservletservlet-namexfireServlet/servlet-nameservlet-classorg.codehaus.xfire.spring.XFireSpringServlet/servlet-class/servletservlet-mappingservlet-namexfireServlet/servlet-nameurl-pattern/webservice/*/url-pattern/servlet-mapping
/web-app
4. 创建一个xml文件
在resources下创建cofnig文件夹并在config文件夹下面创建boot-xfire.xml
?xml version1.0 encodingUTF-8 ?
beans xmlnshttp://www.springframework.org/schema/beansxmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexmlns:contexthttp://www.springframework.org/schema/contextxsi:schemaLocationhttp://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-2.0.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-3.1.xsd!--扫描被webService的包--context:component-scan base-packagecom.gblfy.ws.service.impl/!-- XFire start --import resourceclasspath:org/codehaus/xfire/spring/xfire.xml/!--import resourcexfire.xml /--bean idwebAnnotations classorg.codehaus.xfire.annotations.jsr181.Jsr181WebAnnotations/bean idjsr181HandlerMapping classorg.codehaus.xfire.spring.remoting.Jsr181HandlerMappingproperty namexfire refxfire/property namewebAnnotations refwebAnnotations//bean
/beans5. 使用ImportResource注入xml
package com.gblfy.ws.config;import org.springframework.context.annotation.ImportResource;
import org.springframework.stereotype.Component;/*** 引入boot-xfire.xml配置文件** author gblfy* date 2021-09-17*/
ImportResource(locations {classpath:config/boot-xfire.xml})
Component
public class XfireConfig {
}6. 创建WebService接口
package com.gblfy.ws.service;import javax.jws.WebService;/*** Xfire接口** author gblfy* date 2021-09-17*/
WebService
public interface IXfireService {public String sayHello(String info);public String sayHello2(String info,String info2);
}
6. 创建实现类
package com.gblfy.ws.service.impl;import com.gblfy.ws.service.IXfireService;
import com.gblfy.ws.utils.SpringBootBeanAutowiringSupport;
import org.springframework.stereotype.Service;import javax.jws.WebService;
import javax.xml.ws.BindingType;
import javax.xml.ws.soap.SOAPBinding;
/*** Xfire接口实现类** author gblfy* date 2021-09-17*//*** serviceName:?wsdl前缀* targetNamespace:命名空间* name:无实际意义*/
WebService(serviceName xfireServiceShell, name xfireService,targetNamespace http://impl.service.ws.gblfy.com)
BindingType(value SOAPBinding.SOAP12HTTP_BINDING)
Service
//继承SpringBootBeanAutowiringSupport 可以让Autowired注入成功我重写了WebApplicationContextLocator的onStartup方法
public class XfireServiceImpl extends SpringBootBeanAutowiringSupport implements IXfireService {/*** param info* return*/Overridepublic String sayHello(String info) {return sayHello: info;}Overridepublic String sayHello2(String info, String info2) {return info info2;}
}
7. 添加配置类
WebApplicationContextLocator
package com.gblfy.ws.config;import org.springframework.boot.web.servlet.ServletContextInitializer;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;import javax.servlet.ServletContext;
import javax.servlet.ServletException;/*** 手动获取bean** author gblfy* date 2021-09-17*/
Configuration
public class WebApplicationContextLocator implements ServletContextInitializer {private static WebApplicationContext webApplicationContext;public static WebApplicationContext getCurrentWebApplicationContext() {return webApplicationContext;}/*** 在启动时将servletContext 获取出来后面再读取二次使用。* param servletContext* throws ServletException*/Overridepublic void onStartup(ServletContext servletContext) throws ServletException {webApplicationContext WebApplicationContextUtils.getWebApplicationContext(servletContext);}
}8. 工具类
SpringBootBeanAutowiringSupport
package com.gblfy.ws.utils;import com.gblfy.ws.config.WebApplicationContextLocator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor;
import org.springframework.util.Assert;
import org.springframework.util.ClassUtils;
import org.springframework.web.context.WebApplicationContext;/*** 手动获取bean** author gblfy* date 2021-09-17*/
public abstract class SpringBootBeanAutowiringSupport {private static final Logger logger LoggerFactory.getLogger(SpringBootBeanAutowiringSupport.class);/*** This constructor performs injection on this instance,* based on the current web application context.* pIntended for use as a base class.** see #processInjectionBasedOnCurrentContext*/public SpringBootBeanAutowiringSupport() {System.out.println(SpringBootBeanAutowiringSupport.SpringBootBeanAutowiringSupport);processInjectionBasedOnCurrentContext(this);}/*** Process {code Autowired} injection for the given target object,* based on the current web application context.* pIntended for use as a delegate.** param target the target object to process* see org.springframework.web.context.ContextLoader#getCurrentWebApplicationContext()*/public static void processInjectionBasedOnCurrentContext(Object target) {Assert.notNull(target, Target object must not be null);WebApplicationContext cc WebApplicationContextLocator.getCurrentWebApplicationContext();if (cc ! null) {AutowiredAnnotationBeanPostProcessor bpp new AutowiredAnnotationBeanPostProcessor();bpp.setBeanFactory(cc.getAutowireCapableBeanFactory());bpp.processInjection(target);} else {if (logger.isDebugEnabled()) {logger.debug(Current WebApplicationContext is not available for processing of ClassUtils.getShortName(target.getClass()) : Make sure this class gets constructed in a Spring web application. Proceeding without injection.);}}}
}
这样就完成了
二、XFIRE 发布服务
2.1. 运行项目 2.2. 异常解决
但是在启动的时候遇到Attribute “singleton” must be declared for element type “bean”. 移步跳转即可解决 Attribute “singleton” must be declared for element type “bean”.
移步跳转即可解决 遇到cannot convert value of type ‘org.codehaus.xfire.spring.editors.ServiceFactoryEditor’ cannot convert value of type ‘org.codehaus.xfire.spring.editors.ServiceFactoryEditor
2.3. 测试验证
http://localhost:8080//webservice/xfireServiceShell?wsdl
三、XFIRE客户端
package com.gblfy.ws.client;import org.codehaus.xfire.client.Client;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;import java.net.URL;Component
public class XFireClient {private final static Logger log LoggerFactory.getLogger(XFireClient.class);public static void main(String[] args) throws Exception {String xfireUrl http://localhost:8080/xfire/xfireServiceShell?wsdl;String namespaceURI http://impl.service.ws.gblfy.com;//单个参数String method sayHello;//多参// String method sayHello2;String reqXml 1;String reqXml2 2;//调用服务XFireClient.xfireSendMsg(xfireUrl, namespaceURI, method, reqXml);// XFireClient.xfireSendMsg(xfireUrl, namespaceURI, method, reqXml, reqXml2);}/*** 单参调用工具类** param xfireUrl url地址* param method 调用方法名* param reqXml 发送报文体* return res 返回结果* throws Exception 若有异常在控制台输出异常并将异常抛出*/public static String xfireSendMsg(String xfireUrl, String namespaceURI, String method, String reqXml) throws Exception {// 创建服务Client client new Client(new URL(xfireUrl));// 设置调用的方法和方法的命名空间client.setProperty(namespaceURI, method);// 通过映射获得结果Object[] result new Object[0];try {result client.invoke(method, new Object[]{reqXml});} catch (Exception e) {e.printStackTrace();throw e;}String xml (String) result[0];log.info(响应报文 : {}, xml);return xml;}/*** 多参调用工具类Object类型** param xfireUrl url地址* param method 调用方法名* param reqXml 发送报文体* return res 返回结果* throws Exception 若有异常在控制台输出异常并将异常抛出*/public static String xfireSendMsg(String xfireUrl, String namespaceURI, String method, String reqXml, String reqXml2) throws Exception {// 创建服务Client client new Client(new URL(xfireUrl));// 设置调用的方法和方法的命名空间client.setProperty(namespaceURI, method);// 通过映射获得结果Object[] result new Object[0];try {result client.invoke(method, new Object[]{reqXml, reqXml2});} catch (Exception e) {e.printStackTrace();throw e;}String xml (String) result[0];log.info(响应报文 : {}, xml);return xml;}
}
开源源码.
https://gitee.com/gblfy/unified-access-center