介绍
示例
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web-services</artifactId>
</dependency>
<!-- CXF webservice -->
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-spring-boot-starter-jaxws</artifactId>
<version>3.4.3</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http</artifactId>
<version>3.4.3</version>
</dependency>
@Configuration
public class WebServiceConfig {
@Bean(name = Bus.DEFAULT_BUS_ID)
public SpringBus springBus() {
return new SpringBus();
}
@Bean
public Endpoint endpoint(HelloService helloService) {
EndpointImpl endpoint = new EndpointImpl(springBus(), helloService);
endpoint.publish("/v1/demo");
return endpoint;
}
}
@WebService(name = "HelloService", targetNamespace = "http://service.server.webservice.example.com")
public interface HelloService {
@WebMethod
String hello(@WebParam String msg);
}
@Service
@WebService(name = "HelloService", targetNamespace = "http://service.server.webservice.example.com",
endpointInterface = "com.example.webservice.server.service.HelloService")
public class HelloServiceImpl implements HelloService {
@Override
public String hello(String msg) {
return "Hello, " + msg;
}
}
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web-services</artifactId>
</dependency>
<!-- CXF webservice -->
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-spring-boot-starter-jaxws</artifactId>
<version>3.4.3</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http</artifactId>
<version>3.4.3</version>
</dependency>
public class MyClient {
public static void main(String[] args) throws Exception {
test1();
test2();
test3();
}
private static void test1() {
JaxWsProxyFactoryBean proxy = new JaxWsProxyFactoryBean();
proxy.setAddress("http://localhost:8080/services/v1/demo?wsdl");
proxy.setServiceClass(HelloService.class);
HelloService service = (HelloService) proxy.create();
String response = service.hello("World");
System.out.println("test1返回结果:" + response);
}
private static void test2() throws Exception {
JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
Client client = dcf.createClient("http://localhost:8080/services/v1/demo?wsdl");
Object[] response = client.invoke("hello", "World");
System.out.println("test2返回结果:" + response[0]);
}
private static void test3() {
HelloServiceImplService service = new HelloServiceImplService();
String response = service.getHelloServicePort().hello("World");
System.out.println("test3返回结果:" + response);
}
}
- 码云 https://gitee.com/hweiyu/spring-boot-webservice.git