SpringBoot集成WebService(wsdl)

Meta39 2024-07-27 17:03:01 阅读 51

pom.xml

<code> <dependency>

<groupId>com.fasterxml.jackson.dataformat</groupId>

<artifactId>jackson-dataformat-xml</artifactId>

</dependency>

<dependency>

<groupId>org.apache.cxf</groupId>

<artifactId>cxf-spring-boot-starter-jaxws</artifactId>

<!-- 对版本没要求,建议跟我一样 -->

<version>3.4.4</version>

</dependency>

创建入口

ApplicationContextUtils.java

bean调用工具

import org.springframework.beans.BeansException;

import org.springframework.context.ApplicationContext;

import org.springframework.context.ApplicationContextAware;

import org.springframework.stereotype.Component;

/**

* 创建日期:2024-07-01

*/

@Component

public class ApplicationContextUtils implements ApplicationContextAware {

//构造函数私有化,防止其它人实例化该对象

private ApplicationContextUtils(){ }

private static ApplicationContext applicationContext;

@Override

public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {

ApplicationContextUtils.applicationContext = applicationContext;

}

//通过name获取 Bean.(推荐,因为bean的name是唯一的,出现重名的bean启动会报错。)

public static Object getBean(String name) {

return applicationContext.getBean(name);

}

//通过class获取Bean.(确保bean的name不会重复。因为可能会出现在不同包的同名bean导致获取到2个实例)

public static <T> T getBean(Class<T> clazz) {

return applicationContext.getBean(clazz);

}

//通过name,以及Clazz返回指定的Bean(这个是最稳妥的)

public static <T> T getBean(String name, Class<T> clazz) {

return applicationContext.getBean(name, clazz);

}

}

IWebService.java

统一入口

public interface IWebService {

String handle(String parameter);

}

WebServiceEntry.java

import lombok.extern.slf4j.Slf4j;

import org.springframework.stereotype.Service;

import javax.jws.WebMethod;

import javax.jws.WebParam;

import javax.jws.WebService;

/**

* 创建日期:2024-07-01

*/

@Slf4j

@Service

@WebService

public class WebServiceEntry {

/**

* 反射调用方法

* @param service bean name

* @param parameter 请求参数字符串,一般是xml格式的字符串

*/

@WebMethod

public String invoke(@WebParam(name = "service") String service,

@WebParam(name = "parameter") String parameter) {

IWebService webService = (IWebService) ApplicationContextUtils.getBean(service);

return webService.handle(parameter);

}

}

WebServiceConfig.java

配置类

有些依赖千万不要导错,所以我依赖都粘贴进来了。防止导错包。

import org.apache.cxf.Bus;

import org.apache.cxf.bus.spring.SpringBus;

import org.apache.cxf.jaxws.EndpointImpl;

import org.apache.cxf.transport.servlet.CXFServlet;

import org.springframework.boot.web.servlet.ServletRegistrationBean;

import org.springframework.context.annotation.Bean;

import org.springframework.context.annotation.Configuration;

import javax.xml.ws.Endpoint;

/**

* 创建日期:2024-07-01

*/

@Configuration

public class WebServiceConfig {

@Bean(name = "cxfServlet")

public ServletRegistrationBean<?> cxfServlet() {

//urlMappings默认是:services

return new ServletRegistrationBean<>(new CXFServlet(), "/services/*");

}

@Bean(name = Bus.DEFAULT_BUS_ID)

public SpringBus springBus() {

return new SpringBus();

}

@Bean

public Endpoint helloServiceEndpoint() {

EndpointImpl endpoint = new EndpointImpl(springBus(), new WebServiceEntry());

//services后面的uri地址

endpoint.publish("/WebServiceEntry");

return endpoint;

}

}

WebMvcConfig.java

web的配置类,因为增加了xml依赖,springboot会默认把json放到xml后面,因此要手动改回默认json。

import org.springframework.context.annotation.Configuration;

import org.springframework.http.MediaType;

import org.springframework.web.servlet.config.annotation.ContentNegotiationConfigurer;

import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration

public class WebMvcConfig implements WebMvcConfigurer {

@Override

public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {

//引入 jackson-dataformat-xml 后,原本默认返回json变成了默认返回xml。因此这里要设置默认返回json

configurer.defaultContentType(MediaType.APPLICATION_JSON);

}

}

实现IWebService接口

如:WebServiceImpl

@Service("Hello")

public class Hello implements IWebService {

@Override

public String handle(String parameter) {

return "hello";

}

}

启动SpringBoot

访问

http://localhost:8080/services/WebServiceEntry?wsdl

会出现如下所示界面

在这里插入图片描述

用soapUI去调用接口

ws="http://ws.bsjkt.bsoft.com/"这里每个人可能不一样

service=bean name

parameter=请求参数

<code><soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ws="http://ws.bsjkt.bsoft.com/">code>

<soapenv:Header/>

<soapenv:Body>

<ws:invoke>

<service>Hello</service>

<parameter><![CDATA[

<Data>

</Data>

]]></parameter>

</ws:invoke>

</soapenv:Body>

</soapenv:Envelope>



声明

本文内容仅代表作者观点,或转载于其他网站,本站不以此文作为商业用途
如有涉及侵权,请联系本站进行删除
转载本站原创文章,请注明来源及作者。