webservice
常用的数据格式:
json({}[]) xml(特点标签<></>)
采用的传输协议:
soap 它是用于交换(传输)XML(数据 格式/结构)编码信息的轻量级协议
WSDL:
Web Service描述语言,用于描述Web Service及其函数、参数和返回值
A 调用 B
客户端 服务端
常见的webservice 技术
cxf(xfire) 支持语言少
axis2(axis) 支持语言多
使用 cxf 暴露一个接口
1.首先到jar包
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-ws-security</artifactId>
<version>2.7.8</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-api</artifactId>
<version>2.7.8</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxws</artifactId>
<version>2.7.8</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-bindings-soap</artifactId>
<version>2.7.8</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http</artifactId>
<version>2.7.8</version>
</dependency>
2.在resource目录下创建一个spring-cxf.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:jaxws="http://cxf.apache.org/jaxws"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://cxf.apache.org/jaxws
http://cxf.apache.org/schemas/jaxws.xsd">
<!-- 导入cxf配置文件 -->
<import resource="classpath:META-INF/cxf/cxf.xml" />
<import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" />
<import resource="classpath:META-INF/cxf/cxf-servlet.xml" />
<!-- 发布接口 -->
<jaxws:endpoint
id="cxfUserServiceWeb"
implementor="com.jk.cqd.service.impl.CxfUserServiceImpl"
address="/cxfUserService">
</jaxws:endpoint>
3.在接口的实现类上加上 @WebService注解
4:在web.xml中加上如下配置
<servlet>
<servlet-name>CXFServlet</servlet-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>CXFServlet</servlet-name>
<url-pattern>/webservice/*</url-pattern>
</servlet-mapping>
5:在浏览器访问
ip地址:端口号/项目名/servlet url-pattern/spring-cxf中的address?wsdl
客户端如何调用webservice的服务端接口
1:自动生成客户端代码
命令
-p 包结构
-client 值得是服务端的接口地址
wsdl2java -p com.jk.wdd.service.webservice -client http://localhost:8081/login/webservice/cxfUserService?wsdl
2:在resource目录下创建一个spring-cxf.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:jaxws="http://cxf.apache.org/jaxws"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://cxf.apache.org/jaxws
http://cxf.apache.org/schemas/jaxws.xsd">
<!-- 导入cxf配置文件 -->
<import resource="classpath:META-INF/cxf/cxf.xml" />
<import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" />
<import resource="classpath:META-INF/cxf/cxf-servlet.xml" />
<!-- 这个class的包路径和类名和服务端提供web服务的接口一致-->
<bean id="client" class="com.jk.cqd.service.webservice.CxfUserServiceImpl" factory-bean="clientfactory" factory-method="create"/>
<bean id="clientfactory" class="org.apache.cxf.jaxws.JaxWsProxyFactoryBean">
<property name="serviceClass" value="com.jk.cqd.service.webservice.CxfUserServiceImpl"/>
<!-- 这个address一定要注意,正确的 沒有?wsdl-->
<property name="address" value="http://192.168.126.1:8081/webservice/cxfUserService"/>
</bean>
<bean id="clientFy" class="com.jk.cqd.service.fanyiwebservice.TranslatorWebServiceSoap" factory-bean="clientfactoryFy" factory-method="create"/>
<bean id="clientfactoryFy" class="org.apache.cxf.jaxws.JaxWsProxyFactoryBean">
<property name="serviceClass" value="com.jk.cqd.service.fanyiwebservice.TranslatorWebServiceSoap"/>
<!-- 这个address一定要注意,正确的 沒有?wsdl -->
<property name="address" value="http://www.webxml.com.cn/WebServices/TranslatorWebService.asmx"/>
</bean>
</beans>
---------------------
作者:四方度爷
来源:优快云
原文:https://blog.youkuaiyun.com/caiqiandu/article/details/88613938
版权声明:本文为博主原创文章,转载请附上博文链接!