JWS webservice 实验

webservice 发送和接收的示例


1.先创建服务端(service)即发送方法,
新建一个webservice project ,然后创建一个类
import java.io.IOException;
import java.io.StringWriter;


import javax.jws.WebService;
import javax.xml.ws.Endpoint;


import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.output.XMLOutputter;


@WebService
public class ServiceHello {


public String getInfo(String name){
return "测试方法,获取参数为"+ name;
}

public String sendYDSSInfo(){
Document document = new Document();
//根节点
Element root = new Element("SyncYDCGSS");
//CurrentMonth节点
Element currentMonth = new Element("CurrentMonth");
Element year = new Element("Year");
Element month = new Element("Month");
year.setText("2017");
month.setText("5");
currentMonth.addContent(year);
currentMonth.addContent(month);

Element DataArea = new Element("DataArea");
for(int i=0;i<2;i++){
Element info = new Element("YDSS");
createInfoElement(info,"projectType","工程");
createInfoElement(info,"projectName","建设工程");
DataArea.addContent(info);
}

root.addContent(currentMonth);
root.addContent(DataArea);
document.setRootElement(root);

String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><root/>";
try {
StringWriter sw = new StringWriter();
XMLOutputter out = new XMLOutputter();
out.output(document, sw);
xml = sw.toString();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return xml;
}

private void createInfoElement(Element info, String elementName, String userId) {
Element temp = new Element(elementName);
temp.setText(userId);
info.addContent(temp);

}

public static void main(String[] args) {
Endpoint.publish("http://localhost:9001/Service/ServiceHello", new ServiceHello());
System.out.println("发布成功,访问地址:http://localhost:9001/Service/ServiceHello");
}
}
说明:getInfo()方法就是之后给客户端调用的方法
      Endpoint.publish("http://localhost:9001/Service/ServiceHello", new ServiceHello())中 new ServiceHello(),我理解为new 的是提供给客户端调用的方法所在的类
如果 存在一个接口,然后在这个接口的实现类 ServiceHelloImpl 中存在一个 提供给客户端调用的方法 getInfo()  ,则  Endpoint.publish("http://localhost:9001/Service/ServiceHello", new ServiceHelloImpl())




2.Run As Java Application,如果控制台显示: 发布成功,访问地址:http://localhost:9001/Service/ServiceHello 
在浏览器中输入 http://localhost:9001/Service/ServiceHello?wsdl  显示的就是soap 的一些信息(具体的可以参考相关内容)


3.新建一个webservice project 即客户端(client)也就是接收数据方  建完之后在命令行,输入下面这些:
C:\Users\wisdom>wsimport -s F:\webservicetest\TheClient\src -p com.hyan.client -keep http://localhost:9001/Service/ServiceHello?wsdl   
其中:F:\webservicetest\TheClient\src 是刚才新建的客户端项目
-p com.hyan.client 表示会在  TheClient 这个客户端项目下新建一个包 ,包名:com.hyan.client
-keep http://localhost:9001/Service/ServiceHello?wsdl   keep后面的路径就是在刚才在浏览器中输入的地址,其中?wsdl一定要加上去,不要漏了
注意在写路径和包名的时候不要写空格




4.经过上面这个命令 之后 会在 TheClient 中多出很多类,在 TheClient 中建一个 测试类  ServiceTest
import java.io.IOException;
import java.io.StringReader;
import java.io.StringWriter;


import javax.jws.WebService;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.ws.Endpoint;


import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.output.XMLOutputter;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;


import com.hyan.client.ServiceHello;
import com.hyan.client.ServiceHelloService;
import com.hyan.toservice.SendOk;
@WebService
public class ServiceTest {


public static void main(String[] args) {
/*ServiceHello hello = new ServiceHelloService().getServiceHelloPort();
String info = hello.getInfo("qiusu");
System.out.println(info);*/

ServiceHello hello = new ServiceHelloService().getServiceHelloPort();
String sendYDSSInfo = hello.sendYDSSInfo();
System.out.println(sendYDSSInfo);
System.out.println("=============================================================");

//读取xml中的数据
StringReader sr = new StringReader(sendYDSSInfo); 
InputSource is = new InputSource(sr); 
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 
DocumentBuilder builder;
try {
builder = factory.newDocumentBuilder();
org.w3c.dom.Document document = builder.parse(is);
NodeList root = document.getElementsByTagName("SyncYDCGSS");
for(int i=0;i<root.getLength();i++){
System.out.println(root.item(i).getChildNodes().getLength());
}
System.out.println("##############################################################");
NodeList childNodes = root.item(0).getChildNodes();
NodeList currentMonth = childNodes.item(0).getChildNodes();
for(int i=0;i<currentMonth.getLength();i++){
System.out.println(currentMonth.item(i).getTextContent());
}
System.out.println("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
NodeList YDSS = document.getElementsByTagName("YDSS");
System.out.println(YDSS.getLength());
for(int i=0;i<YDSS.getLength();i++){
System.out.println(YDSS.item(i).getTextContent());
}

//接收数据成功后返回状态码给Service端
if(YDSS.getLength()>0){
String sendOkToService = new SendOk().sendOkToService();
Endpoint.publish("http://localhost:9002/Client/SendOk", new SendOk());
System.out.println("客户端发布成功,访问地址:http://localhost:9002/Client/SendOk");
}

} catch (ParserConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();

}


}


Run As Java Application  显示结果如下:
<?xml version="1.0" encoding="UTF-8"?>
<SyncYDCGSS><CurrentMonth><Year>2017</Year><Month>5</Month></CurrentMonth><DataArea><YDSS><projectType>工程</projectType><projectName>建设工程</projectName></YDSS><YDSS><projectType>工程</projectType><projectName>建设工程</projectName></YDSS></DataArea></SyncYDCGSS>


=============================================================
2
##############################################################
2017
5
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
2
工程建设工程
工程建设工程
客户端发布成功,访问地址:http://localhost:9002/Client/SendOk






这个就可以了




参考网站:
http://blog.csdn.net/yipanbo/article/details/46516755





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值