1.创建服务端
import javax.jws.WebMethod;
import javax.jws.WebService;
import javax.xml.ws.Endpoint;
@WebService
public class WebTest {
@WebMethod
public void testWebservice(String data){
System.out.print("接受到请求:" + data);
}
public static void main(String[] args) {
System.out.println("开始发布");
WebTest test = new WebTest();
Endpoint.publish("http://localhost:8000/test", test);
System.out.println("发布成功");
}
}
<!--Published by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is JAX-WS RI 2.1.6 in JDK 6. -->
<!--Generated by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is JAX-WS RI 2.1.6 in JDK 6. -->
<definitions xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://test.com/" xmlns:xsd="http://www.w3.org/2001/XMLSchema"xmlns="http://schemas.xmlsoap.org/wsdl/" targetNamespace="http://test.com/" name="WebTestService">
<types>
<xsd:schema>
<xsd:import namespace="http://test.com/" schemaLocation="http://localhost:8000/test?xsd=1" />
</xsd:schema>
</types>
<message name="testWebservice">
<part name="parameters" element="tns:testWebservice" />
</message>
<message name="testWebserviceResponse">
<part name="parameters" element="tns:testWebserviceResponse" />
</message>
<portType name="WebTest">
<operation name="testWebservice">
<input message="tns:testWebservice" />
<output message="tns:testWebserviceResponse" />
</operation>
</portType>
<binding name="WebTestPortBinding" type="tns:WebTest">
<soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document" />
<operation name="testWebservice">
<soap:operation soapAction="" />
<input>
<soap:body use="literal" />
</input>
<output>
<soap:body use="literal" />
</output>
</operation>
</binding>
<service name="WebTestService">
<port name="WebTestPort" binding="tns:WebTestPortBinding">
<soap:address location="http://localhost:8000/test" />
</port>
</service>
</definitions>
2.客户端通过URLConnection调用
package com.ceb.czsczj.client.init;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.StringReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import com.ceb.czsczj.client.bo.AccountInfoBO;
import com.ceb.czsczj.client.common.util.DateTools;
/**一个简单的请求的例子还没有做完*/
public class ClientTest {
/**
* @Description: demo
* @throws IOException
* @throws DocumentException
* @author fengjiahuan
* @date 2018-2-9上午10:49:25
*===========================================
* 修改人:, 修改时间:, 修改版本:
* 修改备注:
*===========================================
*/
public static void main(String[] args) throws IOException, DocumentException {
ArrayList<AccountInfoBO> list = new ArrayList<AccountInfoBO>();
Map<Object, Object> resMap = new HashMap<Object, Object>();
resMap.put("result", "error");
//创建url地址
URL url = new URL("http://localhost:8000/test");
//打开连接
URLConnection conn = url.openConnection();
//转换成HttpURL
HttpURLConnection httpConn = (HttpURLConnection) conn;
//打开输入输出的开关
httpConn.setDoInput(true);
httpConn.setDoOutput(true);
//设置请求方式
httpConn.setRequestMethod("POST");
//设置请求的头信息
httpConn.setRequestProperty("Content-type", "text/xml;charset=UTF-8");
String date = DateTools.getSysDateStr();
date = date.replaceAll("-", "");
String data = "<burlap:call>" +
"<method>createBusinessData</method>" +
"<string>BANK.****</string>" +
"<string>ABANKTEST</string>" +
"<int>"+date.substring(0, 4)+"</int>" +
"<list>"+ "</list>" + "</burlap:call>";
//获得输出流
OutputStream out = httpConn.getOutputStream();
//发送数据
out.write(data.getBytes());
out.close();
System.out.println(data);
//判断请求成功
if(httpConn.getResponseCode() == 200){
System.out.println("sss");
//获得输入流
InputStream in = httpConn.getInputStream();
//使用输入流的缓冲区
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
in.close();
StringBuffer sb = new StringBuffer();
String line = null;
//读取输入流
while((line = reader.readLine()) != null){
sb.append(line);
}
//创建sax的读取器
SAXReader saxReader = new SAXReader();
//创建文档对象
Document doc = saxReader.read(new StringReader(sb.toString()));
//获得请求响应return元素
List<Element> eles = doc.selectNodes("//list");
for(Element ele : eles){
System.out.println(ele.getText());
}
}
httpConn.disconnect();
/*返回报文数据项*/
resMap.put("result", "success");
System.out.println("请求成功!!");
}
}