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

相关文章

RabbitMQ使用及与spring boot整合

1.MQ   消息队列(Message Queue,简称MQ)——应用程序和应用程序之间的通信方法   应用:不同进程Process/线程Thread之间通信   比较流行的中间件:     ActiveMQ     RabbitMQ(非常重量级,更适合于企业级的开发)     Kafka(高吞吐量的分布式发布订阅消息系统)     RocketMQ   在高并发、可靠性、成熟度等

Spring Boot 入门篇

一、简介 Spring Boot是一款开源的Java Web应用框架,旨在简化Spring应用的初始搭建以及开发过程。它整合了Spring技术栈中的诸多关键组件,为开发者提供了一种快速、简便的Spring应用开发方式。Spring Boot遵循“约定优于配置”的原则,通过自动配置、起步依赖和内置的Servlet容器,极大地简化了传统Spring应用的配置和部署过程。 二、Spring Boot

springboot整合swagger2之最佳实践

来源:https://blog.lqdev.cn/2018/07/21/springboot/chapter-ten/ Swagger是一款RESTful接口的文档在线自动生成、功能测试功能框架。 一个规范和完整的框架,用于生成、描述、调用和可视化RESTful风格的Web服务,加上swagger-ui,可以有很好的呈现。 SpringBoot集成 pom <!--swagge

springboot 整合swagger

没有多余废话,就是干 spring-boot 2.7.8 springfox-boot-starter 3.0.0 结构 POM.xml <?xml version="1.0" encoding="UTF-8"?><project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/

Spring Boot集成Tess4J实现OCR

1.什么是Tess4j? Tesseract是一个开源的光学字符识别(OCR)引擎,它可以将图像中的文字转换为计算机可读的文本。支持多种语言和书面语言,并且可以在命令行中执行。它是一个流行的开源OCR工具,可以在许多不同的操作系统上运行。Tess4J是一个基于Tesseract OCR引擎的Java接口,可以用来识别图像中的文本,说白了,就是封装了它的API,让Java可以直接调用。 Tess

部署若依Spring boot项目

nohup和& nohup命令解释 nohup命令:nohup 是 no hang up 的缩写,就是不挂断的意思,但没有后台运行,终端不能标准输入。nohup :不挂断的运行,注意并没有后台运行的功能,就是指,用nohup运行命令可以使命令永久的执行下去,和用户终端没有关系,注意了nohup没有后台运行的意思;&才是后台运行在缺省情况下该作业的所有输出都被重定向到一个名为nohup.o

使用Spring Boot集成Spring Data JPA和单例模式构建库存管理系统

引言 在企业级应用开发中,数据库操作是非常重要的一环。Spring Data JPA提供了一种简化的方式来进行数据库交互,它使得开发者无需编写复杂的JPA代码就可以完成常见的CRUD操作。此外,设计模式如单例模式可以帮助我们更好地管理和控制对象的创建过程,从而提高系统的性能和可维护性。本文将展示如何结合Spring Boot、Spring Data JPA以及单例模式来构建一个基本的库存管理系统

Spring Boot集成PDFBox实现电子签章

概述 随着无纸化办公的普及,电子文档的使用越来越广泛。电子签章作为一种有效的身份验证方式,在很多场景下替代了传统的纸质文件签名。Apache PDFBox 是一个开源的Java库,可以用来渲染、生成、填写PDF文档等操作。本文将介绍如何使用Spring Boot框架结合PDFBox来实现电子签章功能。 准备工作 环境搭建:确保你的开发环境中安装了JDK 8或更高版本,并且配置好了Maven或

uniapp,vite整合windicss

官方文档:https://weapp-tw.icebreaker.top/docs/quick-start/frameworks/hbuilderx 安装: npm i -D tailwindcss postcss autoprefixer# 初始化 tailwind.config.js 文件npx tailwindcss initnpm i -D weapp-tailwindcss# 假

spring boot 创建no-web应用

1. 问题 不是所有的Spring应用都必须是web应用(或web服务)。如果你想在main方法中执行一些代码,但需要启动一个Spring应用去设置需要的底层设施,那使用Spring Boot的SpringApplication特性可以很容易实现。 spring boot绝大多数用于web应用,但是有时我们只想用spring boot启动容器、使用它的一些特性,单并不想启动一个web服务,如何