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

相关文章

Spring Boot整合消息队列RabbitMQ的实现示例

《SpringBoot整合消息队列RabbitMQ的实现示例》本文主要介绍了SpringBoot整合消息队列RabbitMQ的实现示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的... 目录RabbitMQ 简介与安装1. RabbitMQ 简介2. RabbitMQ 安装Spring

Spring Boot 中正确地在异步线程中使用 HttpServletRequest的方法

《SpringBoot中正确地在异步线程中使用HttpServletRequest的方法》文章讨论了在SpringBoot中如何在异步线程中正确使用HttpServletRequest的问题,... 目录前言一、问题的来源:为什么异步线程中无法访问 HttpServletRequest?1. 请求上下文与线

在 Spring Boot 中使用异步线程时的 HttpServletRequest 复用问题记录

《在SpringBoot中使用异步线程时的HttpServletRequest复用问题记录》文章讨论了在SpringBoot中使用异步线程时,由于HttpServletRequest复用导致... 目录一、问题描述:异步线程操作导致请求复用时 Cookie 解析失败1. 场景背景2. 问题根源二、问题详细分

Spring Boot 3 整合 Spring Cloud Gateway实践过程

《SpringBoot3整合SpringCloudGateway实践过程》本文介绍了如何使用SpringCloudAlibaba2023.0.0.0版本构建一个微服务网关,包括统一路由、限... 目录引子为什么需要微服务网关实践1.统一路由2.限流防刷3.登录鉴权小结引子当前微服务架构已成为中大型系统的标

SpringBoot整合easy-es的详细过程

《SpringBoot整合easy-es的详细过程》本文介绍了EasyES,一个基于Elasticsearch的ORM框架,旨在简化开发流程并提高效率,EasyES支持SpringBoot框架,并提供... 目录一、easy-es简介二、实现基于Spring Boot框架的应用程序代码1.添加相关依赖2.添

SpringBoot中整合RabbitMQ(测试+部署上线最新完整)的过程

《SpringBoot中整合RabbitMQ(测试+部署上线最新完整)的过程》本文详细介绍了如何在虚拟机和宝塔面板中安装RabbitMQ,并使用Java代码实现消息的发送和接收,通过异步通讯,可以优化... 目录一、RabbitMQ安装二、启动RabbitMQ三、javascript编写Java代码1、引入

spring-boot-starter-thymeleaf加载外部html文件方式

《spring-boot-starter-thymeleaf加载外部html文件方式》本文介绍了在SpringMVC中使用Thymeleaf模板引擎加载外部HTML文件的方法,以及在SpringBoo... 目录1.Thymeleaf介绍2.springboot使用thymeleaf2.1.引入spring

在 Spring Boot 中使用 @Autowired和 @Bean注解的示例详解

《在SpringBoot中使用@Autowired和@Bean注解的示例详解》本文通过一个示例演示了如何在SpringBoot中使用@Autowired和@Bean注解进行依赖注入和Bean... 目录在 Spring Boot 中使用 @Autowired 和 @Bean 注解示例背景1. 定义 Stud

Spring Boot整合log4j2日志配置的详细教程

《SpringBoot整合log4j2日志配置的详细教程》:本文主要介绍SpringBoot项目中整合Log4j2日志框架的步骤和配置,包括常用日志框架的比较、配置参数介绍、Log4j2配置详解... 目录前言一、常用日志框架二、配置参数介绍1. 日志级别2. 输出形式3. 日志格式3.1 PatternL

如何使用Spring boot的@Transactional进行事务管理

《如何使用Springboot的@Transactional进行事务管理》这篇文章介绍了SpringBoot中使用@Transactional注解进行声明式事务管理的详细信息,包括基本用法、核心配置... 目录一、前置条件二、基本用法1. 在方法上添加注解2. 在类上添加注解三、核心配置参数1. 传播行为(