This article mainly introduces in detail how to implement simple webservice in java, which has certain reference value. Interested friends can refer to it
The examples in this article share with you how to implement webservice in java. The specific code is for your reference. The specific content is as follows
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. Implement interface
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. Define service
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. Test service
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(); } } }【Related Recommendations】1.
Special Recommendation: "php Programmer Toolbox" V0.1 version download
2. 3.The above is the detailed content of Introduction to the method of completing web service mode in java. For more information, please follow other related articles on the PHP Chinese website!

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于结构化数据处理开源库SPL的相关问题,下面就一起来看一下java下理想的结构化数据处理类库,希望对大家有帮助。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于PriorityQueue优先级队列的相关知识,Java集合框架中提供了PriorityQueue和PriorityBlockingQueue两种类型的优先级队列,PriorityQueue是线程不安全的,PriorityBlockingQueue是线程安全的,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于java锁的相关问题,包括了独占锁、悲观锁、乐观锁、共享锁等等内容,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于多线程的相关问题,包括了线程安装、线程加锁与线程不安全的原因、线程安全的标准类等等内容,希望对大家有帮助。

本篇文章给大家带来了关于Java的相关知识,其中主要介绍了关于关键字中this和super的相关问题,以及他们的一些区别,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于枚举的相关问题,包括了枚举的基本操作、集合类对枚举的支持等等内容,下面一起来看一下,希望对大家有帮助。

封装是一种信息隐藏技术,是指一种将抽象性函式接口的实现细节部分包装、隐藏起来的方法;封装可以被认为是一个保护屏障,防止指定类的代码和数据被外部类定义的代码随机访问。封装可以通过关键字private,protected和public实现。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于平衡二叉树(AVL树)的相关知识,AVL树本质上是带了平衡功能的二叉查找树,下面一起来看一下,希望对大家有帮助。


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

SublimeText3 Linux new version
SublimeText3 Linux latest version

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 English version
Recommended: Win version, supports code prompts!
