webservice 即web服务,它是一种跨编程语言和跨操作系统平台的远程调用技术。
三种WebService 规范,分别是JAX-WS(JAX-RPC)、JAXM&SAAJ、JAX-RS
- jax-ws:基于soap协议,传输xml格式数据。
- jax-rs: 基于rest风格的webservice,是基于http协议的一个应用层,既可以传输xml数据,也可以传输json数据。
SOAP协议 = 在HTTP的基础上+XML数据
wsdl说明书: webservice definition language,基于XML(标准通用的标记语言),既是机器可阅读又是人可阅读的语言
UDDI 是一种目录服务,企业可以通过 UDDI 来注册和搜索 Web 服务
优缺点:
- 优点:解决了跨语言、跨平台的服务调用以及功能复用
- jax-ws是基于soap协议,soap是基于xml传输的,xml会传输一些无关内容而影响效率(解读xml标签)
发布JAX-WS服务步骤:采用Apache CXF (Apache CXF 是一个开源的 Services 框架)
- 添加cxf依赖
- 写服务接口
- 写服务接口实现
- 发布服务
服务端
添加cxf依赖
<dependencies>
<!-- 要进行jaxws 服务开发 -->
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxws</artifactId>
<version>3.0.1</version>
</dependency>
<!-- 内置jetty web服务器 ,如果发布到tomcat环境,则这个就不需要了 -->
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http-jetty</artifactId>
<version>3.0.1</version>
</dependency>
<!-- 日志实现 -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.12</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.10</version>
<scope>test</scope>
</dependency>
</dependencies>
写服务接口 (需要@WebService注解)
import javax.jws.WebService;
/**
* @Description: 对外发布服务的接口
*/
@WebService
public interface HelloService {
/**
* 对外发布服务接口的方法
*/
public String sayHello(String name);
}
写服务接口实现
import com.hxm.service.HelloService;
public class HelloServiceImpl implements HelloService {
@Override
public String sayHello(String name) {
return name+" ,welcome to hxm!";
}
}
发布服务
import com.hxm.service.impl.HelloServiceImpl;
import org.apache.cxf.jaxws.JaxWsServerFactoryBean;
public class Server {
public static void main(String[] args) {
//发布服务工厂
JaxWsServerFactoryBean factory = new JaxWsServerFactoryBean();
//设置服务地址
factory.setAddress("http://localhost:8008/ws/hello");
//设置服务类
factory.setServiceBean(new HelloServiceImpl());
//添加日志输入、输出拦截器,观察soap请求、soap响应内容
factory.getInInterceptors().add(new LoggingInInterceptor());
factory.getOutInterceptors().add(new LoggingOutInterceptor());
//发布服务
factory.create();
System.out.println("发布服务成功,端口为8008");
//查看wsdl说明:http://localhost:8008/ws/hello?wsdl
}
客户端
添加cxf依赖
<dependencies>
<!-- 要进行jaxws 服务开发 -->
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxws</artifactId>
<version>3.0.1</version>
</dependency>
<!-- 内置jetty web服务器 ,如果发布到tomcat环境,则这个就不需要了 -->
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http-jetty</artifactId>
<version>3.0.1</version>
</dependency>
<!-- 日志实现 -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.12</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.10</version>
<scope>test</scope>
</dependency>
</dependencies>
写服务接口 (需要@WebService注解)
import javax.jws.WebService;
/**
* @Description: 对外发布服务的接口
*/
@WebService
public interface HelloService {
/**
* 对外发布服务接口的方法
*/
public String sayHello(String name);
}
调用服务
import com.hxm.service.HelloService;
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
public class Client {
public static void main(String[] args) {
//创建cxf代理工厂
JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
//设置远程访问服务器地址
factory.setAddress("http://localhost:8008/ws/hello");
//设置接口类型
factory.setServiceClass(HelloService.class);
//对接口生成代理对象
HelloService helloService = factory.create(HelloService.class);
//代理对象对象 class com.sun.proxy.$Proxy34
// [java代理:1.静态代理 2.动态代理(jdk接口代理:$Proxy34,cglib子类代理:$CGLIB34)]
System.out.println(helloService.getClass());
//远程访问服务端方法
String content = helloService.sayHello("LinHao");
System.out.println(content);
}
}
结果:服务端日志内容
2021-02-10 12:08:28,575 0 [ main] INFO y.ReflectionServiceFactoryBean - Creating Service {http://impl.service.hxm.com/}HelloServiceImplService from class com.hxm.service.HelloService
2021-02-10 12:08:28,986 411 [ main] INFO apache.cxf.endpoint.ServerImpl - Setting the server's publish address to be http://localhost:8008/ws/hello
2021-02-10 12:08:29,018 443 [ main] INFO rg.eclipse.jetty.server.Server - jetty-8.1.15.v20140411
2021-02-10 12:08:29,361 786 [ main] INFO jetty.server.AbstractConnector - Started SelectChannelConnector@localhost:8008
2021-02-10 12:09:08,373 0 [ main] INFO y.ReflectionServiceFactoryBean - Creating Service {http://service.hxm.com/}HelloServiceService from class com.hxm.service.HelloService
2021-02-10 12:09:09,211 40636 [qtp652433136-20] INFO loServiceImplPort.HelloService - Inbound Message
----------------------------
ID: 1
Address: http://localhost:8008/ws/hello
Encoding: UTF-8
Http-Method: POST
Content-Type: text/xml; charset=UTF-8
Headers: {Accept=[*/*], Cache-Control=[no-cache], connection=[keep-alive], Content-Length=[193], content-type=[text/xml; charset=UTF-8], Host=[localhost:8008], Pragma=[no-cache], SOAPAction=[""], User-Agent=[Apache CXF 3.0.1]}
Payload: <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Body><ns2:sayHello xmlns:ns2="http://service.hxm.com/"><arg0>LinHao</arg0></ns2:sayHello></soap:Body></soap:Envelope>
--------------------------------------
2021-02-10 12:09:09,358 40783 [qtp652433136-20] INFO loServiceImplPort.HelloService - Outbound Message
---------------------------
ID: 1
Response-Code: 200
Encoding: UTF-8
Content-Type: text/xml
Headers: {}
Payload: <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Body><ns2:sayHelloResponse xmlns:ns2="http://service.hxm.com/"><return>LinHao ,welcome to hxm!</return></ns2:sayHelloResponse></soap:Body></soap:Envelope>
soap协议
soap:Envelope soap根元素
soap:Body soap内容