springboot 整合 webservice服务
阿里云国内75折 回扣 微信号:monov8 |
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6 |
目录
在前一段时间用到了webservice服务今天整理了一下记录下来。
webservice百科
Web Service是一个平台独立的低耦合的自包含的、基于可编程的web的应用程序可使用开放的XML标准通用标记语言下的一个子集标准来描述、发布、发现、协调和配置这些应用程序用于开发分布式的交互操作的应用程序。 [1]
Web Service技术 能使得运行在不同机器上的不同应用无须借助附加的、专门的第三方软件或硬件 就可相互交换数据或集成。依据Web Service规范实施的应用之间 无论它们所使用的语言、 平台或内部协议是什么 都可以相互交换数据。Web Service是自描述、 自包含的可用网络模块 可以执行具体的业务功能。Web Service也很容易部署 因为它们基于一些常规的产业标准以及已有的一些技术诸如标准通用标记语言下的子集XML、HTTP。Web Service减少了应用接口的花费。
简单来说就是可以让各个系统相互调用获取信息。以前用的很多现在不是很常见一般都是与一些老系统交互才会用到。
创建一个springboot项目并导入相关依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http</artifactId>
<version>3.3.3</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxws</artifactId>
<version>3.3.3</version>
</dependency>
编写webservice接口
package com.jpl.service;
import javax.jws.WebMethod;
import javax.jws.WebService;
/**
* @author xla
* @date 2023-01-07 13:22
* targetNamespace 命名空间一般为当前接口包名的倒序
*/
@WebService(targetNamespace="http://service.jpl.com/")
public interface WsService {
@WebMethod(operationName= "say" )
public String say(String name);
}
编写实现类
package com.jpl.service.impl;
import com.jpl.service.WsService;
import javax.jws.WebService;
/**
* @author xla
* @date 2023-01-07 13:25
* targetNamespace 与当前类实现的接口中的targetNamespace要一致
* serviceName 对外暴露的服务名
* endpointInterface 是当前类实现接口类的地址
*/
@WebService(targetNamespace="http://service.jpl.com/",endpointInterface="com.jpl.service.WsService",serviceName = "CrmWs")
public class WsServiceImpl implements WsService {
@Override
public String say(String name) {
System.out.println(name);
StringBuffer result = new StringBuffer();
result.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
result.append("<root>");
result.append("<returnCode>0</returnCode>");
result.append("<message>调用成功</message>");
result.append("<name>");
result.append(name);
result.append("</name>");
result.append("</root>");
return result.toString();
}
}
发布webservice
package com.jpl.config;
import com.jpl.service.WsService;
import com.jpl.service.impl.WsServiceImpl;
import org.apache.cxf.Bus;
import org.apache.cxf.bus.spring.SpringBus;
import org.apache.cxf.jaxws.EndpointImpl;
import org.apache.cxf.transport.servlet.CXFServlet;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import javax.xml.ws.Endpoint;
/**
* @author xla
* @date 2023-01-07 13:44
* @deprecated 发布webservice
*/
@Configuration
public class CxfConfig {
@Bean(name = Bus.DEFAULT_BUS_ID)
public SpringBus springBus() {
return new SpringBus();
}
@Bean(name = "wsBean")
public ServletRegistrationBean dispatcherServlet() {
ServletRegistrationBean wbsServlet = new ServletRegistrationBean(new CXFServlet(),
"/service/*"); //找到webservice接口中targetNamespace的值并去掉启动类之前的包名将剩下来包名填到这里
return wbsServlet;
}
@Bean
public WsService userService() {
return new WsServiceImpl();
}
@Bean
public Endpoint endpointPurchase(SpringBus springBus, WsService WsService) {
EndpointImpl endpoint = new EndpointImpl(springBus(), userService());
endpoint.publish("/CrmWs");
System.out.println("服务发布成功地址为http://localhost:1000/wbse/service/CrmWs?wsdl");
return endpoint;
}
}
浏览器访问
服务发布成功后在浏览器中输入地址即可以看到所有的方法
postman调用
请求地址
http://localhost:1000/wbse/service/CrmWs?wsdl
请求体
<Envelope xmlns="http://schemas.xmlsoap.org/soap/envelope/">
<Body>
<say xmlns="http://service.jpl.com/">
<arg0 xmlns="">"17364062985"</arg0>
</say>
</Body>
</Envelope>
<Envelope xmlns="http://schemas.xmlsoap.org/soap/envelope/">
<Body>
<方法名 xmlns="命名空间">
<arg0 xmlns="">"17364062985"</arg0>
</方法名>
</Body>
</Envelope>