1.创建接口
package org.soap.service;
import java.util.List;
import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService;
@WebService
public interface IMyService {
@WebResult(name="addResult")
public int add(@WebParam(name="a")int a,@WebParam(name="b")int b);
@WebResult(name="user")
public User addUser(@WebParam(name="user")User user);
@WebResult(name="user")
public User login(@WebParam(name="username")String username,
@WebParam(name="password")String password)throws UserException;
@WebResult(name="user")
public List<User> list(@WebParam(header=true,name="authInfo")String authInfo);
}
2.创建实现类
package org.soap.service;
import java.util.ArrayList;
import java.util.List;
import javax.jws.WebService;
@WebService(endpointInterface="org.soap.service.IMyService")
public class MyServiceImpl implements IMyService {
private static List<User> users = new ArrayList<User>();
//@HandlerChain(file="handler-chain.xml")
public MyServiceImpl() {
users.add(new User(1,"admin","wxh","111111"));
}
@Override
public int add(int a, int b) {
System.out.println("a+b="+(a+b));
return a+b;
}
@Override
public User addUser(User user) {
users.add(user);
return user;
}
@Override
public User login(String username, String password) throws UserException{
for(User user:users) {
if(username.equals(user.getUsername())&&password.equals(user.getPassword()))
return user;
}
throw new UserException("用户不存在");
}
@Override
public List<User> list(String authInfo) {
System.out.println(authInfo);
return users;
}
}
3.开启服务
package org.soap.service;
import javax.xml.ws.Endpoint;
public class MyServer {
public static void main(String[] args) {
Endpoint.publish("http://localhost:8889/ns", new MyServiceImpl());
}
}
4.SOAP 消息的建立
private String ns = "http://service.soap.org/";
private String wsdlUrl = "http://localhost:8889/ns?wsdl";
@Test
public void test01() {
try {
//1、创建消息工厂
MessageFactory factory = MessageFactory.newInstance();
//2、根据消息工厂创建SoapMessage
SOAPMessage message = factory.createMessage();
//3、创建SOAPPart
SOAPPart part = message.getSOAPPart();
//4、获取SOAPENvelope
SOAPEnvelope envelope = part.getEnvelope();
//5、可以通过SoapEnvelope有效的获取相应的Body和Header等信息
SOAPBody body = envelope.getBody();
//6、根据Qname创建相应的节点(QName就是一个带有命名空间的)
QName qname = new QName("http://java.soap.third.cn/webservice",
"add","ns");//<ns:add xmlns="http://java.soap.third.cn/webservice"/>
//如果使用以下方式进行设置,会见<>转换为<和>
//body.addBodyElement(qname).setValue("<a>1</a><b>2</b>");
SOAPBodyElement ele = body.addBodyElement(qname);
ele.addChildElement("a").setValue("22");
ele.addChildElement("b").setValue("33");
//打印消息信息
message.writeTo(System.out);
} catch (SOAPException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
5.SOAP显示打印消息
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"><SOAP-ENV:Header/><SOAP-ENV:Body><ns:add xmlns:ns="http://java.soap.third.cn/webservice"><a>22</a><b>33</b></ns:add></SOAP-ENV:Body></SOAP-ENV:Envelope>
6.SOAP消息的创建与服务器端进行交互MESSAGE方式
@Test
public void test02() {
try {
//1、创建服务(Service)
URL url = new URL(wsdlUrl);
QName sname = new QName(ns,"MyServiceImplService");
Service service = Service.create(url,sname);
//2、创建Dispatch
Dispatch<SOAPMessage> dispatch = service.createDispatch(new QName(ns,"MyServiceImplPort"),
SOAPMessage.class, Service.Mode.MESSAGE);
//3、创建SOAPMessage
SOAPMessage msg = MessageFactory.newInstance().createMessage();
SOAPEnvelope envelope = msg.getSOAPPart().getEnvelope();
SOAPBody body = envelope.getBody();
//4、创建QName来指定消息中传递数据
QName ename = new QName(ns,"add","nn");//<nn:add xmlns="xx"/>
SOAPBodyElement ele = body.addBodyElement(ename);
ele.addChildElement("a").setValue("22");
ele.addChildElement("b").setValue("33");
msg.writeTo(System.out);
System.out.println("\n invoking.....");
//5、通过Dispatch传递消息,会返回响应消息
SOAPMessage response = dispatch.invoke(msg);
response.writeTo(System.out);
System.out.println();
//将响应的消息转换为dom对象
Document doc = response.getSOAPPart().getEnvelope().getBody().extractContentAsDocument();
String str = doc.getElementsByTagName("addResult").item(0).getTextContent();
System.out.println(str);
} catch (SOAPException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
7.显示打印信息
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"><SOAP-ENV:Header/><SOAP-ENV:Body><nn:add xmlns:nn="http://service.soap.org/"><a>22</a><b>33</b></nn:add></SOAP-ENV:Body></SOAP-ENV:Envelope>
invoking.....
<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/"><S:Header/><S:Body><ns2:addResponse xmlns:ns2="http://service.soap.org/"><addResult>55</addResult></ns2:addResponse></S:Body></S:Envelope>
55
8.SOAP消息的创建与服务器端进行交互PAYLOAD方式
@Test
public void test03() {
try {
//1、创建服务(Service)
URL url = new URL(wsdlUrl);
QName sname = new QName(ns,"MyServiceImplService");
Service service = Service.create(url,sname);
//2、创建Dispatch(通过源数据的方式传递)
Dispatch<Source> dispatch = service.createDispatch(new QName(ns,"MyServiceImplPort"),
Source.class, Service.Mode.PAYLOAD);
//3、根据用户对象创建相应的xml
User user = new User(3,"zs","wr","11111");
JAXBContext ctx = JAXBContext.newInstance(User.class);
Marshaller mar = ctx.createMarshaller();
mar.setProperty(Marshaller.JAXB_FRAGMENT, true);
StringWriter writer= new StringWriter();
mar.marshal(user, writer);
//4、封装相应的part addUser
String payload = "<nn:addUser xmlns:nn=\""+ns+"\">"+writer.toString()+"</nn:addUser>";
System.out.println(payload);
StreamSource rs = new StreamSource(new StringReader(payload));
//5、通过dispatch传递payload
Source response = (Source)dispatch.invoke(rs);
//6、将Source转化为DOM进行操作,使用Transform对象转换
Transformer tran = TransformerFactory.newInstance().newTransformer();
DOMResult result = new DOMResult();
tran.transform(response, result);
//7、处理相应信息(通过xpath处理)
XPath xpath = XPathFactory.newInstance().newXPath();
NodeList nl = (NodeList)xpath.evaluate("//user", result.getNode(),XPathConstants.NODESET);
User ru = (User)ctx.createUnmarshaller().unmarshal(nl.item(0));
System.out.println(ru.getNickname());
} catch (IOException e) {
e.printStackTrace();
} catch (JAXBException e) {
e.printStackTrace();
} catch (TransformerConfigurationException e) {
e.printStackTrace();
} catch (TransformerFactoryConfigurationError e) {
e.printStackTrace();
} catch (TransformerException e) {
e.printStackTrace();
} catch (XPathExpressionException e) {
e.printStackTrace();
}
}
9.SOAP消息的创建与服务器端进行交互MESSAGE方式,增加head信息
@Test
public void test04() {
try {
//1、创建服务(Service)
URL url = new URL(wsdlUrl);
QName sname = new QName(ns,"MyServiceImplService");
Service service = Service.create(url,sname);
//2、创建Dispatch
Dispatch<SOAPMessage> dispatch = service.createDispatch(new QName(ns,"MyServiceImplPort"),
SOAPMessage.class, Service.Mode.MESSAGE);
//3、创建SOAPMessage
SOAPMessage msg = MessageFactory.newInstance().createMessage();
SOAPEnvelope envelope = msg.getSOAPPart().getEnvelope();
SOAPBody body = envelope.getBody();
//3.2、处理header信息
SOAPHeader header = envelope.getHeader();
if(header==null) header = envelope.addHeader();
QName hname = new QName(ns,"authInfo","nn");
header.addHeaderElement(hname).setValue("wxh");
//4、创建QName来指定消息中传递数据
QName ename = new QName(ns,"list","nn");//<nn:add xmlns="xx"/>
body.addBodyElement(ename);
msg.writeTo(System.out);
System.out.println("\n invoking.....");
//5、通过Dispatch传递消息,会返回响应消息
SOAPMessage response = dispatch.invoke(msg);
response.writeTo(System.out);
System.out.println();
//将响应的消息转换为dom对象
Document doc = response.getSOAPBody().extractContentAsDocument();
NodeList nl = doc.getElementsByTagName("user");
JAXBContext ctx = JAXBContext.newInstance(User.class);
for(int i=0;i<nl.getLength();i++) {
Node n = nl.item(i);
User u = (User)ctx.createUnmarshaller().unmarshal(n);
System.out.println(u.getNickname());
}
} catch (SOAPException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (JAXBException e) {
e.printStackTrace();
}
}
10SOAP消息的创建与服务器端进行交互MESSAGE方式,异常信息
@Test
public void test05() {
try {
//1、创建服务(Service)
URL url = new URL(wsdlUrl);
QName sname = new QName(ns,"MyServiceImplService");
Service service = Service.create(url,sname);
//2、创建Dispatch
Dispatch<SOAPMessage> dispatch = service.createDispatch(new QName(ns,"MyServiceImplPort"),
SOAPMessage.class, Service.Mode.MESSAGE);
//3、创建SOAPMessage
SOAPMessage msg = MessageFactory.newInstance().createMessage();
SOAPEnvelope envelope = msg.getSOAPPart().getEnvelope();
SOAPBody body = envelope.getBody();
//4、创建QName来指定消息中传递数据
QName ename = new QName(ns,"login","nn");//<nn:add xmlns="xx"/>
SOAPBodyElement ele = body.addBodyElement(ename);
ele.addChildElement("username").setValue("ss");
ele.addChildElement("password").setValue("dd");
msg.writeTo(System.out);
System.out.println("\n invoking.....");
//5、通过Dispatch传递消息,会返回响应消息
SOAPMessage response = dispatch.invoke(msg);
response.writeTo(System.out);
System.out.println();
} catch(SOAPFaultException e){
System.out.println(e.getMessage());
} catch (SOAPException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
分享到:
相关推荐
C#开发核心代码,用拼接XML方式,调用封装了实体类的SOAP接口
金蝶调用第三方webservice接口以soap方式实现,包括创建服务service、DIspatch、SOAPMessage、QName、封装数据、Dispatch传递消息、消息转换为doc对象、调用结果判断等
WebService调用第三方服务的案例,中英文自动转换,服务是webxml提供的,有需要的伙伴们可以下载
Webservice调用工具 Soap调用工具
最近项目中遇到需要去掉老系统的webservice服务中的insert方法,找了很久,发现这样最简单,实测有效!!!
都说WebService是一种基于SOAP协议的远程调用标准,对于这个协议理解不深,知道webservice可以将不同操作系统平台、不同语言、不同技术整合到一块,android SDK没有直接调用webservice的库,最常用的是借助ksoap2-...
Webservice三要素 Wsdl(webservice使用说明书)重点掌握 Soap(jaxws开发webservice的传输协议)重点掌握 UDDI(了解) Webservice的使用场景分析(掌握) 学会jaxws基本开发方法(重点) Soap1.1和soap1.2...
如果需要调Web Service往往需要借助一些第三方的库,再加上第三方的加解密算法及包是由第三方提供的,如果直接在UNIX下开发项目成本会比较高,所以增加了一个前置机系统用于socket协议到soap协议的转换,希望这些...
Membrane-monitor是一款好用的Soap监听工具,它既可以是eclipse插件,也是应用程序,本文发布的是win32下的应用程序。 使用方法: (1)运行Membrane-monitor,选择Membrane->...注意:本文件有三个压缩包,这是第三个
本例程是android端如何去调用...android SDK没有直接调用webservice的库,最常用的是借助ksoap2-android这个第三方SDK。 该Demo参考《疯狂Android讲义》并做改动,希望可以帮助到初次接触Androi调用WebService的朋友
在Android平台调用Web Service需要依赖于第三方类库ksoap2,它是一个SOAP Web service客户端开发包,主要用于资源受限制的Java环境如Applets或J2ME应用程序(CLDC/ CDC/MIDP)。认真读完对ksoap2的介绍你会发现并...
Membrane-monitor是一款好用的Soap监听工具,它既可以是eclipse插件,也是应用程序,本文发布的是win32下的应用程序。 使用方法: (1)运行Membrane-monitor,选择Membrane->...注意:本文件有三个压缩包,这是第二个
gSOAP是独立开发包,不需要任何第三方的软件支持(除非你要用到OpenSSL及Zlib)。 与平台无关的gSOAP版本需要你下面的工具编译'soapcpp2'及'wsdl2h'文件: 一个C++编译器(用来编译'wsdl2h'WSDL解析器)。 ...
第三个项目使用了C#来实现PIM。这个项目的目的是为了演示如何将.net技术与Java 技术集成来实现一个完整的应用。一个团队要想获得成功,需要每个成员通力合作,各尽所长。而在一个项目中,也和一个团队是一样的,没有...
在Android SDK中并没有提供调用WebService的库,因此,需要使用第三方类库(KSOAP2)来调用WebService。本文将介绍在Android 中调用WebService的具体细节,并在最后给出一个完整的例子来演示如何使用KSOAP2来调用...
目前Apache Axis已经发展到了第三代,其核心是一个SOAP处理器,用于开发包括客户端,服务器端,SOAP Gateway等各种应用。事实上Apache Axis在了1.0版后,其发行版本还包括了完整的J2EE服务器插件, WSDL支持和生成,...
ksoap2-android用于和WebService通信第三方开源库,WebService 是一种基于SOAP协议的远程调用标准。通过WebService可以将不同操作系统平台,不同语言、不同技术整合到一起
第一步:新建一个Application。 第二步:File----->New----->Other------>WebServices----->Soap Services Importer ... 第二步:在主form上放上一个按钮和一个Httprio组件(在...第三步:书写客户调用程序,原代码如下:
突破jdk与wsdl第三方对xml文本解析领域割据趋势,该脚本既可以支持普通xml解析,也支持soap-xml第三方xml文本的解析; 极大的节省了引入冗余jar资源; 纯底层算法解析,不依赖任何第三方工具包,自主产权维护性强。
1.3 AXIS第三课:AXIS高级应用,使用Handler来增强Web服务的功能 1.4 AXIS第四课:AXIS高级应用,建立安全的AXIS服务 1.5 AXIS第五课:AXIS高级应用,在AXIS服务间传递JavaBean及其安全解决 1.6 使用TCPMonitor监控...