我的第一个Webserveice开发

我的第一个Webserveice开发

简述

	最近公司需要做全国失业保险待遇的领取对接功能,需要用到webservice接口用来进行数据交互。但是由于自
	己以前没有使用过webservice,在布下任务后就加紧学习,下面是我学习的过程。

Webservice的介绍

  1. Webservice是一个平台独立的,低耦合的,自包含的、基于可编程的web的应用程序,可使用开放的XML(标准通用标记语言下的一个子集)标准来描述、发布、发现、协调和配置这些应用程序,用于开发分布式的互操作的应用程序。WebService技术, 能使得运行在不同机器上的不同应用无须借助附加的、专门的第三方软件或硬件, 就可相互交换数据或集成。依据Web Service规范实施的应用之间, 无论它们所使用的语言、 平台或内部协议是什么,都可以相互交换数据。Web Service是自描述、 自包含的可用网络模块, 可以执行具体的业务功能。WebService也很容易部署, 因为它们基于一些常规的产业标准以及已有的一些技术,诸如标准通用标记语言下的子集XML、HTTP。Web Service减少了应用接口的花费。Web Service为整个企业甚至多个组织之间的业务 流程的集成提供了一个通用机制。关webservice的描述百度一堆,简单来说他就是一种可以跨平台、跨语言进行远程调用的技术。在使用时只需遵守双方的约定,传入正确的参数即可。

Webservice的开发过程

  1. **1、**在本次开发的webservice是通过CXF 开发的webservice接口。在开发前需要在web.xml中配置访问路径。配置如下:
	<!-- WebService -->
	<servlet>
		<servlet-name>CXFServlet</servlet-name>
		<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
		<load-on-startup>2</load-on-startup>
	</servlet>
	<servlet-mapping>
		<servlet-name>CXFServlet</servlet-name>
		<url-pattern>/services/*</url-pattern>
	</servlet-mapping>

**/services/***是他的访问路径,例如:http://localhost:8080/sxsye/services/testWebservicet?wsdl

**2、**配置好xml后就需要写访问类与访问方法了,代码如下:

import javax.jws.WebParam;
import javax.jws.WebService;

@WebService(targetNamespace = "qgsydyWbservice")
public interface qgsydyslWebservice {
    String receiveSybxjData(@WebParam(name = "data") String data);//失业保险金
    String receiveSybzjData(@WebParam(name = "data") String data);//失业补助金
}
  1. 在创建实现类时需要注意:
    a:在实现类上加入@WebService注解,该注解的作用是标识该实现类是webservice的服务类,发布该实现类中的public方法 。targetNamespace 是命名空间,@WebParam是发布的参数名称。
    b:在写完类与方法后就需要进行发布。
  	<?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:jaxws="http://cxf.apache.org/jaxws"
	xmlns:http-conf="http://cxf.apache.org/transports/http/configuration"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
						http://www.springframework.org/schema/beans/spring-beans.xsd
						http://cxf.apache.org/jaxws
						http://cxf.apache.org/schemas/jaxws.xsd
						http://cxf.apache.org/transports/http/configuration 
             			http://cxf.apache.org/schemas/configuration/http-conf.xsd">
	<!-- 3.0之後可以不再引入 -->
	<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" />
	
	<!-- Start 安全配置 -->
	<bean id="wss4jInInterceptor" class="org.apache.cxf.ws.security.wss4j.WSS4JInInterceptor">
		<constructor-arg>
			<map>
				<entry key="action" value="UsernameToken" />
				<entry key="passwordType" value="PasswordText" />
				<entry key="passwordDigest" value="PasswordText" />
				<entry key="passwordCallbackClass"
					value="com.lbs.brick.ws.ServerPasswordCallbackHandler" />
					 
			</map>
		</constructor-arg>
	</bean>
	<!-- End Server 配置要对外暴露的接口及访问地址 -->
	<!--测试使用http://localhost:8080/sxsye/services/testWebservicet?wsdl-->
	<bean id="testWebservice" class="com.lbs.webservice.testWebservice"></bean>
	<jaxws:server id="testWebservicet" address="/testWebservicet">
		<jaxws:serviceBean>
			<ref bean="testWebservice" />
		</jaxws:serviceBean>
	</jaxws:server>
</beans>

c、发布后可以直接访问发布的地址,http://localhost:8080/sxsye/services/testWebservicet?wsdl来测试是否发布成功。如果成功了,访问网址会出现如下界面:

<wsdl:definitions xmlns:ns1="http://cxf.apache.org/bindings/xformat" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="test" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" name="testWebserviceService" targetNamespace="test">
<wsdl:types>
<xs:schema xmlns:tns="test" xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="unqualified" targetNamespace="test" version="1.0">
<xs:element name="testPost" type="tns:testPost"/>
<xs:element name="testPostResponse" type="tns:testPostResponse"/>
<xs:complexType name="testPost">(方法名称)
<xs:sequence>
<xs:element minOccurs="0" name="data" type="xs:string"/>(参数名称与类型)
</xs:sequence>
</xs:complexType>
<xs:complexType name="testPostResponse">
<xs:sequence>
<xs:element minOccurs="0" name="return" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:schema>
</wsdl:types>
<wsdl:message name="testPostResponse">
<wsdl:part element="tns:testPostResponse" name="parameters"> </wsdl:part>
</wsdl:message>
<wsdl:message name="testPost">
<wsdl:part element="tns:testPost" name="parameters"> </wsdl:part>
</wsdl:message>
<wsdl:portType name="testWebservice">
<wsdl:operation name="testPost">
<wsdl:input message="tns:testPost" name="testPost"> </wsdl:input>
<wsdl:output message="tns:testPostResponse" name="testPostResponse"> </wsdl:output>
</wsdl:operation>
</wsdl:portType>
<wsdl:binding name="testWebserviceServiceSoapBinding" type="tns:testWebservice">
<soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
<wsdl:operation name="testPost">
<soap:operation soapAction="" style="document"/>
<wsdl:input name="testPost">
<soap:body use="literal"/>
</wsdl:input>
<wsdl:output name="testPostResponse">
<soap:body use="literal"/>
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
<wsdl:service name="testWebserviceService">
<wsdl:port binding="tns:testWebserviceServiceSoapBinding" name="testWebservicePort">
<soap:address location="http://localhost:8080/sxsye/services/testWebservicet"/>
</wsdl:port>
</wsdl:service>
</wsdl:definitions>	

d、发布成功后,我们可以用postman进行测试,用来进行请求,看接口是否可以访问。
在这里插入图片描述
上面就是postman得界面,我们再请求时需要设置一些参数。
1、请求地址:http://localhost:8080/sxsye/services/testWebservicet?wsdl
3、请求头:请求头就是在发送请求时设置得一些东西,列如设置他的编码格式:
text/xml;charset=utf-8。
4、请求体:请求体就是你所要在访问时需要传入得参数,如下:

<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:com="test">
<soapenv:Header/>
<soapenv:Body>
<com:testPost>
<!--Optional:-->
<data>
<![CDATA[{'name':'信息','age':'18'}]]>


</data>
</com:testPost>
</soapenv:Body>
</soapenv:Envelope>

这里主要说一下(xmlns:com=“test”),test是webservixe得命名空间,namespace。
5、点击发送就会请求并带回返回得参数。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值