Skip to content

Commit

Permalink
Spring Boot 集成 WebService
Browse files Browse the repository at this point in the history
  • Loading branch information
zhouzhaodong committed Sep 30, 2020
1 parent 1687f6d commit 1d76053
Show file tree
Hide file tree
Showing 7 changed files with 212 additions and 0 deletions.
55 changes: 55 additions & 0 deletions spring-boot-webservice/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>xyz.zhouzhaodong</groupId>
<artifactId>spring-boot-webservice</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>spring-boot-webservice</name>
<description>Demo project for Spring Boot</description>

<properties>
<java.version>1.8</java.version>
</properties>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-spring-boot-starter-jaxws</artifactId>
<version>3.4.0</version>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package xyz.zhouzhaodong.springbootwebservice;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class SpringBootWebserviceApplication {

public static void main(String[] args) {
SpringApplication.run(SpringBootWebserviceApplication.class, args);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package xyz.zhouzhaodong.springbootwebservice.client;

import org.apache.cxf.endpoint.Client;
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;
import xyz.zhouzhaodong.springbootwebservice.service.CommonService;

import java.util.Arrays;
import java.util.List;

/**
* @author zhouzhaodong
*/
public class CxfClient {

public static void main(String[] args) {
cl2();
}

/**
* 方式1.代理类工厂的方式,需要拿到对方的接口
*/
public static void cl1() {
try {
// 接口地址
String address = "http://localhost:8080/services/CommonService?wsdl";
// 代理工厂
JaxWsProxyFactoryBean jaxWsProxyFactoryBean = new JaxWsProxyFactoryBean();
// 设置代理地址
jaxWsProxyFactoryBean.setAddress(address);
// 设置接口类型
jaxWsProxyFactoryBean.setServiceClass(CommonService.class);
// 创建一个代理接口实现
CommonService cs = (CommonService) jaxWsProxyFactoryBean.create();
// 数据准备
String userName = "Leftso";
// 调用代理接口的方法调用并返回结果
String result = cs.sayHello(userName);
System.out.println("返回结果:" + result);
} catch (Exception e) {
e.printStackTrace();
}
}

/**
* 方式2.动态调用方式
*/
public static void cl2() {

// 创建动态客户端
JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
Client client = dcf.createClient("http://localhost:8080/services/CommonService?wsdl");
// 需要密码的情况需要加上用户名和密码
// client.getOutInterceptors().add(new ClientLoginInterceptor(USER_NAME, PASS_WORD));
Object[] objects;
try {
objects = client.invoke("sayHello", "1");
List<Object> objects1 = Arrays.asList(objects);
System.out.println(objects1.get(0));
} catch (java.lang.Exception e) {
e.printStackTrace();
}

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package xyz.zhouzhaodong.springbootwebservice.config;

import javax.xml.ws.Endpoint;

import org.apache.cxf.Bus;
import org.apache.cxf.jaxws.EndpointImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import xyz.zhouzhaodong.springbootwebservice.service.CommonService;

/**
* cxf配置类
* @author zhouzhaodong
*/
@Configuration
public class CxfConfig {
@Autowired
private Bus bus;

@Autowired
CommonService commonService;

/** JAX-WS **/
@Bean
public Endpoint endpoint() {
EndpointImpl endpoint = new EndpointImpl(bus, commonService);
//发布地址
endpoint.publish("/CommonService");
return endpoint;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package xyz.zhouzhaodong.springbootwebservice.service;

import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;

/**
* @author zhouzhaodong
*/
@WebService(name = "CommonService", // 暴露服务名称
targetNamespace = "http://springbootwebservice.zhouzhaodong.xyz/"// 命名空间,一般是接口的包名倒序
)
public interface CommonService {

/**
* 暴露接口
* @param name
* @return
*/
@WebMethod
String sayHello(@WebParam(name = "userName") String name);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package xyz.zhouzhaodong.springbootwebservice.service.impl;

import org.springframework.stereotype.Component;
import xyz.zhouzhaodong.springbootwebservice.service.CommonService;

import javax.jws.WebService;

/**
* @author zhouzhaodong
*/
@WebService(serviceName = "CommonService", // 与接口中指定的name一致
targetNamespace = "http://springbootwebservice.zhouzhaodong.xyz/", // 与接口中的命名空间一致,一般是接口的包名倒
endpointInterface = "xyz.zhouzhaodong.springbootwebservice.service.CommonService"// 接口地址
)
@Component
public class CommonServiceImpl implements CommonService {

@Override
public String sayHello(String name) {
return "HELLO" + name;
}
}
1 change: 1 addition & 0 deletions spring-boot-webservice/src/main/resources/application.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

0 comments on commit 1d76053

Please sign in to comment.