导航:首页 > 编程语言 > javawebservice接口开发

javawebservice接口开发

发布时间:2022-06-29 04:38:03

A. java 如何实现webservice 怎么调用接口

一、利用jdkweb服务api实现,这里使用基于SOAPmessage的Web服务

①.首先建立一个WebservicesEndPoint:packageHello;
importjavax.jws.WebService;
importjavax.jws.WebMethod;
importjavax.xml.ws.Endpoint;
@WebService
publicclassHello{
@WebMethod
publicStringhello(Stringname){
return"Hello,"+name+" ";
}
publicstaticvoidmain(String[]args){
//createandpublishanendpoint
Hellohello=newHello();
Endpointendpoint=Endpoint.publish("


,hello);
}
}
②.使用apt编译Hello.java(例:apt-d[存放编译后的文件目录]Hello.java),
会生成jaws目录
③.使用javaHello.Hello运行,然后将浏览器指向


就会出现下列显示
④.使用wsimport生成客户端使用如下:
wsimport-p.-keep


这时,会在当前目录中生成如下文件:
⑤.客户端程序:
1classHelloClient{
2publicstaticvoidmain(Stringargs[]){
3HelloServiceservice=newHelloService();
4HellohelloProxy=service.getHelloPort();
5Stringhello=helloProxy.hello("你好");
6System.out.println(hello);
7}
8}

以上方法还稍显繁琐,还有更加简单的方法


二、使用xfire,我这里使用的是myeclipse集成的xfire进行测试的利用xfire开发WebService,可以有三种方法:

1. 一种是从javabean中生成;

2.一种是从wsdl文件中生成;

3. 还有一种是自己建立webservice

步骤如下:

用myeclipse建立webservice工程,目录结构如下:首先建立webservice接口,

代码如下:

1packagecom.myeclipse.wsExample;
2//GeneratedbyMyEclipse
3
{
5
6publicStringexample(Stringmessage);
7
8}
接着实现这个借口:
1packagecom.myeclipse.wsExample;
2//GeneratedbyMyEclipse
3
{
5
6publicStringexample(Stringmessage){
7returnmessage;
8}
9
10}



修改service.xml文件,加入以下代码:

1<service>
2<name>HelloWorldService</name>
3<serviceClass>
4com.myeclipse.wsExample.IHelloWorldService
5</serviceClass>
6<implementationClass>
7com.myeclipse.wsExample.HelloWorldServiceImpl
8</implementationClass>
9<style>wrapped</style>
10<use>literal</use>
11<scope>application</scope>
12</service>


把整个项目部署到tomcat服务器中打开浏览器,输入http://localhost:8989/HelloWorld/services/HelloWorldService?wsdl,可以看到如下:

然后再展开HelloWorldService后面的wsdl可以看到:

客户端实现如下:

1packagecom.myeclipse.wsExample.client;
2
3importjava.net.MalformedURLException;
4importjava.net.URL;
5
6importorg.codehaus.xfire.XFireFactory;
7importorg.codehaus.xfire.client.Client;
8importorg.codehaus.xfire.client.XFireProxyFactory;
9importorg.codehaus.xfire.service.Service;
10importorg.codehaus.xfire.service.binding.ObjectServiceFactory;
11
12importcom.myeclipse.wsExample.IHelloWorldService;
13
14publicclassHelloWorldClient{
15publicstaticvoidmain(String[]args)throwsMalformedURLException,Exception{
16//TODOAuto-generatedmethodstub
17Services=newObjectServiceFactory().create(IHelloWorldService.class);
18XFireProxyFactoryxf=newXFireProxyFactory(XFireFactory.newInstance().getXFire());
19Stringurl="

20
21try
22{
23
24IHelloWorldServicehs=(IHelloWorldService)xf.create(s,url);
25Stringst=hs.example("zhangjin");
26System.out.print(st);
27}
28catch(Exceptione)
29{
30e.printStackTrace();
31}
32}
33
34}

有时候我们知道一个wsdl地址,比如想用java客户端引用net做得webservice,使用myeclipse引用,但是却出现无法通过验证的错误,这时我们可以直接在类中引用,步骤如下:

1.publicstaticvoidmain(String[]args)throwsMalformedURLException,Exception{
2.//TODOAuto-generatedmethodstub



B. 如何在java web 里面使用webservice技术

一、利用jdk web服务api实现,这里使用基于 SOAP message 的 Web 服务
1.首先建立一个Web services EndPoint:
package Hello;
import javax.jws.WebService;
import javax.jws.WebMethod;
import javax.xml.ws.Endpoint;

@WebService
public class Hello {

@WebMethod
public String hello(String name) {
return "Hello, " + name + "\n";
}

public static void main(String[] args) {
// create and publish an endpoint
Hello hello = new Hello();
Endpoint endpoint = Endpoint.publish("http://localhost:8080/hello", hello);
}
}

2.使用 apt 编译 Hello.java(例:apt -d [存放编译后的文件目录] Hello.java ) ,会生成 jaws目录
3.使用java Hello.Hello运行,然后将浏览器指向http://localhost:8080/hello?wsdl就会出现下列显示

4.使用wsimport 生成客户端

使用如下:wsimport -p . -keep http://localhost:8080/hello?wsdl

这时,会在当前目录中生成如下文件:

5.客户端程序:

1class HelloClient{
2public static void main(String args[]) {
3 HelloService service = new HelloService();
4 Hello helloProxy = service.getHelloPort();
5 String hello = helloProxy.hello("你好");
6 System.out.println(hello);
7 }
8}
9

以上方法还稍显繁琐,还有更加简单的方法

二、使用xfire,我这里使用的是myeclipse集成的xfire进行测试的
利用xfire开发WebService,可以有三种方法:
1一种是从javabean 中生成;
2 一种是从wsdl文件中生成;
3 还有一种是自己建立webservice
步骤如下:
用myeclipse建立webservice工程,目录结构如下:

首先建立webservice接口,
代码如下:

1package com.myeclipse.wsExample;
2//Generated by MyEclipse
3
4public interface IHelloWorldService {
5
6 public String example(String message);
7
8}
接着实现这个借口:
1package com.myeclipse.wsExample;
2//Generated by MyEclipse
3
4public class HelloWorldServiceImpl implements IHelloWorldService {
5
6 public String example(String message) {
7 return message;
8 }
9
10}
修改service.xml 文件,加入以下代码:

1<service>
2 <name>HelloWorldService</name>
3 <serviceClass>
4 com.myeclipse.wsExample.IHelloWorldService
5 </serviceClass>
6 <implementationClass>
7 com.myeclipse.wsExample.HelloWorldServiceImpl
8 </implementationClass>
9 <style>wrapped</style>
10 <use>literal</use>
11 <scope>application</scope>
12 </service>
把整个项目部署到tomcat服务器中 ,打开浏览器,输入http://localhost:8989/HelloWorld/services/HelloWorldService?wsdl,可以看到如下:

然后再展开HelloWorldService后面的wsdl可以看到:

客户端实现如下:
1package com.myeclipse.wsExample.client;
2
3import java.net.MalformedURLException;
4import java.net.URL;
5
6import org.codehaus.xfire.XFireFactory;
7import org.codehaus.xfire.client.Client;
8import org.codehaus.xfire.client.XFireProxyFactory;
9import org.codehaus.xfire.service.Service;
10import org.codehaus.xfire.service.binding.ObjectServiceFactory;
11
12import com.myeclipse.wsExample.IHelloWorldService;
13
14public class HelloWorldClient {
15public static void main(String[] args) throws MalformedURLException, Exception {
16// TODO Auto-generated method stub
17Service s=new ObjectServiceFactory().create(IHelloWorldService.class);
18XFireProxyFactory xf=new XFireProxyFactory(XFireFactory.newInstance().getXFire());
19String url="http://localhost:8989/HelloWorld/services/HelloWorldService";
20
21 try
22 {
23
24 IHelloWorldService hs=(IHelloWorldService) xf.create(s,url);
25 String st=hs.example("zhangjin");
26 System.out.print(st);
27 }
28 catch(Exception e)
29 {
30 e.printStackTrace();
31 }
32 }
33
34}
35
这里再说点题外话,有时候我们知道一个wsdl地址,比如想用java客户端引用.net 做得webservice,使用myeclipse引用,但是却出现无法通过验证的错误,这时我们可以直接在类中引用,步骤如下:

1public static void main(String[] args) throws MalformedURLException, Exception {
2 // TODO Auto-generated method stub
3 Service s=new ObjectServiceFactory().create(IHelloWorldService.class);
4 XFireProxyFactory xf=new XFireProxyFactory(XFireFactory.newInstance().getXFire());
5
6
7//远程调用.net开发的webservice
8Client c=new Client(new URL("http://www.webxml.com.cn/webservices/qqOnlineWebService.asmx?wsdl"));
9 Object[] o=c.invoke("qqCheckOnline", new String[]{"531086641","591284436"});
10
11//调用.net本机开发的webservice
12Client c1=new Client(new URL("http://localhost/zj/Service.asmx?wsdl"));
13Object[] o1=c1.invoke("HelloWorld",new String[]{});
14
15}

C. 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


http://cxf.apache.org

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

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

E. java做webservice接口怎么跟别的系统对接

一般是用webservice来实现这种对接需求。
你可以先整理一下有多少个功能点需要从你这获取数据(归纳统计共需要多少个webservice接口)
然后逐个webservice确认需要哪些参数进行查询
写一个webservice接口文档提供给银行那边的系统开发人员即可

F. java调用webservice接口具体怎么调用

Java调用WebService可以直接使用Apache提供的axis.jar自己编写代码,或者利用Eclipse自动生成WebService Client代码,利用其中的Proxy类进行调用。理论上是一样的,只不过用Eclipse自动生成代码省事些。 1、编写代码方式: package com.yun.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("http://www.w3.org/2001/XMLSchema", "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("http://www.w3.org/2001/XMLSchema", "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("http://www.w3.org/2001/XMLSchema", "string"), java.lang.String.class, false, false); param.setOmittable(true); oper.addParameter(param);

G. java编写一个webservice接口,接口中的方法参数应该是什么,返回值呢(急)要求见补充,谢谢啦

private JaxWsProxyFactoryBean getProxyFactory(Class<?> clazz,String address){
JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
factory.setServiceClass(clazz);
factory.setAddress(address);
return factory;
}

public void save(){
JaxWsProxyFactoryBean factory = getProxyFactory(ProgramVerWebService.class, PROGRAM_VER_WEB_SERVICE_ADDRESS);
ProgramVerWebService service = (ProgramVerWebService)factory.create();
String result = service.getEmpByAccount("abc");

System.out.println(result);
}
本地建立接口,如ProgramVerWebService,然后建立factory调用即可,这里用的是cxf
也可以用axis2通过解析wsdl文件来直接生成本地代码,调用如下:
stub = new ();
GetEmpByAccountE e = new GetEmpByAccountE();
GetEmpByAccount request = new GetEmpByAccount();

request.setArg0("abc");

e.setGetEmpByAccount(request);

String result = stub.getEmpByAccount(e).getGetEmpByAccountResponse().get_return();
System.out.println(result);

H. java怎样发布webservice接口详细步骤

用netbeans建项目的时候,选择webservice就可以了。
//
你用main函数能启动一个新端口?
既然用main函数能行,就把main函数功能,写到一个类的构造函数中,把这个类配置到spring配置文件中,spring启动的时候自然会装载类,装载类的时候自然就能执行到这部分代码了。

I. java调用 webservice 接口怎么调用

太简单了,这个跟Java访问url是一样的:

	/**
*程序中访问http数据接口
*@paramurlStrwebService地址地址
*/
(StringurlStr){
/**网络的url地址*/
URLurl=null;
/**http连接*/
HttpURLConnectionhttpConn=null;
/**//**输入流*/
BufferedReaderin=null;
StringBuffersb=newStringBuffer();
try{
url=newURL(urlStr);
in=newBufferedReader(newInputStreamReader(url.openStream(),"UTF-8"));
Stringstr=null;
while((str=in.readLine())!=null){
sb.append(str);
}
}catch(Exceptionex){
ex.printStackTrace();
}finally{
try{
if(in!=null){
in.close();
}
}catch(IOExceptionex){
ex.printStackTrace();
}
}
Stringresult=sb.toString();
System.out.println(result);
returnresult;
}

然后解析字符串就好了。是不是很简单

阅读全文

与javawebservice接口开发相关的资料

热点内容
成都市区建成面积算法 浏览:656
智能家居单片机 浏览:93
买男装用什么app好 浏览:851
文件夹合并了怎么拆开 浏览:256
波段副图源码无未来函数 浏览:84
livecn服务器地址 浏览:257
程序员这个工作真的很吃香吗 浏览:844
程序员和数学分析师待遇 浏览:678
压缩气弹簧怎么拆 浏览:321
华为公有云服务器添加虚拟ip 浏览:209
程序员和运营哪个累 浏览:24
抖音安卓信息提示音怎么设置 浏览:454
光速虚拟机的共享文件夹 浏览:248
程序员培训机构发的朋友圈真实性 浏览:742
天干地支简单算法 浏览:299
下载个压缩文件 浏览:300
普通人电脑关机vs程序员关机 浏览:628
米酷建站源码 浏览:115
氢气app怎么搜搭配 浏览:619
pdf绿盟 浏览:505