java开发webservice接口,java怎样提供webservice接口
java开发webservice接口,java怎样提供webservice接口详细介绍
本文目录一览: java怎样提供webservice接口
Java通过WSDL文件来调用webservice:
注意,以下的代码并没有经过真正的测试,只是说明这些情况,不同版本的Axis相差很大,大家最好以apache网站上的例子为准,这里仅仅用于说明其基本用法。
1,直接AXIS调用远程的web service
这种方法比较适合那些高手,他们能直接看懂XML格式的WSDL文件,我自己是看不懂的,尤其我不是专门搞这行的,即使一段时间看懂,后来也就忘记了。直接调用模式如下:
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);
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();
}
}
}
3,使用wsdl2java把WSDL文件转成本地类,然后像本地类一样使用,即可。
这是像我这种懒人最喜欢的方式,仍然以前面的global weather report为例。
首先 java org.apache.axis.wsdl.WSDL2Java http://www.webservicex.net/globalweather.asmx.WSDL
原本的网址是http://www.webservicex.net/globalweather.asmx?WSDL,中间个各问号,但是Linux下面它不能解析,所以去掉问号,改为点号。
那么就会出现4个文件:
GlobalWeather.java GlobalWeatherLocator.java GlobalWeatherSoap.java GlobalWeatherSoapStub.java
其中GlobalWeatherSoap.java是我们最为关心的接口文件,如果你对RMI等SOAP实现的具体细节不感兴趣,那么你只需要看接口文件即可,在使用的时候,引入这个接口即可,就好像使用本地类一样。
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 = "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());
}
}
}
java编写一个webservice接口,接口中的方法参数应该是什么,返回值呢?(急)要求见补充,谢谢啦
直接用soupUI测试,如果好用,就是客户端的问题,服务端不用理会。
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文件来直接生成本地代码,调用如下:
ProgramVerWebServiceImplServiceStub stub = new ProgramVerWebServiceImplServiceStub();
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);
我想用java做一个web services.请问该怎么做
网上有很多答案的,下面这个是个专业性的答案:
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代码
这个文件比较容易理解,就不解释了。
5)配置CXFServlet
在web.xml文件中配置CXFServlet,加载cxf-beans.xml文件,内容如下:
Web.xml代码
id="WebApp_ID" version="2.5">
contextConfigLocation
WEB-INF/cxf-beans.xml
org.springframework.web.context.ContextLoaderListener
cxf
org.apache.cxf.transport.servlet.CXFServlet
1
cxf
/services/*
把工程部署到中间件,如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代码
MessageService
com.lamp.service.MessageService
com.lamp.service.impl.MessageServiceImpl
[xml] view plaincopyprint?
MessageService
com.lamp.service.MessageService
com.lamp.service.impl.MessageServiceImpl
最后在web.xml中配置xfire的servlet
Xml代码
XFireServlet
XFireServlet
/servlet/XFireServlet/*
XFireServlet
/services/*
[xml] view plaincopyprint?
XFireServlet
XFireServlet
/servlet/XFireServlet/*
XFireServlet
/services/*
这样服务器端开发完毕,现在开始客户端的开发
新建一个java project也将xfire相关的jar引入,我用ant在客户端生成代理对象,在项目路径下新建build.xml,代码为
Xml代码
<wsgen outputdirectory="${src.dir}"
wsdl="${wsdl.dir}" package="com.lamp.ws.client" overwrite="true"/>
[xml] view plaincopyprint?
<wsgen outputdirectory="${src.dir}"
wsdl="${wsdl.dir}" package="com.lamp.ws.client" overwrite="true"/>
java如何写webservice服务端
Java 中的 Web Service 分为基于 SOAP 的和基于 REST 的两种,下面简单说一个基于 SOAP 的例子。要使用 JDK6u4 之后的版本才能编译通过。
先编写一个 Web Service 的接口:
@WebService@SOAPBinding(style = Style.RPC) public interface TimeServer { @WebMethod String getTimeAsString(); @WebMethod long getTimeAsElapsed();}再编写 Web Service 实现:
import java.util.Date;import javax.jws.WebService;@WebService(endpointInterface = "test.TimeServer")public class TimeServerImpl implements TimeServer { public String getTimeAsString() { return new Date().toString(); } public long getTimeAsElapsed() { return new Date().getTime(); }}最后启动 Web Service:
public class TimeServerPublisher { public static void main(String[ ] args) { Endpoint.publish("http://127.0.0.1:9876/ts", new TimeServerImpl()); }}
如果正常启动,可以用浏览器访问 http://127.0.0.1:9876/ts?wsdl 看到这个 Web Service 的 wsdl 文档。
如何提供java的基于documentliteral的webservice接口
一、利用jdk web服务api实现,这里使用基于SOAP message的Web服务
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
①.首先建立一个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("
, hello);
}
}
②.使用apt编译Hello.java(例:apt -d [存放编译后的] Hello.java ) ,
会生成jaws目录
③.使用java Hello.Hello运行,然后将浏览器指向
就会出现下列显示
④.使用wsimport生成客户端使用如下:
wsimport -p . -keep
这时,会在中生成如下文件:
⑤.客户端程序:
1 class HelloClient{
2 public 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 }
以上方法还稍显繁琐,还有更加简单的方法
二、使用xfire,我这里使用的是myeclipse集成的xfire进行测试的利用xfire开发WebService,可以有三种方法:
1. 一种是从javabean中生成;
2. 一种是从wsdl文件中生成;
3. 还有一种是自己建立webservice
步骤如下:
用myeclipse建立webservice工程,目录结构如下:首先建立webservice接口,
代码如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
1 package com.myeclipse.wsExample;
2 //Generated by MyEclipse
3
4 public interface IHelloWorldService {
5
6 public String example(String message);
7
8 }
接着实现这个借口:
1 package com.myeclipse.wsExample;
2 //Generated by MyEclipse
3
4 public class HelloWorldServiceImpl implements IHelloWorldService {
5
6 public String example(String message) {
7 return message;
8 }
9
10 }
修改 service.xml文件,加入以下代码:
1
2
3
4
5
6
7
8
9
10
11
12
1
2
HelloWorldService
3
4 com.myeclipse.wsExample.IHelloWorldService
5
6
7 com.myeclipse.wsExample.HelloWorldServiceImpl
8
9 < style>wrapped
10
literal
11
application
12
把整个项目部署到tomcat服务器中打开浏览器,输入http://localhost:8989/HelloWorld/services/HelloWorldService?wsdl,可以看到如下:
然后再展开HelloWorldService后面的wsdl可以看到:
客户端实现如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
1 package com.myeclipse.wsExample.client;
2
3 import java.net.MalformedURLException;
4 import java.net.URL;
5
6 import org.codehaus.xfire.XFireFactory;
7 import org.codehaus.xfire.client.Client;
8 import org.codehaus.xfire.client.XFireProxyFactory;
9 import org.codehaus.xfire.service.Service;
10 import org.codehaus.xfire.service.binding.ObjectServiceFactory;
11
12 import com.myeclipse.wsExample.IHelloWorldService;
13
14 public class HelloWorldClient {
15 public static void main(String[] args) throws MalformedURLException, Exception {
16 // TODO Auto-generated method stub
17 Service s=new ObjectServiceFactory().create(IHelloWorldService.class);
18 XFireProxyFactory xf=new XFireProxyFactory(XFireFactory.newInstance().getXFire());
19 String url="
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 }
有时候我们知道一个wsdl地址,比如想用java客户端引用net做得webservice,使用myeclipse引用,但是却出现无法通过验证的错误,这时我们可以直接在类中引用,步骤如下:
1
2
1. public static void main(String[] args) throws MalformedURLException, Exception {
2. // TODO Auto-generated method stub
java语言 编写接口开发需要用到WebService么?
你理解的接口只是系统内部的接口,第三方怎么调用呢?
WebService就是针对第三方开发的 ,当然也可以用基于http协议的restful接口
你要问的应该是数据接口服务吧?
数据接口是提供给第三方调用的服务,主要是为了我们自己的应用得安全性,所以我们只把能供给第三方调用的东西封装在接口服务里。
如果用Java写 可以用WebService 也可以用 http服务写 都可以的。
1、WebService 写的接口服务,调用时需要根据发布地址生成客户端文件才能使用。
WebService 直接用新建 WebService项目
代码如下
import javax.jws.WebService;
import javax.xml.ws.Endpoint;
@WebService
public class XXService {
public static void main(String[] args) {
Endpoint.publish("ip:端口/工程名/XXService", new XXService());
}
public String abc(String aa){
return aa;
}
}
可以直接用http://ip:端口/工程名/XXService?wsdl 查看。
也可以用这个地址生成客户端文件。
2、http的写法使用方便 直接用地址调用 注意get 和post就好。
接口开发种类很多,但是一定是双方定义请求的方式和数据类型,约定大于配置。
接口,java就是对外提供Controller的路径访问!
比如下面:
@Controller
@RequestMapping(value = "/order/info")
我们访问接口地址:http://localhost:8080/order/info
不需要,wenservice只是一个网络传输协议,做servcie并不一定要实现这个协议,做接口只要能够完成相应的功能即可,但是如果你要公开接口,你就必须按照一定标准来,如果不是那你就要有完整的文档和手册来教会接入者怎么使用。
通常所说的接口开发(面向接口开发)说的是数据之间的访问,访问的应该是接口而不是具体类,即对于服务提供者来说,我只是提供了对外开放的接口,内部底层的实现外部是不知道的,并不是说webservice,那只是web开发前后台交互的一种方式
举个小例子:
// 服务提供商提供的api,提供登陆、下载功能public interface ServiceProvider { void login(String name, String passwd); void downLoad(String fileName);}// 服务使用者通过api调用服务public class Customer { ServiceProvider provider; void loginServer(String name, String passwd){ provider.login(name, passwd); } void downLoadFile(String fileName){ provider.downLoad(fileName); }}
WebService是第三方接口,就是可以远程调用服务接口。如果是本机上,直接调用就行了,不需要用WebService技术!
用java怎么写webservice?
JDK1.5就已经有了WS的解决方案,API里面有
下一个spring webservice,里边有helloworld,很简单
下一个spring webservice,里边有helloworld
网上直接百度应该都有吧
Web Services以XML作为数据交换的标准格式,它是跨平台的应用,允许以任何方式创建Web Services,在.NET、Java平台上访问。
在Java平台创建和访问Web Service多通过Axis完成。Axis本质上就是一个SOAP引擎,提供创建服务器端、客户端和网关SOAP操作的基本框架。Axis目前版本是为Java编写的。在使用Axis访问Web Service时,需要引入以下包(10个):axis-ant.jar、axis.jar、commons-discovery-0.2.jar、commons-logging-1.0.4.jar、jaxrpc.jar、log4j-1.2.8.jar、saaj.jar、wsdl4j-1.5.1.jar、activation-1.1.jar和mail-1.4.jar。
(1)访问Java创建的Web Service
在当前Java客户端应用中添加相应的10个Axis包,编写客户端程序:
import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
public class Test {
public static void main(String[] args) throws Exception {
try{
String endpoint = "http://localhost:8080/MyService/services/Hello";
Service service = new Service();
Call call = (Call)service.createCall();
call.setTargetEndpointAddress(new java.net.URL(endpoint));
call.setOperationName("getHello");
String res = (String) call.invoke(new Object[]{});
System.out.println(res);
}
catch (Exception ex){
ex.printStackTrace();
}
}
}
其中两处代码加粗,第一处表示引用Java Web Service的URL,第二处表示公共的方法名称。
java程序如何调用webservice接口,实现发送短信功能
给你一个最简单的方法:
第一、根据http://134.224.102.6:80/CompanySendSmInf/services/SmsInf?wsdl 拿到WSDL文件。
第二、根据Axis的jar包,把WSDL文件生成客服端java代码。(可以把java文件打成jar文件,便于管理。怎么生成java代码,百度里都有说明我就不写了。)
第三、在你工程里用AXIS的功能属性,调用外部接口;给你一个格式模板:
MobileCodeWSLocator l=new MobileCodeWSLocator();//MobileCodeWSLocator是WSDL文件生成客服端java类;
MobileCodeWSSoap s=l.getMobileCodeWSSoap();();//MobileCodeWSSoap 是WSDL文件生成客服端java类
String m=s.getMobileCodeInfo("13800", "");
如果你用Axis生成的java类,格式和上面一样;自己参考一下就懂了。
你上面明显的连接异常,第三方服务明显没有开,WEBSERVICE可以设置户名、密码,像我们行所有的WEBSERVICE都设置,安全考虑吧。还有不懂的可以call我。