1.Web Services是什么?
(1)从表面上看,WebService就是一个应用程序,它向外界暴露出一个能够通过Web进行调用的API。这就是说,你能够用编程的方法通过Web调用来实现某个功能的应用程序。从深层次上看,Web Service是一种新的Web应用程序分支,它们是自包含、自描述、模块化的应用,可以在网络(通常为Web)中被描述、发布、查找以及通过Web来调用。
(2)Web Services 主要利用 HTTP 和 SOAP 协议使业务数据在 Web 上传输,SOAP通过 HTTP 调用业务对象执行远程功能调用,Web 用户能够使用 SOAP 和 HTTP通过 Web 调用的方法来调用远程对象的。
2.Web Services平台的元素有哪些?
(1)SOAP:基本的 Web services 平台是 XML + HTTP。
- SOAP 指简易对象访问协议
- SOAP 是一种通信协议
- SOAP 用于应用程序之间的通信
- SOAP 是一种用于发送消息的格式
- SOAP被设计用来通过因特网进行通信
- SOAP 独立于平台
- SOAP 独立于语言
- SOAP 基于 XML
- SOAP 很简单并可扩展
- SOAP允许您绕过防火墙
- SOAP 将作为 W3C 标准来发展
(2)WSDL:基于 XML 的用于描述 Web Services 以及如何访问 Web Services 的语言。
- WSDL 指网络服务描述语言
- WSDL 使用 XML 编写
- WSDL 是一种 XML 文档
- WSDL 用于描述网络服务
- WSDL也可用于定位网络服务
- WSDL 还不是 W3C 标准
(3)UDDI:是一种目录服务,通过它,企业可注册并搜索 Web services。
- UDDI 指通用的描述、发现以及整合(Universal Description, Discovery and Integration)。
- UDDI 是一种用于存储有关 web services 的信息的目录。
- UDDI 是一种由 WSDL 描述的网络服务接口目录。
- UDDI经由 SOAP 进行通迅。 UDDI 被构建于 Microsoft .NET 平台之中。
2.Web Services结合Springboot的案例
(1)接口
@WebService(name = "EventEnbidService")
public interface EventEnbidService {
@WebMethod(operationName = "getEventEnbid")
@WebResult(name = "getEventEnbid")
EventResponse getEventEnbid(@WebParam(name = "enbid1") String enbid1, @WebParam(name = "enbid2") String enbid2,
@WebParam(name = "enbid3") String enbid3, @WebParam(name = "enbid4") String enbid4,
@WebParam(name = "enbid5") String enbid5, @WebParam(name = "enbid6") String enbid6);
}
(2)接口实现类
@Service
public class EventEnbidServiceImpl implements EventEnbidService{
@Resource
private EventMapper eventMapper;
@Override
public EventResponse getEventEnbid(String enbid1, String enbid2, String enbid3, String enbid4, String enbid5, String enbid6) {
Set<EventParam> set = eventMapper.selectEventBaseByEnodebid(enbid1, enbid2, enbid3, enbid4, enbid5, enbid6);
return new EventResponse(200,"success",set);
}
}
(3)返回数据类
@XmlRootElement
public class EventResponse {
private int code;
private String msg;
@XmlElement
private Set<EventParam> data;
public EventResponse() {
}
public EventResponse(int code, String msg, Set<EventParam> data) {
this.code = code;
this.msg = msg;
this.data = data;
}
public int getCode() {
return code;
}
public void setCode(int code) {
this.code = code;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public Object getData() {
return data;
}
public void setData(Set<EventParam> data) {
this.data = data;
}
}
(4)配置类
@Configuration
public class WebServiceConfig {
/**
*/
@Autowired
private WarningEnbidService warningEnbidService;
@Autowired
private TextEnbidService textEnbidService;
@Autowired
private EventEnbidService eventEnbidService;
@Bean(name = Bus.DEFAULT_BUS_ID)
public SpringBus springBus() {
return new SpringBus();
}
@Bean
public ServletRegistrationBean cxfServlet() {
return new ServletRegistrationBean(new CXFServlet(),"/webservice/*");
}
/**
* 注册WebServiceDemoService接口到webservice服务
* @return
*/
@Bean
public Endpoint sweptPayEndpoint(){
EndpointImpl endpoint = new EndpointImpl(springBus(), eventEnbidService);
endpoint.publish("/eventEnbidService");
return endpoint;
}
}
(5)测试
测试工具:soapui
接口地址:http://localhost:8989/webservice/eventEnbidService?wsdl
接口报文:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ser="http://service.webservice.pv.com/">
<soapenv:Header/>
<soapenv:Body>
<ser:getEventEnbid>
<!--Optional:-->
<enbid1>450561</enbid1>
<!--Optional:-->
<enbid2>450564</enbid2>
<!--Optional:-->
<enbid3>450574</enbid3>
<!--Optional:-->
<enbid4>450575</enbid4>
<!--Optional:-->
<enbid5>450576</enbid5>
<!--Optional:-->
<enbid6>450577</enbid6>
</ser:getEventEnbid>
</soapenv:Body>
</soapenv:Envelope>
返回报文:
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<ns2:getEventEnbidResponse xmlns:ns2="http://service.webservice.pv.com/">
<getEventEnbid>
<data>
<enodebName>aa</enodebName>
<type>高考</type>
</data>
<data>
<enodebName>bb</enodebName>
<type>高考</type>
</data>
<data>
<enodebName>cc</enodebName>
<type>高考</type>
</data>
<data>
<enodebName>dd</enodebName>
<type>高考</type>
</data>
<data>
<enodebName>ee$[01030092]</enodebName>
<type>高考</type>
</data>
<data>
<enodebName>ff</enodebName>
<type>高考</type>
</data>
<code>200</code>
<msg>success</msg>
</getEventEnbid>
</ns2:getEventEnbidResponse>
</soap:Body>
</soap:Envelope>