外观
webservice
437字约1分钟
2020-09-06
简介
WebService
- 使用XML编解码数据,使用SOAP传输数据
- 三种基本元素:SOAP、WSDL、UUDI
WSDL
- 基于XML描述WebService的一种语言,描述接口列表和信息
- WSDL文档
- 定义WebService执行的操作
- 定义WebService执行操作的参数(消息)
- 定义WebService使用的数据类型
- 定义消息的格式和细节
<definitions> <types> definition of types........ </types> <binding> definition of a binding.... </binding> <message name="getTermRequest"> <part name="term" type="xs:string"/> </message> <message name="getTermResponse"> <part name="value" type="xs:string"/> </message> <portType name="glossaryTerms"> <operation name="getTerm"> <input message="getTermRequest"/> <output message="getTermResponse"/> </operation> </portType> </definitions> - WSDL端口
- WSDL绑定
- WSDL语法
SOAP
- 使程序可用基于HTTP传输XML的协议,是一种传输协议。
RSS
CXF框架
- SpringBoot + cxf
- 引包
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxws</artifactId>
<version>3.2.6</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http</artifactId>
<version>3.2.6</version>
</dependency>- cxf配置
@Configuration
publicclassCxfConfig {
@Bean
publicServletRegistrationBean<CXFServlet>dispatcherServlet(){
ServletRegistrationBean<CXFServlet>servletRegistrationBean=newServletRegistrationBean<>(newCXFServlet(),"/dpkshinterface/*");
servletRegistrationBean.setName("webService");
return servletRegistrationBean;
}
@Bean(name=Bus.DEFAULT_BUS_ID)
publicSpringBusspringBus(){
return new SpringBus();
}
@Bean
publicDpkshGzOrderWebServicedpkshGzOrderJsonService(){
return new DpkshGzOrderWebServiceImpl();
}
@Bean
publicEndpointendpoint(){
EndpointImplendpoint=newEndpointImpl(springBus(),dpkshGzOrderJsonService());
endpoint.publish("/api");
endpoint.getInInterceptors().add(newWsInterceptor());//addwebserviceinteceptor
returnendpoint;
}
}- 拦截器
publicclassWsInterceptorextendsAbstractLoggingInterceptor{
privatestaticfinalorg.slf4j.LoggerLOGGER=LoggerFactory.getLogger(WsInterceptor.class);
publicWsInterceptor(){
super(Phase.RECEIVE);
}
@Override
publicvoidhandleMessage(Messagemessage)throwsFault{
InputStreamis=message.getContent(InputStream.class);
if(is!=null){
CachedOutputStreambos=newCachedOutputStream();
if(threshold>0){
bos.setThreshold(threshold);
}
try{
InputStreambis=isinstanceofDelegatingInputStream?((DelegatingInputStream)is).getInputStream():is;
IOUtils.copyAtLeast(bis,bos,limit==-1?Integer.MAX_VALUE:limit);
bos.flush();
bis=newSequenceInputStream(bos.getInputStream(),bis);
if(isinstanceofDelegatingInputStream){
((DelegatingInputStream)is).setInputStream(bis);
}else{
message.setContent(InputStream.class,bis);
}
bos.close();
}catch(Exceptione){
thrownewFault(e);
}finally{
LOGGER.info(bos.toString());
}
}
}
@Override
protectedLoggergetLogger(){
returnnull;
}
}- 客户端生成
wsdl2java -p com.jc -d D:\demo_project\src -client -encoding utf-8 -noAddressBinding http://10.62.233.167:8080/dpkshinterface/api?wsdl
Axis2
- 报文 http://localhost:8080/testaxis2webservice/services/Testservice?wsdl
- 客户端
@Test
public void test() {
try {
RPCServiceClient sender = new RPCServiceClient();
// 连接
String loc = "http://localhost/myWS/services/AxisService";
EndpointReference er = new EndpointReference(loc);
Options options = sender.getOptions();
options.setTimeOutInMilliSeconds(2*20000L);
options.setTo(er);
// 调用
String targetNamespace = "http://service.jc.com";
String name = "sayHello";
QName qName = new QName(targetNamespace, name);
Object[] params = new Object[]{"客户端调用成功!"};
Class<?>[] types = new Class[]{String.class};
Object[] respObj = sender.invokeBlocking(qName, params, types);
System.out.println(respObj[0]);
} catch (AxisFault e) {
e.printStackTrace();
}
}