加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

webservice的调用

发布时间:2020-12-17 00:27:40 所属栏目:安全 来源:网络整理
导读:1,直接AXIS调用远程的web service import java.util.Date; import java.text.DateFormat; import org.apache.axis.client.Call; import org.apache.axis.client.Service; import javax.xml.namespace.QName; import java.lang.Integer; import javax.xml.rpc

1,直接AXIS调用远程的web service


import java.util.Date;

import java.text.DateFormat;

import org.apache.axis.client.Call;

import org.apache.axis.client.Service;

import javax.xml.namespace.QName;

import java.lang.Integer;

import javax.xml.rpc.ParameterMode;

?

public class caClient {

????????????

?????? public static void main(String[] args) {

????????????? try {

???????????????????? String endpoint = "http://localhost:8080/ca3/services/caSynrochnized?wsdl";

???????????????????? //直接引用远程的wsdl文件

??????????????????? //以下都是套路?
???????????????????? Service service = new Service();

???????????????????? Call call = (Call) service.createCall();

???????????????????? call.setTargetEndpointAddress(endpoint);

???????????????????? call.setOperationName("addUser");//WSDL里面描述的接口名称

???????????????????? call.addParameter("userName",org.apache.axis.encoding.XMLType.XSD_DATE,

?????????????????????????????????? javax.xml.rpc.ParameterMode.IN);//接口的参数

???????????????????? call.setReturnType(org.apache.axis.encoding.XMLType.XSD_STRING);//设置返回类型?


???????????????????? String temp = "测试人员";?
???????????????????? String result = (String)call.invoke(new Object[]{temp});

???????????????????? //给方法传递参数,并且调用方法

???????????????????? System.out.println("result is "+result);

????????????? }

????????????? catch (Exception e) {

???????????????????? System.err.println(e.toString());

?????? }

}

2,直接SOAP调用远程的webservice

这种模式我从来没有见过,也没有试过,但是网络上有人贴出来,我也转过来

import org.apache.soap.util.xml.*;

import org.apache.soap.*;

import org.apache.soap.rpc.*;

import java.io.*;

import java.net.*;

import java.util.Vector;

public class caService{

?????? public static String getService(String user) {

?????? URL url = null;

?????? try {

?????????? url=new URL("http://192.168.0.100:8080/ca3/services/caSynrochnized");

?????? } catch (MalformedURLException mue) {

????????? return mue.getMessage();

???????? }

???????????? // This is the main SOAP object

?????? Call soapCall = new Call();

?????? // Use SOAP encoding

?????? soapCall.setEncodingStyleURI(Constants.NS_URI_SOAP_ENC);

?????? // This is the remote object we're asking for the price

?????? soapCall.setTargetObjectURI("urn:xmethods-caSynrochnized");

?????? // This is the name of the method on the above object

?????? soapCall.setMethodName("getUser");

?????? // We need to send the ISBN number as an input parameter to the method

?????? Vector soapParams = new Vector();

?????? // name,type,value,encoding style

?????? Parameter isbnParam = new Parameter("userName",String.class,user,null);

?????? soapParams.addElement(isbnParam);

?????? soapCall.setParams(soapParams);

????????? // Invoke the remote method on the object

????????? Response soapResponse = soapCall.invoke(url,"");

????????? // Check to see if there is an error,return "N/A"

????????? if (soapResponse.generatedFault()) {

????????????? Fault fault = soapResponse.getFault();

???????????? String f = fault.getFaultString();

???????????? return f;

????????? } else {

???????????? // read result

???????????? Parameter soapResult = soapResponse.getReturnValue ();

???????????? // get a string from the result

???????????? return soapResult.getValue().toString();

????????? }

?????? } catch (SOAPException se) {

????????? return se.getMessage();

??? }

3,使用wsdl2java把WSDL文件转成本地类,然后像本地类一样使用,即可。

==========================================

总结: 以上是前几年老的调用方式。都需要基于wsdl进行发布。进入spring-ws之后。我们的webservice开发流程是

1. 设计schema文件

<element name="updateRequest" type="tns:UpdateRequest"></element>
? ? ? ?<complexType name="UpdateRequest">
<sequence>
<element name="applications" type="tns:ApplicationType" maxOccurs="unbounded"
minOccurs="0">
</element>
</sequence>
<attribute name="address" type="string" use="required">
</attribute>
<attribute name="oeid" type="string" use="required"></attribute>
</complexType>


<complexType name="ApplicationType">
<sequence>
<element name="connected" type="tns:ConnectedType"
minOccurs="1" maxOccurs="1">
</element>
</sequence>
<attribute name="uniqueId" type="string" use="required"></attribute>
<attribute name="id" type="string" use="required"></attribute>
</complexType>

2. 服务器端使用一个servlet发布webservice:servlet在web环境成功加载后,根据servlet中DefaultWsdl11Definition配置的id,访问wsdl路径(比如:?http://localhost:8888/synchronize/synchronize.wsdl),即可看到wsdl --> webservice成功发布。

实现AbstractFaultCreatingValidatingMarshallingPayloadEndpoint

为endpoint类,配置context 文件

<bean id="webservice-***-endpoint" class="nl.enovation.lspconnect.console.interfaces.SynchronizeConfigurationEndpoint">
<property name="marshaller" ref="synchronize-configuration-marshaller" />
<property name="unmarshaller" ref="synchronize-configuration-marshaller" />
<property name="addValidationErrorDetail" value="true" />
</bean>

<bean id="synchronize-configuration-marshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
<!-- nl.enovation.lspconnect.jaxb.synchronize.configuration will be better -->
<property name="contextPath" value="jaxb包路径:cn.inovation.hotshare.synchronize.jaxb" />
<property name="schema">
<value>/WEB-INF/schemas/configuration/synchoronization.xsd</value>
</property>
</bean>

在Servlet文件中,定义bean;?org.springframework.ws.wsdl.wsdl11.DefaultWsdl11Definition,指定xsd文件,soapAction等webservice信息

<bean id="synchronize"
class="org.springframework.ws.wsdl.wsdl11.DefaultWsdl11Definition">
<property name="schema">
<bean class="org.springframework.xml.xsd.SimpleXsdSchema">
<property name="xsd"
value="/WEB-INF/schemas/configuration/***-configuration.xsd" />
</bean>
</property>
<property name="portTypeName" value="project-information" />
<property name="locationUri"
value="http://www.some-domain.org/synchronize/" />
<property name="targetNamespace"
value="http://www.enovation.nl/schemas/hotshare/synchronize" />
<property name="soapActions">
<value> ? ? ? ? ? ? ? ? ? ??
synchronize={http://www.enovation.nl/schemas/hotshare/synchronize}synchronize
</value>
</property>
</bean>

3. 客户端

配置:利用maven使客户端项目依赖xsd文件,使用jaxb2-maven-plugin,将xsd文件输出成jaxb object. 则可以直接使用本地对象完成webservice需要的创建过程。

实现:

====================================

WebServiceMessageCallback callback = new WebServiceMessageCallback() {


? ? ? ? ? ? public void doWithMessage(final WebServiceMessage message) throws IOException,TransformerException {
? ? ? ? ? ? ? ? ((SoapMessage) message).setSoapAction(connectSoapAction);
? ? ? ? ? ? ? ? Transformer newTransformer = TransformerFactory.newInstance().newTransformer();
? ? ? ? ? ? ? ? StringResult stringResult = new StringResult();
? ? ? ? ? ? ? ? newTransformer.transform(((SoapMessage) message).getSoapBody().getSource(),stringResult);
? ? ? ? ? ? ? ? log.debug("send update message: " + stringResult.toString());
? ? ? ? ? ? }


? ? ? ? };
? ? ? ? try {
? ? ? ? ? ? lspConnectWebServiceOperations.marshalSendAndReceive(connectUrl,message,callback);

=================================

注:connectUrl不是wsdl方式的url,是基于soap的方式(前文中第二中方式所提到的URL)

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读