SpringBoot整合xfire发布webSerivce详细教程

tech2022-08-04  157

1.导入xfire的依赖包xfire-all,会自动导入相关依赖包,其中spring可能会与项目本身的spring冲突,需要将其排除依赖

<dependency> <groupId>org.codehaus.xfire</groupId> <artifactId>xfire-all</artifactId> <version>1.2.6</version> <!--排除冲突的依赖包--> <exclusions> <exclusion> <groupId>javax.activation</groupId> <artifactId>activation</artifactId> </exclusion> <exclusion> <groupId>org.springframework</groupId> <artifactId>spring</artifactId> </exclusion> </exclusions> </dependency>

2.注入XFireSpringServlet

@Bean public ServletRegistrationBean registrationBean() { System.out.println("servletRegistrationBean----------"); ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(); servletRegistrationBean.setServlet(new XFireSpringServlet()); servletRegistrationBean.addUrlMappings("/webservice/*"); return servletRegistrationBean; }

相当于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"> <servlet> <servlet-name>xfireServlet</servlet-name> <servlet-class>org.codehaus.xfire.spring.XFireSpringServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>xfireServlet</servlet-name> <url-pattern>/webservice/*</url-pattern> </servlet-mapping> </web-app>

3.创建一个xml文件

<?xml version="1.0" encoding="UTF-8" ?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd"> //扫描被@webService的包 <context:component-scan base-package="com.itender.ms.webService.impl" /> <!-- XFire start --> <import resource="classpath:org/codehaus/xfire/spring/xfire.xml" /> <!--<import resource="xfire.xml" />--> <bean id="webAnnotations" class="org.codehaus.xfire.annotations.jsr181.Jsr181WebAnnotations" /> <bean id="jsr181HandlerMapping" class="org.codehaus.xfire.spring.remoting.Jsr181HandlerMapping"> <property name="xfire" ref="xfire" /> <property name="webAnnotations" ref="webAnnotations" /> </bean> </beans>

4.使用@ImportResource注入xml

@ImportResource(locations = {"classpath:config/applicationContext.xml"}) @Component public class XfireConfig { }

5.创建@WebService接口及实现类

//webService接口 @WebService public interface IOrgCheckWebService { } //实现类 //继承SpringBootBeanAutowiringSupport 可以让@Autowired注入成功,我重写了WebApplicationContextLocator的onStartup方法 @WebService(serviceName = "orgCheckWebService",name = "orgCheckWebService", targetNamespace = "http://impl.webService.ms.itender.com") @BindingType(value = SOAPBinding.SOAP12HTTP_BINDING) @Service public class OrgCheckWebServiceImpl extends SpringBootBeanAutowiringSupport implements IOrgCheckWebService { }

这样就完成了,但是在启动的时候会报错,原因是Spring4.0和xfire1.2.6有版本冲突。修改 第一个错误:Attribute “singleton” must be declared for element type “bean”. 因为:xfire定义bean的时候,用了 singleton 属性,Spring4.0取消了singletion 解决方案:找到xfire-spring-1.2.6.jar中的org/codehaus/xfire/spring的xfire.xml和和xfireXmlBeans.xml,将里边的singleton属性删除 第二个错误:cannot convert value of type ‘org.codehaus.xfire.spring.editors.ServiceFactoryEditor’ 解决方案:修改xfire-spring-1.2.6.jar中的org/codehaus/xfire/spring/customEditors.xml文件,将< map>< /map>中的内容替换成下面的内容:

<entry key="org.codehaus.xfire.service.ServiceFactory" value="org.codehaus.xfire.spring.editors.ServiceFactoryEditor"></entry>

即将源文件的

<map> <entry key="org.codehaus.xfire.service.ServiceFactory"> <bean class="org.codehaus.xfire.spring.editors.ServiceFactoryEditor"> <property name="transportManager" ref="xfire.transportManager" /> </bean> </entry> </map>

替换成

<map> <entry key="org.codehaus.xfire.service.ServiceFactory" value="org.codehaus.xfire.spring.editors.ServiceFactoryEditor"></entry> </map>

这样,在启动项目就可以成功了,如下图: 注意:修改xml文件的时候可以使用360解压直接打开,然后修改内容,我之前傻乎乎的直接解压出来后修改,之后再重新打成了jar包。。。

最新回复(0)