欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

java实现简单的webservice方式

程序员文章站 2024-02-21 11:55:00
本文实例为大家分享了java实现webservice方式的具体代码,供大家参考,具体内容如下 经过测试 jdk1.6.10以下会出现bug 建议使用10以上版本 1...

本文实例为大家分享了java实现webservice方式的具体代码,供大家参考,具体内容如下

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();
 } 
 }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。