webService简写为ws,也就是web服务,是基于HTTP协议的,通过HTTP进行传输的
使用 JDK 发布 WS
package com.cxf.a;
import javax.jws.WebService;
//@WebService说明该接口是一个 WS 接口(称为“Endpoint,端点”),其中的方法是 WS 方法(称为“Operation,操作”)
@WebService
public interface HelloService {
public String say(String name);
}
package com.cxf.a;
import javax.jws.WebService;
@WebService(
serviceName = "HelloService",
portName = "HelloServicePort",
endpointInterface = "com.cxf.a.HelloService"
)
public class HelloServiceImpl implements HelloService {
public String say(String name) {
return "hello " + name;
}
}
package com.cxf.a;
import javax.xml.ws.Endpoint;
public class Server {
public static void main(String[] args) {
String address = "http://localhost:8080/ws/soap/hello";
HelloService helloService = new HelloServiceImpl();
Endpoint.publish(address, helloService);
System.out.println("ws is published");
}
}
引入依赖
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http-jetty</artifactId>
<version>2.4.6</version>
</dependency>
只需要以上三个类,即可发布一个服务(使用jetty作为服务容器,故,必须引入jetty的相关jar)
http://localhost:8080/ws/soap/hello?wsdl
http://localhost:8080/ws/soap/hello/say/arg0/yuruixin
打开上述网址,若出现xml以下内容即为发布成功
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<ns2:sayResponse xmlns:ns2="http://a.cxf.com/">
<return>hello yuruixin</return>
</ns2:sayResponse>
</soap:Body>
</soap:Envelope>
JAX-WS 有一个官方实现,就是上面提到的 JAX-WS RI,它是 Oracle 公司提供的实现,而 Apache 旗下的 Axis 与 CXF 也同样实现了该规范。Axis 相对而言更加老牌一些,而 CXF 的前世就是 XFire,它是一款著名的 WS 框架,擅长与 Spring 集成
从本质上讲,JAX-WS 是基于 SOAP 的,而 SOAP 的全称是 Simple Object Access Protocol(简单对象访问协议)
本系列笔记学习对象为开源中国https://my.oschina.net/huangyong/blog