javawebservice接口开发(webservice接口开发wsdl)

http://www.itjxue.com  2023-03-16 20:54  来源:未知  点击次数: 

java如何调用webservice接口

Java调用WebService可以直接使用Apache提供的axis.jar自己编写代码,或者利用Eclipse自动生成WebService Client代码,利用其中的Proxy类进行调用。理论上是一样的,只不过用Eclipse自动生成代码省事些。

1、编写代码方式:

package com.yudun.test;

import java.rmi.RemoteException;

import org.apache.axis.client.Call;

import org.apache.axis.client.Service;

import org.apache.axis.message.PrefixedQName;

import org.apache.axis.message.SOAPHeaderElement;

import com.cezanne.golden.user.Exception;

import com.cezanne.golden.user.UserManagerServiceProxy;

import javax.xml.namespace.QName;

import java.net.MalformedURLException;

import javax.xml.rpc.ServiceException;

import javax.xml.soap.Name;

import javax.xml.soap.SOAPException;

public class testWebService {

public static String getResult() throws ServiceException, MalformedURLException, RemoteException, SOAPException

{

//标识Web Service的具体路径

String endpoint = "WebService服务地址";

// 创建 Service实例

Service service = new Service();

// 通过Service实例创建Call的实例

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

//将Web Service的服务路径加入到call实例之中.

call.setTargetEndpointAddress( new java.net.URL(endpoint) );//为Call设置服务的位置

// 由于需要认证,故需要设置调用的SOAP头信息。

Name headerName = new PrefixedQName( new QName("发布的wsdl里的targetNamespace里的url", "string_itemName") );

org.apache.axis.message.SOAPHeaderElement header = new SOAPHeaderElement(headerName);

header.addTextNode( "blablabla" );

call.addHeader(header);

// SOAPHeaderElement soapHeaderElement = new SOAPHeaderElement("发布的wsdl里的targetNamespace里的url", "SoapHeader");

// soapHeaderElement.setNamespaceURI("发布的wsdl里的targetNamespace里的url");

// try

// {

// soapHeaderElement.addChildElement("string_itemName").setValue("blablabla");

// }

// catch (SOAPException e)

// {

// e.printStackTrace();

// }

// call.addHeader(soapHeaderElement);

//调用Web Service的方法

org.apache.axis.description.OperationDesc oper;

org.apache.axis.description.ParameterDesc param;

oper = new org.apache.axis.description.OperationDesc();

oper.setName("opName");

param = new org.apache.axis.description.ParameterDesc(new javax.xml.namespace.QName("", "arg0"), org.apache.axis.description.ParameterDesc.IN, new javax.xml.namespace.QName("", "string"), java.lang.String.class, false, false);

param.setOmittable(true);

oper.addParameter(param);

param = new org.apache.axis.description.ParameterDesc(new javax.xml.namespace.QName("", "arg1"), org.apache.axis.description.ParameterDesc.IN, new javax.xml.namespace.QName("", "string"), java.lang.String.class, false, false);

param.setOmittable(true);

oper.addParameter(param);

param = new org.apache.axis.description.ParameterDesc(new javax.xml.namespace.QName("", "arg2"), org.apache.axis.description.ParameterDesc.IN, new javax.xml.namespace.QName("", "string"), java.lang.String.class, false, false);

param.setOmittable(true);

oper.addParameter(param);

oper.setReturnType(new javax.xml.namespace.QName("", "string"));

oper.setReturnClass(java.lang.String.class);

oper.setReturnQName(new javax.xml.namespace.QName("", "return"));

oper.setStyle(org.apache.axis.constants.Style.WRAPPED);

oper.setUse(org.apache.axis.constants.Use.LITERAL);

oper.addFault(new org.apache.axis.description.FaultDesc(

new javax.xml.namespace.QName("发布的wsdl里的targetNamespace里的url", "Exception"),

"Exception",

new javax.xml.namespace.QName("发布的wsdl里的targetNamespace里的url", "Exception"),

true

));

call.setOperation( oper );

call.setOperationName(new javax.xml.namespace.QName("发布的wsdl里的targetNamespace里的url", "opName"));

//调用Web Service,传入参数

String res = ( String ) call.invoke( new Object[]("arg0","arg1"));

System.out.println("===============");

return res;

}

/**

* @param args

*/

public static void main(String[] args) {

try {

System.out.println(getResult());

} catch (MalformedURLException e) {

e.printStackTrace();

} catch (RemoteException e) {

e.printStackTrace();

} catch (ServiceException e) {

e.printStackTrace();

} catch (SOAPException e) {

e.printStackTrace();

}

}

}

2、利用Eclipse自动生成WebService client代码就容易多了:(由于还不会发图片,就用语言描述了,大家酬和看吧。。。)

首先,new project,选择other,在输入框中输入Web Service Client,选中搜索后的结果,点击Next,在Service definition中输入 WebService的发布地址,点击Finish

这样,WebService Client代码已经生成好了。

接下来写一个Test类,在main函数中输入如下代码:

String endpoint = "服务器的WebService的地址";

YourWebServiceNameProxy umsp = new YourWebServiceNameProxy (endpoint);

try {

String resultStr = umsp.opMethod("arg0","arg1");

System.out.println(resultStr);

} catch (Exception e) {

System.out.println("异常");

e.printStackTrace();

} catch (RemoteException e) {

System.out.println("RemoteException异常");

e.printStackTrace();

}

如果还有疑问的话还有视频,如果对你有帮助请采纳!

java Webservice都有哪几种方式啊,哪种方式比较好啊

webservice的应用已经越来越广泛了,下面介绍几种在Java体系中开发webservice的方式,相当于做个记录。

1.Axis2

Axis是apache下一个开源的webservice开发组件,出现的算是比较早了,也比较成熟。这里主要介绍Axis+eclipse开发webservice,当然不用eclipse也可以开发和发布webservice,只是用eclipse会比较方便。

(1)下载eclipse的Java EE版本

(2)下载axis2

(3)下载eclipse的axis2插件

Axis2_Codegen_Wizard

Axis2_Service_Archiver

推荐使用1.3的版本

(4)eclipse安装axis2插件

1)在任意目录下新建一个Axis2文件夹,在该文件夹下新建eclipse目录,在eclipse目录中新建plugins目录和features目录,例如:D:\programSoftware\eclipse-SVN\Axis2\eclipse;

2)把下载的axis2插件解压,并把解压的文件放到新建的eclipse的plugins目录下;

3)在%eclipse_home%的目录下新建links目录,并在links目录下新建axis2.link文件,内容为:path=D:\programSoftware\eclipse-SVN\Axis2;

4)重启eclipse,点击·file-new-other,如果看到Axis2 Wizards,则表明插件安装成功。

(5)安装axis2

(6)使用eclipse新建web工程,创建一个普通java类,至少包含一个方法。

(7)发布webservice

1)点击eclipse的File-New-other,打开Axis2 Wizards,选择Axis2 Service Archiver,然后Next;

2)选择Class File Location,也就是类文件存放路径,注意:只选到classes目录,不要包括包文件夹,然后Next;

3)选择Skip WSDL,然后Next

4)一路Next到Select the Service XML file to be included in the Service archive,勾选Generate theservice xml automatically;

5)Service Name-填写你的service名称,Class Name-填写类名称,要包括包名,然后点击load,然后点击Finish,这时webservice就发布成功了;

6)然后到%TOMCAT_HOME%/webapps/axis2/WEB-INF/services 看看是否多了一个.aar的文件;

注意:以上的方式是发布到axis2.war包中,你也可以把生成.aar文件copy到你的实际应用中,同时,你也可以使用eclipse的create webservice功能发布你的webservice,选择axis2生成你的webservice,这样webservice就会部署到你的应用中了。

2.Apche CXF

CXF开发webservice也是比较方便和简单的,它和spring的集成可以说是非常地好。举一个CXF开发webservice的例子吧。

1)在eclipse中新建一个web工程,导入依赖包,如图:

2)编写一个接口,如:

注意:CXF开发的webservice,接口中的方法的参数一定要以这种方式,否则客户端调用的时候CXF服务端会接收不到参数的值,name:参数名称,可不写(建议写上),targetNamespace:命名空间,一定要填写上,默认是包名反过来的顺序,mode:参数类型,IN表示输入。

3)编写一个实现类,实现接口的方法;

4)和spring的集成,编写一个bean文件,如:cxf-beans.xml,内容如下:

Cxf-beans.xml代码

?xml version="1.0" encoding="UTF-8" ?

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="vote" implementor="com.zcl.cxf.service.VoteImpl" address="/Vote" /

/beans

这个文件比较容易理解,就不解释了。

5)配置CXFServlet

在web.xml文件中配置CXFServlet,加载cxf-beans.xml文件,内容如下:

Web.xml代码

id="WebApp_ID" version="2.5"

context-param

param-namecontextConfigLocation/param-name

param-valueWEB-INF/cxf-beans.xml/param-value

/context-param

listener

listener-classorg.springframework.web.context.ContextLoaderListener/listener-class

/listener

servlet

servlet-namecxf/servlet-name

servlet-classorg.apache.cxf.transport.servlet.CXFServlet/servlet-class

load-on-startup1/load-on-startup

/servlet

servlet-mapping

servlet-namecxf/servlet-name

url-pattern/services/*/url-pattern

/servlet-mapping

/web-app

把工程部署到中间件,如tomcat,就可以访问该webservice了。

3.JDK开发webservice

1)编写一个Java类,如下:

Jdkwebservice.java代码

package demo;

import javax.jws.WebParam;

import javax.jws.WebService;

import javax.xml.ws.Endpoint;

@WebService

public class JdkWebService {

return "Just do it," + value + "!";

}

public static void main(String[] args) {

}

}

2)运行该java类,在浏览器上就可以访问该webservice了。

注意:开发web工程的时候,这种方法不太友好。我们可以编写一个servlet类,在servlet类的初始化方法中发布webservice,这样我们的中间件服务器启动的时候就会帮我们自动webservice了。

4) xfire

开发WebService的框架不少,每个框架都有各自的有点,最近我用xfire练习开发WebService,下面是开发WebService的小例子,希望对入门的人有些小帮助

1.新建一个java web project命名为TestWebService,将xfire相关的jar包添加到lib目录中,写接口类和实现类

Java代码

package com.lamp.service;

public interface MessageService {

public String getName(String name);

}

[java] view plaincopyprint?

package com.lamp.service;

public interface MessageService {

public String getName(String name);

}

实现类

Java代码

package com.lamp.service.impl;

import com.lamp.service.MessageService;

public class MessageServiceImpl implements MessageService {

public String getName(String name) {

return "hellow " + name + ", welcome to WebService world";

}

}

[java] view plaincopyprint?

package com.lamp.service.impl;

import com.lamp.service.MessageService;

public class MessageServiceImpl implements MessageService {

public String getName(String name) {

return "hellow " + name + ", welcome to WebService world";

}

}

在src目录下新建文件夹META-INF,然后再在其下新建文件夹xfire,在xfire目录下新建配置文件services.xml

Xml代码

?xml version="1.0" encoding="UTF-8"?

service

nameMessageService/name

serviceClasscom.lamp.service.MessageService/serviceClass

implementationClasscom.lamp.service.impl.MessageServiceImpl/implementationClass

/service

/beans

[xml] view plaincopyprint?

?xml version="1.0" encoding="UTF-8"?

service

nameMessageService/name

serviceClasscom.lamp.service.MessageService/serviceClass

implementationClasscom.lamp.service.impl.MessageServiceImpl/implementationClass

/service

/beans

最后在web.xml中配置xfire的servlet

Xml代码

servlet

servlet-nameXFireServlet/servlet-name

servlet-class

/servlet-class

/servlet

servlet-mapping

servlet-nameXFireServlet/servlet-name

url-pattern/servlet/XFireServlet/*/url-pattern

/servlet-mapping

servlet-mapping

servlet-nameXFireServlet/servlet-name

url-pattern/services/*/url-pattern

/servlet-mapping

[xml] view plaincopyprint?

servlet

servlet-nameXFireServlet/servlet-name

servlet-class

/servlet-class

/servlet

servlet-mapping

servlet-nameXFireServlet/servlet-name

url-pattern/servlet/XFireServlet/*/url-pattern

/servlet-mapping

servlet-mapping

servlet-nameXFireServlet/servlet-name

url-pattern/services/*/url-pattern

/servlet-mapping

这样服务器端开发完毕,现在开始客户端的开发

新建一个java project也将xfire相关的jar引入,我用ant在客户端生成代理对象,在项目路径下新建build.xml,代码为

Xml代码

?xml version="1.0" encoding="UTF-8"?

project name="WebService" basedir="." default="gen-webservice"

property file="build.properties"

/property

path id="project-classpath"

fileset dir="${lib.dir}"

include name="**/*.jar" /

/fileset

/path

target name="gen-webservice"

taskdef name="wsgen" classname="org.codehaus.xfire.gen.WsGenTask" classpathref="project-classpath" /

wsgen outputDirectory="${src.dir}"

wsdl="${wsdl.dir}" package="com.lamp.ws.client" overwrite="true"/

/target

/project

[xml] view plaincopyprint?

?xml version="1.0" encoding="UTF-8"?

project name="WebService" basedir="." default="gen-webservice"

property file="build.properties"

/property

path id="project-classpath"

fileset dir="${lib.dir}"

include name="**/*.jar" /

/fileset

/path

target name="gen-webservice"

taskdef name="wsgen" classname="org.codehaus.xfire.gen.WsGenTask" classpathref="project-classpath" /

wsgen outputDirectory="${src.dir}"

wsdl="${wsdl.dir}" package="com.lamp.ws.client" overwrite="true"/

/target

/project

java语言 编写接口开发需要用到WebService么?

WebService是第三方接口,就是可以远程调用服务接口。如果是本机上,直接调用就行了,不需要用WebService技术!

java如何调用webservice接口?

Java通过WSDL文件来调用webservice直接调用模式如下:

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());

}

}

}

java开发webservice接口有几种方式

Support for Standards

JSR Support

??? JAX-WS - Java API for XML-Based Web Services (JAX-WS) 2.0 - JSR-224

??? Web Services Metadata for the Java Platform - JSR-181

??? JAX-RS - The Java API for RESTful Web Services - JSR-311

??? SAAJ - SOAP with Attachments API for Java (SAAJ) - JSR-67

WS-* and related Specifications Support

??? Basic support: WS-I Basic Profile 1.1

??? Quality of Service: WS-Reliable Messaging

??? Metadata: WS-Policy, WSDL 1.1 - Web Service Definition Language

??? Communication Security: WS-Security, WS-SecurityPolicy, WS-SecureConversation, WS-Trust (partial support)

??? Messaging Support: WS-Addressing, SOAP 1.1, SOAP 1.2, Message Transmission Optimization Mechanism (MTOM)

Multiple Transports, Protocol Bindings, Data Bindings, and Formats

??? Transports: HTTP, Servlet, JMS, In-VM and many others via the Camel transport for CXF such as SMTP/POP3, TCP and Jabber

??? Protocol Bindings: SOAP, REST/HTTP, pure XML

??? Data bindings: JAXB 2.x, Aegis, Apache XMLBeans, Service Data Objects (SDO), JiBX

??? Formats: XML Textual, JSON, FastInfoset

??? Extensibility API allows additional bindings for CXF, enabling additional message format support such as CORBA/IIOP

(责任编辑:IT教学网)

更多