org.springframework.boot
spring-boot-starter-web-services
org.apache.cxf
cxf-rt-frontend-jaxws
3.2.0
org.apache.cxf
cxf-rt-transports-http
3.2.0
package com.example.demo.service;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService;
/**
* @author: Yinlei
* Package: com.example.demo.service
* @date: 2023-10-18 8:40
* @Description: webservice测试
* @version: 1.0
*/
@WebService(name = "HelloWebService",
targetNamespace = "http://helloWebService.service.demo.example.com")
public interface HelloWebService {
@WebMethod
@WebResult(name = "resultName")
String get(@WebParam(name = "name") String name);
}
package com.example.demo.service.Impl;
import com.example.demo.service.HelloWebService;
import org.springframework.stereotype.Service;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService;
/**
* @author: Yinlei
* Package: com.example.demo.service.Impl
* @date: 2023-10-18 8:46
* @Description:
* @version: 1.0
*/
@Service
@WebService(name = "HelloWebService",
targetNamespace = "http://helloWebService.service.demo.example.com", //命名空间,一般是对应的路径反过来
endpointInterface = "com.example.demo.service.HelloWebService") //实现接口的地址
public class HelloWebServiceImpl implements HelloWebService {
@Override
public String get( String name) {
return name;
}
}
package com.example.demo.config;
import com.example.demo.service.HelloWebService;
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: Yinlei
* Package: com.example.demo.config
* @date: 2023-10-18 10:09
* @Description: Cxf配置类
* @version: 1.0
*/
@Configuration
public class CxfConfig {
@Autowired
private HelloWebService helloWebService;
@Bean
public ServletRegistrationBean disServlet(){
return new ServletRegistrationBean(new CXFServlet(),"/webService/*"); //webservice下的请求将有CXFServlet处理
}
@Bean(name = Bus.DEFAULT_BUS_ID)
public SpringBus springBus(){
return new SpringBus();
}
@Bean
public Endpoint endpoint(){
EndpointImpl endpoint = new EndpointImpl(springBus(), helloWebService);
endpoint.publish("/helloWebservice");
return endpoint;
}
}
@GetMapping("/get1")
public String get1() throws MalformedURLException {
//创建WSDL文件的URL,这个参数为暴露webervice的网址
URL wsdlLocation = new URL("http://localhost:9000/webService/helloWebservice?wsdl");
//创建服务名称:第一个参数为命名空间地址,第二个参数 为本地部分的命名 HelloWebServiceImplService 这个是对应的实现类名加上Service
QName serviceName = new QName("http://helloWebService.service.demo.example.com", "HelloWebServiceImplService");
Service service = Service.create(wsdlLocation, serviceName);
//获取服务实现类 参数为对应的服务接口.class
HelloWebService port = service.getPort(HelloWebService.class);
//调用方法
String asd = port.get("asd");
return asd;
}
Writed By -知识浅谈