導航:首頁 > 編程語言 > 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介面開發相關的資料

熱點內容
不去互聯網程序員 瀏覽:550
電腦qq郵箱解壓的圖片保存在哪裡 瀏覽:544
嵌入命令行 瀏覽:91
檔案為什麼被加密 瀏覽:485
十天學會單片機13 瀏覽:875
榮耀怎麼設置讓app一直運行 瀏覽:992
共享文件夾能在哪裡找到 瀏覽:435
旅遊訂旅店用什麼app 瀏覽:239
一個女程序員的聲音 瀏覽:496
魔術app怎麼用 瀏覽:340
單片機有4個8位的io口 瀏覽:897
win10rar解壓縮軟體 瀏覽:169
plc教程pdf 瀏覽:668
pythonshell清屏命令 瀏覽:279
檢測到加密狗注冊伺服器失敗 瀏覽:205
解壓後手機如何安裝 瀏覽:519
極客學院app為什麼下架 瀏覽:14
圖片批量壓縮綠色版 瀏覽:656
東北程序員帥哥 瀏覽:709
加密封條風噪小 瀏覽:975