【案例】Springboot开发WebService服务端和客户端环境说明
Java JDK 1.8、Spring boot 2.1.6、Apache CXF 3.1.6
POM依赖
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.1.6</version>
</dependency>
<dependency>
<groupId>f</groupId>
<artifactId>cxf-rt-frontend-jaxws</artifactId>
<version>3.1.6</version>
</dependency>
<dependency>
<groupId>f</groupId>
<artifactId>cxf-rt-transports-http</artifactId>
<version>3.1.6</version>
</dependency>
</dependencies>
服务端
webService接⼝
使⽤@WebService声明式注解声明这是⼀个webService接⼝,并设置:
name:服务名称
targetNamespace:命名空间,通常是接⼝的包名倒序
注解@WebMethod是声明接⼝⽅法,可以通过operationName为接⼝⽅法设置别名,默认是⽅法名。
此外他还有⼀个参数exclude,为true时则忽略该⽅法作为webService接⼝⽅法,默认为false。
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService;
@WebService(name = "TestService",
targetNamespace = "server.dandelion")
public interface TestService {
@WebMethod(operationName="getData")
@WebResult(targetNamespace = "server.dandelion")
String execute(@WebParam(name = "action") String action, @WebParam(name = "msg") String msg);
}
创建了webService接⼝TestService,其中⽅法execute有两个参数分别为action和msg,⽤注解@WebParam进⾏声明。
接⼝实现
创建接⼝实现类TestServiceImpl实现TestService接⼝,其中endpointInterface为webService接⼝的路径。
import javax.jws.WebService;
@WebService(serviceName = "TestService"
targetNamespace = "server.dandelion",
endpointInterface = "com.dandelion.server.TestService")
public class TestServiceImpl implements TestService{
@Override
public String execute(String action, String msg) {
System.out.println(String.format("action: %s", action));
System.out.println(String.format("msg: %s", msg));
return "<root><code>1</code><msg>同步成功</msg></root>";
}
}
注册接⼝
创建webService的配置类WebServiceConfig,将接⼝服务注⼊容器并进⾏发布。
import com.dandelion.server.TestServiceImpl;
import f.Bus;
import f.bus.spring.SpringBus;
import f.jaxws.EndpointImpl;
import f.transport.servlet.CXFServlet;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import t.annotation.Bean;
import t.annotation.Configuration;
l.ws.Endpoint;
@Configuration
public class WebServiceConfig {
@Bean
public ServletRegistrationBean<CXFServlet> disServlet() {调用webservice服务
ServletRegistrationBean<CXFServlet> servletRegistrationBean = new ServletRegistrationBean<>();
servletRegistrationBean.setServlet(new CXFServlet());
servletRegistrationBean.addUrlMappings("/webService/*");
return servletRegistrationBean;
}
@Bean(name = Bus.DEFAULT_BUS_ID)
public SpringBus springBus() {
return new SpringBus();
}
@Bean
public Endpoint endpoint(SpringBus springBus) {
EndpointImpl endpoint = new EndpointImpl(springBus, new TestServiceImpl());
endpoint.publish("/testService");
return endpoint;
}
}
项⽬启动效果:
SoapUI测试
客户端
JAX动态调⽤WebService⼯具类
import f.endpoint.Client;
import dpoint.dynamic.JaxWsDynamicClientFactory;
public class WsClientUtil {
/**
* 调⽤webservice服务
* @param wsdUrl 服务地址
* @param operationName ⽅法名称
* @param params 参数
* @return 服务响应结果
*/
public static String callWebService(String wsdUrl, String operationName, params){ JaxWsDynamicClientFactory dcf = wInstance();
Client client = ateClient(wsdUrl);
try {
Object[] objects = client.invoke(operationName, params);
return objects[0].toString();
} catch (Exception e) {
e.printStackTrace();
}
return "";
}
}
API测试
@RestController
@RequestMapping(value = "/client")
public class TestClientController {
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
@RequestMapping(value = "test")
public String test(){
//在获取连接之前还原上下⽂
Thread.currentThread().setContextClassLoader(classLoader);
// 服务地址
String url = "localhost:9010/webService/testService?wsdl";
// ⽅法名称
String methodName = "getData";
// 参数1
String action = "同步⽤户信息";
// 参数2
String msg = "<user><id>10012334223</id><name>蒲公英不是梦</name><sex>1</sex></user>"; try {
// 发起请求
return WsClientUtil.callWebService(url, methodName, action, msg);
} catch (Exception e) {
return String.format("webService调⽤异常:%s", e.getMessage());
}
}
}
效果
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论