SpringBoot WebService服务端客户端使用教程
服务端:
依赖
<!-- webservice相关依赖--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web-services</artifactId></dependency><dependency><groupId>org.apache.cxf</groupId><artifactId>cxf-rt-frontend-jaxws</artifactId><version>3.2.0</version></dependency><dependency><groupId>org.apache.cxf</groupId><artifactId>cxf-rt-transports-http</artifactId><version>3.2.0</version></dependency>
服务接口
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 {@Overridepublic String get( String name) {return name;}
}
cxf配置类
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 {@Autowiredprivate HelloWebService helloWebService;@Beanpublic ServletRegistrationBean disServlet(){return new ServletRegistrationBean(new CXFServlet(),"/webService/*"); //webservice下的请求将有CXFServlet处理}@Bean(name = Bus.DEFAULT_BUS_ID)public SpringBus springBus(){return new SpringBus();}@Beanpublic 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 这个是对应的实现类名加上ServiceQName serviceName = new QName("http://helloWebService.service.demo.example.com", "HelloWebServiceImplService");Service service = Service.create(wsdlLocation, serviceName);//获取服务实现类 参数为对应的服务接口.classHelloWebService port = service.getPort(HelloWebService.class);//调用方法String asd = port.get("asd");return asd;
}
Writed By -知识浅谈