依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web-services</artifactId>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-spring-boot-starter-jaxws</artifactId>
<version>3.3.4</version>
</dependency>
<dependency>
<groupId>org.hibernate.validator</groupId>
<artifactId>hibernate-validator</artifactId>
</dependency>
接口
package com.dp.tools;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;
@WebService(name = "WsService",targetNamespace = "http://tools.dp.com")
public interface WsService {
@WebMethod
public Object mWbss(@WebParam(name = "name") String name);
}
接口实现
package com.dp.tools;
import org.springframework.stereotype.Service;
import javax.jws.WebService;
@Service
@WebService(name = "WsService", endpointInterface = "com.dp.tools.WsService",targetNamespace = "http://tools.dp.com")
public class WsServiceImpl implements WsService{
@Override
public Object mWbss(String name) {
System.out.println("进入服务1webservice接口");
return name;
}
}
配置
package com.dp.tools;
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;
/**
* @author xzy
* @date 2020-03-15 17:49
* 说明:服务端发布相关接口
*/
@Configuration
public class CxfConfig {
@Autowired
private WsService wbService;
/**
* 注入servlet bean name不能dispatcherServlet 否则会覆盖dispatcherServlet
* @return
*/
@Bean(name = "cxfServlet")
public ServletRegistrationBean cxfServlet() {
return new ServletRegistrationBean(new CXFServlet(),"/ws/*");
}
@Bean(name = Bus.DEFAULT_BUS_ID)
public SpringBus springBus() {
return new SpringBus();
}
/**
* 注册WebServiceDemoService接口到webservice服务
* @return
*/
@Bean(name = "WebServiceDemoEndpoint")
public Endpoint sweptPayEndpoint() {
EndpointImpl endpoint = new EndpointImpl(springBus(), wbService);
endpoint.publish("/mservice");
return endpoint;
}
}
访问路径:http://localhost:8802/ws/mservice?wsdl
接口暴露成功
访问暴露的接口get请求测试:
@GetMapping("wss")
public String wss(){
JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
Client client = dcf.createClient("http://localhost:8802/ws/mservice?wsdl");
// client.getOutInterceptors().add(new ClientLoginInterceptor(USER_NAME,PASS_WORD));
Object[] objects = new Object[0];
try {
// invoke("方法名",参数1,参数2,参数3....);
objects = client.invoke("mWbss", "传递的参数");
System.out.println("返回数据:" + objects[0]);
} catch (java.lang.Exception e) {
e.printStackTrace();
}
return "ok";
}