SpringBoot 整合WebService
简介:WebService是一个SOA(面向服务的编程)的架构,它是不依赖于语言,平台等,可以实现不同的语言间的相互调用,采用XML格式封装数据,HTTP协议传输数据,是把本地的功能开发出去供别人远程调用的类,缺点是性能差。WebService的RPC实现有四种方式:Axis2、CXF、Jersey、Hessian等,Axis2客户端是可以调用CXF服务端的。
SOAP(Simple Object Access Protocol):简单对象存储协议,是XML Web Service的通讯协议,当用户通过UDDI找到WSDL描述文档后,他通过SOAP调用你建立的Web服务中的一个或多个操作,SOAP是XML文档形式的调用方法的规范。
WSDL(Web Services Description Language):该文件是一个XML文档,用于说明一组SOAP消息以及如何交换这些信息,大多数情况下由软件自动生成和使用。
1. pom.xml Maven依赖
<!-- cxf框架 -->
<dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-spring-boot-starter-jaxws</artifactId> <version>3.3.7</version> </dependency>
2. 注解
@WebService:标识该接口是个WebService服务;
name:用来定义该webservice暴露的服务名称;
targetNamespace属性用来定义命名空间,一般是接口的包名倒序,注意接口和实现类保持一致;
serviceName:与接口中name保持一致;
enpointInterface:接口地址。
@WebMethod:标识接口中的方法是用来提供具体的服务的。
3. 接口层
package logcollector.service; import javax.jws.WebMethod; import javax.jws.WebParam; import javax.jws.WebService; import java.util.Map; @WebService(name = "CommonService", targetNamespace = "ruhuanxingyun.com") public interface CommonService { @WebMethod Map<String, String> sayHello(@WebParam(name = "param") String param); }
4. 接口实现层
package logcollector.service; import org.springframework.stereotype.Service; import javax.jws.WebService; import java.util.HashMap; import java.util.Map;
import com.alibaba.fastjson.JSONObject; @WebService(serviceName = "CommonService", targetNamespace = ”com", endpointInterface = "service.CommonService") @Service public class CommonServiceImpl implements CommonService {
@Override
public String sayHello(String param) {
Map<String, String> map = new HashMap<>();
map.put("接收参数", param);
map.put("返回参数", "已收到");
return JSONObject.toJSONString(map);
}
}
5. cxf服务配置
package logcollector.config; import logcollector.service.CommonService; import org.apache.cxf.Bus; 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; @Configuration public class CxfConfig { @Autowired private Bus bus; @Autowired private CommonService commonService; @Bean("cxfServletRegistration") public ServletRegistrationBean dispatcherServlet() { // 注册servlet 拦截/ruhuanxingyun/*开头的请求,不设置默认是/services/*,可以用配置文件cxf.path: /ruhuanxingyun/*替代 return new ServletRegistrationBean(new CXFServlet(), "/ruhuanxingyun/*"); } @Bean public Endpoint endpoint() { EndpointImpl endpoint = new EndpointImpl(bus, commonService); // 发布地址 endpoint.publish("/CommonService"); return endpoint; }
// 多个服务发布多个endpoint
/*@Bean
public Endpoint endpoint2() {
EndpointImpl endpoint = new EndpointImpl(bus, otherService);
// 发布地址
endpoint.publish("/OtherService");
return endpoint;
}*/
}
6. cxf 客户端(可以用WebServiceTemplate调用)
package com.ruhuanxingyun.util;
import org.apache.commons.codec.binary.Base64;
import org.apache.cxf.endpoint.Client;
import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;
import org.apache.cxf.transport.http.HTTPConduit;
import org.apache.cxf.transports.http.configuration.HTTPClientPolicy;
import javax.xml.ws.handler.MessageContext;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class CxfClient {
public static void main(String[] args) {
// 创建动态客户端
JaxWsDynamicClientFactory jwdcf = JaxWsDynamicClientFactory.newInstance();
Client client = jwdcf.createClient("http://localhost:9003/demo/CommonService?wsdl");
// 服务权限
String auth = "ruhuanxingyun:ruhuanxingyun@999999";
byte[] encodedAuth = Base64.encodeBase64(auth.getBytes(StandardCharsets.UTF_8));
Map<String, List<String>> headers = new HashMap<>(2);
headers.put("Authorization", Arrays.asList("Basic " + new String(encodedAuth)));
client.getRequestContext().put(MessageContext.HTTP_REQUEST_METHOD, headers);
HTTPConduit conduit = (HTTPConduit) client.getConduit();
HTTPClientPolicy httpClientPolicy = new HTTPClientPolicy();
// 连接超时
httpClientPolicy.setConnectionTimeout(60000);
// 取消块编码
httpClientPolicy.setAllowChunking(false);
// 响应超时
httpClientPolicy.setReceiveTimeout(60000);
conduit.setClient(httpClientPolicy);
try {
// 多参数采用json数据格式传递,返回值最好用json
Object[] objects = client.invoke("sayHello", "123");
System.out.println("获取值为:" + objects[1].toString());
} catch (Exception e) {
e.printStackTrace();
}
}
}
可参考:SpringBoot 集成WebServiceTemplate使用