本文实例为大家分享了java实现webservice方式的具体代码,供大家参考,具体内容如下
经过测试 jdk1.6.10以下会出现bug 建议使用10以上版本
1、定义接口
package org.enson.chan; import javax.jws.WebService; import javax.jws.soap.SOAPBinding; @WebService @SOAPBinding(style=SOAPBinding.Style.RPC) public interface IMyService { public int add(int a , int b); public int max(int a , int b); }
2、实现接口
package org.enson.chan; import javax.jws.WebService; @WebService(endpointInterface="org.enson.chan.IMyService") public class MyServiceImpl implements IMyService { public int add(int a, int b) { System.out.println(a+"+"+b+"="+(a+b)); return a+b; } public int max(int a, int b) { System.out.println("a与b比较大小,取大值"+((a>b)?a:b)); return (a>b)?a:b; } }
3、定义服务
package org.enson.chan; import javax.xml.ws.Endpoint; public class MyServer { public static void main(String[] args) { String address = "http://localhost:8090/ns"; Endpoint.publish(address, new MyServiceImpl()); } }
4、测试服务
package org.enson.chan; import java.net.MalformedURLException; import java.net.URL; import javax.xml.namespace.QName; import javax.xml.ws.Service; public class TestClient { public static void main(String[] args) { try { URL url = new URL("http://localhost:8090/ns?wsdl"); QName sname = new QName("http://chan.enson.org/", "MyServiceImplService"); //创建服务 Service service = Service.create(url,sname); //实现接口 IMyService ms = service.getPort(IMyService.class); System.out.println(ms.add(12,33)); //以上服务有问题,依然依赖于IMyServie接口 } catch (MalformedURLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
5、TestSoap
import java.io.IOException; import java.io.StringReader; import java.io.StringWriter; import java.net.URL; import javax.xml.bind.JAXBContext; import javax.xml.bind.JAXBException; import javax.xml.bind.Marshaller; import javax.xml.namespace.QName; import javax.xml.soap.MessageFactory; import javax.xml.soap.SOAPBody; import javax.xml.soap.SOAPBodyElement; import javax.xml.soap.SOAPEnvelope; import javax.xml.soap.SOAPException; import javax.xml.soap.SOAPHeader; import javax.xml.soap.SOAPMessage; import javax.xml.soap.SOAPPart; import javax.xml.transform.Source; import javax.xml.transform.Transformer; import javax.xml.transform.TransformerConfigurationException; import javax.xml.transform.TransformerException; import javax.xml.transform.TransformerFactory; import javax.xml.transform.TransformerFactoryConfigurationError; import javax.xml.transform.dom.DOMResult; import javax.xml.transform.stream.StreamSource; import javax.xml.ws.Dispatch; import javax.xml.ws.Service; import javax.xml.ws.soap.SOAPFaultException; import javax.xml.xpath.XPath; import javax.xml.xpath.XPathConstants; import javax.xml.xpath.XPathExpressionException; import javax.xml.xpath.XPathFactory; import org.junit.Test; import org.soap.service.User; import org.w3c.dom.Document; import org.w3c.dom.Node; import org.w3c.dom.NodeList; public class TestSoap { private String ns = "http://service.soap.org/"; private String wsdlUrl = "http://localhost:8989/ms?wsdl"; @Test public void test01() { try { MessageFactory factory = MessageFactory.newInstance(); SOAPMessage message = factory.createMessage(); SOAPPart part = message.getSOAPPart(); SOAPEnvelope envelope = part.getEnvelope(); SOAPBody body = envelope.getBody(); QName qname = new QName("http://java.zttc.edu.cn/webservice", "add","ns");//<ns:add xmlns="http://java.zttc.edu.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(); } } @Test public void test02() { try { URL url = new URL(wsdlUrl); QName sname = new QName(ns,"MyServiceImplService"); Service service = Service.create(url,sname); Dispatch<SOAPMessage> dispatch = service.createDispatch(new QName(ns,"MyServiceImplPort"), SOAPMessage.class, Service.Mode.MESSAGE); SOAPMessage msg = MessageFactory.newInstance().createMessage(); SOAPEnvelope envelope = msg.getSOAPPart().getEnvelope(); SOAPBody body = envelope.getBody(); 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....."); SOAPMessage response = dispatch.invoke(msg); response.writeTo(System.out); System.out.println(); 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(); } } @Test public void test03() { try { URL url = new URL(wsdlUrl); QName sname = new QName(ns,"MyServiceImplService"); Service service = Service.create(url,sname); Dispatch<Source> dispatch = service.createDispatch(new QName(ns,"MyServiceImplPort"), Source.class, Service.Mode.PAYLOAD); User user = new User(3,"zs","张三","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); String payload = "<nn:addUser xmlns:nn=\""+ns+"\">"+writer.toString()+"</nn:addUser>"; System.out.println(payload); StreamSource rs = new StreamSource(new StringReader(payload)); Source response = (Source)dispatch.invoke(rs); Transformer tran = TransformerFactory.newInstance().newTransformer(); DOMResult result = new DOMResult(); tran.transform(response, result); 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(); } } @Test public void test04() { try { URL url = new URL(wsdlUrl); QName sname = new QName(ns,"MyServiceImplService"); Service service = Service.create(url,sname); Dispatch<SOAPMessage> dispatch = service.createDispatch(new QName(ns,"MyServiceImplPort"), SOAPMessage.class, Service.Mode.MESSAGE); SOAPMessage msg = MessageFactory.newInstance().createMessage(); SOAPEnvelope envelope = msg.getSOAPPart().getEnvelope(); SOAPBody body = envelope.getBody(); SOAPHeader header = envelope.getHeader(); if(header==null) header = envelope.addHeader(); QName hname = new QName(ns,"authInfo","nn"); header.addHeaderElement(hname).setValue("aabbccdd"); QName ename = new QName(ns,"list","nn");//<nn:add xmlns="xx"/> body.addBodyElement(ename); msg.writeTo(System.out); System.out.println("\n invoking....."); SOAPMessage response = dispatch.invoke(msg); response.writeTo(System.out); System.out.println(); 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(); } } @Test public void test05() { try { URL url = new URL(wsdlUrl); QName sname = new QName(ns,"MyServiceImplService"); Service service = Service.create(url,sname); Dispatch<SOAPMessage> dispatch = service.createDispatch(new QName(ns,"MyServiceImplPort"), SOAPMessage.class, Service.Mode.MESSAGE); SOAPMessage msg = MessageFactory.newInstance().createMessage(); SOAPEnvelope envelope = msg.getSOAPPart().getEnvelope(); SOAPBody body = envelope.getBody(); 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....."); 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(); } } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持小牛知识库。
本文向大家介绍Java WebService 简单实例(附实例代码),包括了Java WebService 简单实例(附实例代码)的使用技巧和注意事项,需要的朋友参考一下 前言:朋友们开始以下教程前,请先看第五大点的注意事项,以避免不必要的重复操作。 一、准备工作(以下为本实例使用工具) 1、MyEclipse10.7.1 2、JDK 1.6.0_22 二、创建服务端 1、创建【Web Ser
本文向大家介绍四种php中webservice实现的简单架构方法及实例,包括了四种php中webservice实现的简单架构方法及实例的使用技巧和注意事项,需要的朋友参考一下 一:PHP本身的SOAP 所有的webservice都包括服务端(server)和客户端(client)。 要使用php本身的soap首先要把该拓展安装好并且启用。下面看具体的code 首先这是服务端实现: 然后是客户端 就
本文向大家介绍Eclipse+Webservice简单开发实例,包括了Eclipse+Webservice简单开发实例的使用技巧和注意事项,需要的朋友参考一下 1.实例1(主要看到[2]) 1.1.系统功能: 开发一个计算器服务CalculateService,这个服务包含加(plus)、减(minus)、乘(multiply)、除(divide)的操作。 1.2.开发前准备: 安装Eclipse
本文向大家介绍java简单网页抓取的实现方法,包括了java简单网页抓取的实现方法的使用技巧和注意事项,需要的朋友参考一下 本文实例讲述了java简单网页抓取的实现方法。分享给大家供大家参考。具体分析如下: 背景介绍 一 tcp简介 1 tcp 实现网络中点对点的传输 2 传输是通过ports和sockets ports提供了不同类型的传输(例如 http的port是80) 1
本文向大家介绍php单例模式的简单实现方法,包括了php单例模式的简单实现方法的使用技巧和注意事项,需要的朋友参考一下 php单例模式的简单实现方法 首先我们要知道明确单例模式这个概念,那么什么是单例模式呢? 单例模式顾名思义,就是只有一个实例。 作为对象的创建模式, 单例模式确保某一个类只有一个实例,而且自行实例化并向整个系统提供这个实例, 这个类我们称之为单例类。 单例模式的要点有三个: 一是
本文向大家介绍轮播的简单实现方法,包括了轮播的简单实现方法的使用技巧和注意事项,需要的朋友参考一下 1.闪现方式的轮播 不论述,实现比较简单,效果也比较好 2.滑动轮播 以下面的html代码为例(向左滑动) 插件源码:实现向左和向上轮播,手动切换也是向左和向上切换(手动切换关键源码) 滑动轮播的实现方式主要有两种 1)切换父元素margin-left,将第一个子元素不断添加到父容器结尾 简单实现