boot整合xfire

2024-03-27 17:04
文章标签 整合 boot xfire

本文主要是介绍boot整合xfire,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

最近换了项目组,框架使用的boot整合的xfire,之前没使用过xfire,所以写个例子记录下,看 前辈的帖子 整理下

pom文件     

<parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.4.2</version><relativePath/> <!-- lookup parent from repository -->
</parent>
<!-- webservice start -->
<dependency><groupId>org.codehaus.xfire</groupId><artifactId>xfire-all</artifactId><version>1.2.6</version>
</dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web-services</artifactId>
</dependency>
<!-- webservice end -->

定义XfireServlet

package com.example.demo.config;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;@Configuration
public class XfireServlet {@Beanpublic ServletRegistrationBean registrationBean(){ServletRegistrationBean registrationBean=new ServletRegistrationBean();registrationBean.addUrlMappings("/webservice/*");registrationBean.setServlet(new XFireSpringServlet());return registrationBean;}
}

创建boot-xfire.xml文件

其中<context:component-scan base-package="" />路径对应的@WebService的所在位置

 

<?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/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-package="com.example.demo.webservice.impl" /><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>

创建XfireConfig文件 引入配置文件

package com.example.demo.config;import org.springframework.context.annotation.ImportResource;
import org.springframework.stereotype.Component;@ImportResource(locations = {"classpath:config/boot-xfire.xml"})
@Component
public class XfireConfig {
}

创建WebApplicationContextLocator文件

package com.example.demo.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;@Configuration
public class WebApplicationContextLocator implements ServletContextInitializer {private static WebApplicationContext webApplicationContext;public static WebApplicationContext getWebApplicationContext(){return webApplicationContext;}@Overridepublic void onStartup(ServletContext servletContext) throws ServletException {webApplicationContext= WebApplicationContextUtils.getWebApplicationContext(servletContext);}
}

创建webservice

package com.example.demo.webservice;import javax.jws.WebService;@WebService
public interface UserWebService {String queryAgeLarge(int age);}

创建webservice实现类

package com.example.demo.webservice.impl;import com.example.demo.entity.User;
import com.example.demo.mapper.UserMapper;
import com.example.demo.webservice.UserWebService;
import org.json.JSONArray;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;import javax.jws.WebService;
import javax.xml.ws.BindingType;
import javax.xml.ws.soap.SOAPBinding;
import java.util.List;/*** serviceName: 请求时的地址* name: 无用,与serviceName一致* targetNamespace: 命名空间 一般是包路径反过来*/
@WebService(serviceName = "userWebService",name = "userWebService",targetNamespace = "http://impl.webservice.demo.example.com")
@BindingType(value = SOAPBinding.SOAP12HTTP_BINDING)
@Service
public class UserWebServiceImpl implements UserWebService {@Autowiredprivate UserMapper userMapper;@Overridepublic String queryAgeLarge(int age) {//todoList<User> userList = userMapper.queryAgeLarge(age);JSONArray jsonArray = new JSONArray(userList);String json = jsonArray.toString();return returnXml("200",json);}private String returnXml(String code,String data){return "<resp><code>"+code+"</code>"+"<data>"+data+"</data>"+"</resp>";}
}

项目启动,但是报错

报错一

Offending resource: class path resource [config/boot-xfire.xml]; nested exception is org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException: Line 10 in XML document from class path resource [org/codehaus/xfire/spring/xfire.xml] is invalid; nested exception is org.xml.sax.SAXParseException; lineNumber: 10; columnNumber: 24; 

Attribute "singleton" must be declared for element type "bean".

解决:用好压打开本地仓库的 org\codehaus\xfire\xfire-all\1.2.6 路径的 xfire-all-1.2.6.jar 包,将xfire.xml和xfireXmlBeans.xml文件的属性singletnotallow="true"​删除,保存后更新

报错二

Cannot convert value of type 'org.codehaus.xfire.spring.editors.ServiceFactoryEditor' to required type 'java.lang.Class' for property 'customEditors[org.codehaus.xfire.service.ServiceFactory]': PropertyEditor [org.springframework.beans.propertyeditors.ClassEditor] returned inappropriate value of type 'org.codehaus.xfire.spring.editors.ServiceFactoryEditor'

解决:用好压打开本地仓库的 org\codehaus\xfire\xfire-all\1.2.6 路径的 xfire-all-1.2.6.jar 包,将customEditors.xml文件的<map></map>标签内信息换成 <entry key="org.codehaus.xfire.service.ServiceFactory" value="org.codehaus.xfire.spring.editors.ServiceFactoryEditor"></entry>  保存后更新

接口发布

项目启动,浏览器访问

 接口调用

XfireClient类
package com.example.demo.config;import lombok.extern.slf4j.Slf4j;
import org.codehaus.xfire.client.Client;
import org.springframework.stereotype.Component;import java.net.URL;@Component
@Slf4j
public class XfireClient {public static String xfireSendMsg(String xfireUrl, String namespaceURI, String method, int 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;}
}
controller类
package com.example.demo.controller;import com.example.demo.config.XfireClient;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;@RestController
@RequestMapping("/testXfire")
@Slf4j
public class TestXfireController {@Autowiredprivate XfireClient client;@RequestMapping("/test")public String test() throws Exception {String queryAgeLarge = client.xfireSendMsg("http://localhost:8888//webservice/userWebService?wsdl","http://impl.webservice.demo.example.com","queryAgeLarge",1);log.info("输出日志={}",queryAgeLarge);return queryAgeLarge;}
}
postman调用

可见xml信息正常接收到!

不足之处,还请之处!!!

这篇关于boot整合xfire的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/852827

相关文章

JAVA系统中Spring Boot应用程序的配置文件application.yml使用详解

《JAVA系统中SpringBoot应用程序的配置文件application.yml使用详解》:本文主要介绍JAVA系统中SpringBoot应用程序的配置文件application.yml的... 目录文件路径文件内容解释1. Server 配置2. Spring 配置3. Logging 配置4. Ma

SpringBoot 整合 Grizzly的过程

《SpringBoot整合Grizzly的过程》Grizzly是一个高性能的、异步的、非阻塞的HTTP服务器框架,它可以与SpringBoot一起提供比传统的Tomcat或Jet... 目录为什么选择 Grizzly?Spring Boot + Grizzly 整合的优势添加依赖自定义 Grizzly 作为

springboot整合gateway的详细过程

《springboot整合gateway的详细过程》本文介绍了如何配置和使用SpringCloudGateway构建一个API网关,通过实例代码介绍了springboot整合gateway的过程,需要... 目录1. 添加依赖2. 配置网关路由3. 启用Eureka客户端(可选)4. 创建主应用类5. 自定

springboot整合 xxl-job及使用步骤

《springboot整合xxl-job及使用步骤》XXL-JOB是一个分布式任务调度平台,用于解决分布式系统中的任务调度和管理问题,文章详细介绍了XXL-JOB的架构,包括调度中心、执行器和Web... 目录一、xxl-job是什么二、使用步骤1. 下载并运行管理端代码2. 访问管理页面,确认是否启动成功

解决mybatis-plus-boot-starter与mybatis-spring-boot-starter的错误问题

《解决mybatis-plus-boot-starter与mybatis-spring-boot-starter的错误问题》本文主要讲述了在使用MyBatis和MyBatis-Plus时遇到的绑定异常... 目录myBATis-plus-boot-starpythonter与mybatis-spring-b

Jenkins中自动化部署Spring Boot项目的全过程

《Jenkins中自动化部署SpringBoot项目的全过程》:本文主要介绍如何使用Jenkins从Git仓库拉取SpringBoot项目并进行自动化部署,通过配置Jenkins任务,实现项目的... 目录准备工作启动 Jenkins配置 Jenkins创建及配置任务源码管理构建触发器构建构建后操作构建任务

SpringBoot整合kaptcha验证码过程(复制粘贴即可用)

《SpringBoot整合kaptcha验证码过程(复制粘贴即可用)》本文介绍了如何在SpringBoot项目中整合Kaptcha验证码实现,通过配置和编写相应的Controller、工具类以及前端页... 目录SpringBoot整合kaptcha验证码程序目录参考有两种方式在springboot中使用k

Spring Boot 中整合 MyBatis-Plus详细步骤(最新推荐)

《SpringBoot中整合MyBatis-Plus详细步骤(最新推荐)》本文详细介绍了如何在SpringBoot项目中整合MyBatis-Plus,包括整合步骤、基本CRUD操作、分页查询、批... 目录一、整合步骤1. 创建 Spring Boot 项目2. 配置项目依赖3. 配置数据源4. 创建实体类

详解Spring Boot接收参数的19种方式

《详解SpringBoot接收参数的19种方式》SpringBoot提供了多种注解来接收不同类型的参数,本文给大家介绍SpringBoot接收参数的19种方式,感兴趣的朋友跟随小编一起看看吧... 目录SpringBoot接受参数相关@PathVariable注解@RequestHeader注解@Reque

Spring Boot实现多数据源连接和切换的解决方案

《SpringBoot实现多数据源连接和切换的解决方案》文章介绍了在SpringBoot中实现多数据源连接和切换的几种方案,并详细描述了一个使用AbstractRoutingDataSource的实... 目录前言一、多数据源配置与切换方案二、实现步骤总结前言在 Spring Boot 中实现多数据源连接