「java解析wsdl」java解析xml的几种方式
今天给各位分享java解析wsdl的知识,其中也会对java解析xml的几种方式进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!
本文目录一览:
- 1、Java调用wsdl,怎么实现
- 2、java web项目怎么使用wsdl文件
- 3、怎么用cxf的wsdl2java解析wcf生成的wsdl
- 4、eclipse解析wsdl文件 怎么获取wsdl信息
- 5、myeclipse10 怎么用xfire解析wsdl
Java调用wsdl,怎么实现
java调用wsdl的步骤如下,主要是使用第三方框架:
步骤如下:
1.下载AXIS2类库,AXIS2是目前java调用webservice的一个主要方法(由于更新较频繁,请自行google该类库的网址)
2.由于是第三方webservice,直接引入AXIS2的包就可以用了,代码如下:
import java.rmi.RemoteException;
import javax.xml.rpc.ParameterMode;
import javax.xml.rpc.ServiceException;
import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import org.apache.axis.encoding.XMLType;
public class webServiceTest {
public String invokeRemoteFuc() {
String endpoint = "";
String result = "no result!";
Service service = new Service();
Call call;
Object[] object = new Object[1];
object[0] = "Dear I miss you";//Object是用来存储方法的参数
try {
call = (Call) service.createCall();
call.setTargetEndpointAddress(endpoint);// 远程调用路径
call.setOperationName("say");// 调用的方法名
// 设置参数名:
call.addParameter("str1", // 参数名
XMLType.XSD_STRING,// 参数类型:String
ParameterMode.IN);// 参数模式:'IN' or 'OUT'
// 设置返回值类型:
call.setReturnType(XMLType.XSD_STRING);// 返回值类型:String
result = (String) call.invoke(object);// 远程调用
} catch (ServiceException e) {
e.printStackTrace();
} catch (RemoteException e) {
e.printStackTrace();
}
return result;
}
public static void main(String[] args) {
webServiceTest t = new webServiceTest();
String result = t.invokeRemoteFuc();
System.out.println(result);
}
}
该方法的原理很简单,通过AXIS2封装好的类设置URL和参数,直接调用就好了,我们要关注的就是设置URL,方法,还有方法的参数,其他的copypaste好啦,很简单吧,再看看其他的方法,我勒个去了,害我瞎搞两天。迟点上个源码共大家参考!
java web项目怎么使用wsdl文件
webservice的发布一般都是使用WSDL(web service descriptive language)文件的样式来发布的,在WSDL文件里面,包含这个webservice暴露在外面可供使用的接口。
注意,以下的代码并没有经过真正的测试,只是说明这些情况,不同版本的Axis相差很大,最好以apache网站上的例子为准,这里仅仅用于说明其基本用法。
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 = "";
//直接引用远程的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("");
} 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);
try {
// 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();
}
}
}
怎么用cxf的wsdl2java解析wcf生成的wsdl
1、下载apache-cxf-2.6.2在环境变量中配置CXF_HOME ,在PATH中加入%CXF_HOME%\bin 2、输入cmd 进入控制窗口,输入wsdl2java看是否配置成功
3、参考它的文档看这个工具的具体参数的用法
wsdl2java用法:
wsdl2java -p com -d src -all aa.wsdl
-p 指定其wsdl的命名空间,也就是要生成代码的包名:
-d 指定要产生代码所在目录
-client 生成客户端测试web service的代码
-server 生成服务器启动web service的代码
-impl 生成web service的实现代码
-ant 生成build.xml文件
-all 生成所有开始端点代码:types,service proxy,,service interface, server mainline, client mainline, implementation object, and an Ant build.xml file.
详细用法见:
eclipse解析wsdl文件 怎么获取wsdl信息
一个WSDL文档通常包含7个重要的元素,即types、import、message、portType、operation、binding、 service元素。
这些元素嵌套在definitions元素中,
(1) Definitions是WSDL文档的根元素。对应于这个类: org.eclipse.wst.wsdl.Definition 其他的对象都可以通过这个对象获得
(2) Types - 数据类型定义的容器,它使用某种类型系统(一般地使用XML Schema中的类型系统)。
(3) Message - 通信消息的数据结构的抽象类型化定义。使用Types所定义的类型来定义整个消息的数据结构。
(4) PortType - 对于某个访问入口点类型所支持的操作的抽象集合,这些操作可以由一个或多个服务访问点来支持。
(子节点) Operation - 对服务中所支持的操作的抽象描述,一般单个Operation描述了一个访问入口的请求/响应消息对。
(5) Binding - 特定端口类型的具体协议和数据格式规范的绑定。
(6) Service- 相关服务访问点的集合。
(子节点) Port - 定义为协议/数据格式绑定与具体Web访问地址组合的单个服务访问点。
下面是代码实例:
import java.io.File; import java.io.IOException; import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Set; import javax.wsdl.Message; import javax.wsdl.Part; import javax.wsdl.PortType; import javax.xml.namespace.QName; import org.eclipse.emf.common.util.URI; import org.eclipse.emf.ecore.resource.Resource; import org.eclipse.emf.ecore.resource.ResourceSet; import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl; import org.eclipse.wst.wsdl.Definition; import org.eclipse.wst.wsdl.Types; import org.eclipse.wst.wsdl.internal.impl.PartImpl; import org.eclipse.wst.wsdl.internal.util.WSDLResourceFactoryImpl; import org.eclipse.wst.wsdl.util.WSDLResourceImpl; import org.eclipse.xsd.XSDElementDeclaration; import org.eclipse.xsd.XSDSchema; import org.eclipse.xsd.util.XSDResourceImpl; import org.junit.Test; import org.junit.Before; public class WSDLParserWithEclipse { Definition definition=null; String wsdlPathString="E:/HelloEclipse-EMF-WSDL-XSD/test.wsdl"; @Before public void setup(){ ResourceSet resourceSet = new ResourceSetImpl(); Resource.Factory.Registry registry = resourceSet.getResourceFactoryRegistry(); Map extensionToFactoryMap = registry.getExtensionToFactoryMap(); extensionToFactoryMap.put("wsdl", new WSDLResourceFactoryImpl()); File wsdlFile =new File(wsdlPathString); URI uri = URI.createFileURI(wsdlFile.getAbsolutePath()); // You can avoid this cast, but will have to cast anyway later to get the Definition out the resource contents WSDLResourceImpl wsdlResource = (WSDLResourceImpl) resourceSet.createResource(uri); try { wsdlResource.load(null); definition = wsdlResource.getDefinition(); }catch(Exception e){ e.printStackTrace(); } } @Test public void testTypes(){ Types types = definition.getETypes(); List schemas = types.getSchemas(""); XSDSchema schema = (XSDSchema) schemas.get(0); org.eclipse.xsd.util.XSDResourceImpl.serialize(System.out, schema.getElement()); } @Test public void testMessage(){ Map messages=definition.getMessages(); System.out.println("The message size is:"+messages.size()); Set setMessages=messages.keySet(); Iterator iteratorMessages=setMessages.iterator(); while(iteratorMessages.hasNext()){ QName key=(QName)iteratorMessages.next(); Message message=(Message)messages.get(key); //{}getKeysSoapIn //System.out.println("Message Name:"+message.getQName()); if(message.getQName().toString().indexOf("getKeysSoapIn")0){ System.out.println("Message Name:"+message.getQName()); Map partsMap=message.getParts(); //org.eclipse.xsd.impl.XSDElementDeclarationImpl System.out.println("Message Part size for getKeysSoapIn message is:"+partsMap.size()); PartImpl part= (PartImpl)partsMap.get("problem"); XSDElementDeclaration xsdElementDeclaration=part.getElementDeclaration(); XSDResourceImpl.serialize(System.out, xsdElementDeclaration.getElement()); } } } @Test public void testPortType(){ Map portTypes=definition.getPortTypes(); System.out.println("Port Type size:"+portTypes.size()); if(portTypes!=nullportTypes.size()0){ Set set=portTypes.keySet(); Iterator iterator=set.iterator(); while(iterator.hasNext()){ QName object=(QName)iterator.next(); PortType portType=(PortType)portTypes.get(object); System.out.println("Port Type name:"+portType.getQName()); org.eclipse.xsd.util.XSDResourceImpl.serialize(System.out, portType.getDocumentationElement()); } } } }
myeclipse10 怎么用xfire解析wsdl
用myeclipse将wsdl文件生成java代码:
1、选择新建的工程后,点击右键,选择“Run As”-“Run”,如下图所示:
2、弹出如下窗口:
3、 在上图中,左边选择“Java Application”后,点击左上角的新增“”按钮new configuration,页面如下图所示:
4、在上图中,将“Include libraries when searching for a main class” 勾上,而后点击“Main class”右边的“Search”按钮,弹出窗口如下图所示:
5、在上图中输入WSDL2Java后,查找到axis这个jar包中对应类,点击“OK”按钮,此时主窗口如下图所示:
6、在上图中点击“Arguments”页签,Arguments里把要生成的web service URI或wsdl文件写进去,最后点run。在下图中输入的是wsdl文件的路径: src/wsdl/acd_accessCode_interface.wsdl、src/wsdl/acd_accessCode_service.wsdl:
7、生成的java代码如下:
java解析wsdl的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于java解析xml的几种方式、java解析wsdl的信息别忘了在本站进行查找喔。
发布于:2022-11-27,除非注明,否则均为
原创文章,转载请注明出处。