Springboot项目发布WebService接口(基于CXF)
pom依赖
<!-- CXF webservice -->
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-spring-boot-starter-jaxws</artifactId>
<version>3.3.2</version>
</dependency>
配置类
package com.webservice.outservice.serviceReleaseConfig;
import com.webservice.outservice.impl.SSOWebServiceImpl;
import com.webservice.outservice.interfaces.ICzpWebService;
import com.webservice.outservice.impl.CzpWebServiceImpl;
import com.webservice.outservice.interfaces.ISSOWebService;
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.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import javax.xml.ws.Endpoint;
/**
* 配置并发布
*/
@Configuration
public class WebServiceConfig {
@Autowired
private Bus bus;
/**
* 此方法作用是改变项目中服务名的前缀名,此处127.0.0.1或者localhost不能访问时,请使pconfig查看本机ip来访问
* 此方法被注释后:wsdl访问地址为:http://127.0.0.1:8090/services/czp/czpWebService?wsdl
* 去掉注释后:wsdl访问地址为:http://127.0.0.1:8090/netOrder/czp/czpWebService?wsdl
*/
@Bean
public ServletRegistrationBean disServlet() {
ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(
new CXFServlet(), "/netOrder/*");
return servletRegistrationBean;
}
@Bean(name = Bus.DEFAULT_BUS_ID)
public SpringBus Bus()
{
return new SpringBus();
}
@Bean
public ICzpWebService czpService() {
return new CzpWebServiceImpl();
}
@Bean
public ISSOWebService ssoService() {
return new SSOWebServiceImpl();
}
@Bean
/**
* 操作票相关服务发布
*/
public Endpoint endpointCzp() {
EndpointImpl endpoint = new EndpointImpl(bus, czpService());
endpoint.publish("/czp/czpWebService");
return endpoint;
}
@Bean
/**
* 单点登录相关服务发布
*/
public Endpoint endpointSSO() {
EndpointImpl endpoint = new EndpointImpl(bus, ssoService());
endpoint.publish("/sso/ssoWebService");
return endpoint;
}
}
接口服务实现类示例
package com.webservice.outservice.impl;
import com.webservice.outservice.interfaces.ICzpWebService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import javax.jws.WebService;
@Slf4j
@Service("czpWebService")
//对外发布的服务名
@WebService(serviceName="czpWebService",
//指定你想要的名称空间,通常使用使用包名反转
targetNamespace="http://service.czpservice.com",
//服务接口全路径, 指定做SEI(Service EndPoint Interface)服务端点接口
endpointInterface= "com.webservice.outservice.interfaces.ICzpWebService")
@Component
public class CzpWebServiceImpl implements ICzpWebService {
@Override
public String reliefOfAShift(String param){
return null;
}
}
最终效果,访问(http://127.0.0.1:8090/netOrder/czp/czpWebService?wsdl)
Springboot项目访问WebService接口的两种形式
-
基于axis的接口访问(古老一点)
pom依赖
<!-- axis 1.4 jar start -->
<dependency>
<groupId>org.apache.axis</groupId>
<artifactId>axis</artifactId>
<version>1.4</version>
</dependency>
<dependency>
<groupId>axis</groupId>
<artifactId>axis-jaxrpc</artifactId>
<version>1.4</version>
</dependency>
<dependency>
<groupId>commons-discovery</groupId>
<artifactId>commons-discovery</artifactId>
<version>0.2</version>
</dependency>
<dependency>
<groupId>wsdl4j</groupId>
<artifactId>wsdl4j</artifactId>
<version>1.6.3</version>
</dependency>
<!-- axis 1.4 jar end -->
访问示例
Service service = new Service();
Call call = (Call) service.createCall();
//设置调用地址
call.setTargetEndpointAddress(thirdPartyUrlConfig.getOmsTwoServiceUrl());
//设置调用方法和命名空间
call.setOperationName(
new QName(
thirdPartyUrlConfig.getOmsTwoNameSpaceUrl(),
thirdPartyUrlConfig.getOmsTwoMethodName()
)
);
//设置入参类型,多个入参依次按照arg0,arg1,arg2 ...
call.addParameter("arg0", XMLType.XSD_STRING, ParameterMode.IN);
//设置返回值类型
call.setReturnClass(String.class);
//方法调用
xml = (String)call.invoke(new Object[]{inparam});
-
基于cxf的接口访问
pom依赖同上面发布接口的依赖
调用示例
//创建工厂实例
JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
//设置调用url,获取动态调用客户端
Client client = dcf.createClient(thirdPartyUrlConfig.getOmsTwoServiceUrl());
//构建入参报文
String inParam = buildParam();
//调用接口
Object[] objects = client.invoke(thirdPartyUrlConfig.getOmsTwoMethodName(),inParam);
//返回值
xml = objects[0].toString();