原来是用cxf直接做的webservice接口,非常简单,我的上一篇文章已经介绍过了,但是有不足之处是cxf开发的webservice接口,客户端如果也用cxf的jar去调用,
环境必须是jdk1.6或者以上版本,而客户是jdk1.5,所以换了一种方式,服务端不变,客户端用httpclient调用
测试项目存放地址: http://pan.baidu.com/s/1botaRGb
1、服务端接口代码:注意http方式调用的时候,@WebParam必须写上
package com.lbs.webservice.wsinterface;
import javax.jws.WebParam;
import javax.jws.WebService;
@WebService(name="Test12333WS")
public interface Test12333WS {
public Test12333Dto say( String name);
public String testRtJson(@WebParam(name="json") String json);
//@WebMethod( operationName="test", action="http://wsinterface.webservice.lbs.com/")
public void test();
}
2、服务端实现类代码
package com.lbs.webservice.wsinterface;
import java.util.ArrayList;
import java.util.List;
import javax.jws.WebService;
import com.lbs.gioi.commonbusiness.contribution.utils.ToolsUtils;
@WebService
public class Test12333WSIMP implements Test12333WS {
@Override
public Test12333Dto say( String name){
Test12333Dto dto = new Test12333Dto();
dto.setAab001(123456L);
dto.setAab002(name +"Hello World");
return dto;
}
@Override
public String testRtJson( String json) {
System.out.println("传入参数是: " + json );
Test12333Dto dto = new Test12333Dto();
dto.setAab001(10001L);
dto.setAab002("HELLO WORLD");
List<Test12333Dto> dtoList = new ArrayList<Test12333Dto>();
dtoList.add(dto);
String rtnJson = ToolsUtils.getJsonString(dtoList) ;
System.out.println(" 输出参数: " + rtnJson);
return rtnJson;
}
public static void main(String[] args) {
new Test12333WSIMP().testRtJson("{aab001:3001}");
}
@Override
public void test() {
System.out.println("1234567890-------------------");
}
}
1、单纯的httpclient调用webservice仅仅需要引入的包:
httpclient-4.0.1.jar, httpcore-4.0.1.jar
2、用到了json转对象需要引入的包:
commons-beanutils-1.8.0.jar
commons-collections-3.2.1.jar
commons-lang-2.5.jar
ezmorph-1.0.6.jar
json-lib-2.4-jdk15.ja)
package testhttp;
import java.io.StringReader;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.xml.sax.InputSource;
public class TestHttp {
public static void main(String[] args) {
DefaultHttpClient httpClient = null;
try {
httpClient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://127.0.0.1:8090/gioi/services/test12333WS"); //webservice服务地址
String soapRequestData = getRequestXml(); //soap协议的格式,定义了方法和参数
HttpEntity re = new StringEntity(soapRequestData,HTTP.UTF_8);
httppost.setHeader("Content-Type","application/soap+xml; charset=utf-8");
httppost.setEntity(re);
HttpResponse response = httpClient.execute(httppost); //调用接口
//输出的xml
System.out.println("httppost.getEntity() == EntityUtils.toString: " + EntityUtils.toString(httppost.getEntity()));
//返回是否成功的状态
System.out.println(response.getStatusLine().getStatusCode());
if(response.getStatusLine().getStatusCode() == 200) {
//获得输出的字符串
String xmlString = EntityUtils.toString(response.getEntity());
String jsonString = parseXMLSTRING(xmlString);
Test12333Dto dto = jsonToObj(jsonString);
System.out.println(dto.getAab002());
}
} catch (Exception e) {
e.printStackTrace();
} finally {
httpClient.getConnectionManager().shutdown(); //关闭连接
}
}
/**
* 获得请求XML
* @return
*/
private static String getRequestXml(){
StringBuilder sb = new StringBuilder();
sb.append("<?xml version=\"1.0\"?>");
sb.append("<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" ");
sb.append(" xmlns:sam=\"http://wsinterface.webservice.lbs.com/\" "); //前缀,这一串由服务端提供
sb.append(" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"");
sb.append(" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">");
sb.append("<soap:Header/>");
sb.append("<soap:Body>");
sb.append("<sam:testRtJson>"); //“testRtJson”调用方法名
sb.append("<json>老何0001</json>"); //传参,“json”是配置在服务端的参数名称,“老何0001”是要传入的参数值
sb.append("</sam:testRtJson>");
sb.append("</soap:Body>");
sb.append("</soap:Envelope>");
return sb.toString();
}
//解析xml文件,获取其中的返回值
public static String parseXMLSTRING(String xmlString){
String returnJson = "";
try {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse(new InputSource(new StringReader(xmlString)));
Element root = doc.getDocumentElement();//根节点
Node node = root.getFirstChild();
while(!node.getNodeName().equals("return")) {
node = node.getFirstChild();
}
if(node.getFirstChild() != null) returnJson = node.getFirstChild().getNodeValue();
System.out.println("获取的返回参数为:" + returnJson);
} catch (Exception e) {
e.printStackTrace();
}
return returnJson;
}
//转化json字符串为对象
public static Test12333Dto jsonToObj(String jsonString){
Test12333Dto dto = null;
JSONArray array = JSONArray.fromObject(jsonString);
for(int i = 0; i < array.size(); i++){
JSONObject jsonObject = array.getJSONObject(i);
dto = (Test12333Dto)JSONObject.toBean(jsonObject, Test12333Dto.class);
System.out.println(dto.getAab002());
}
return dto;
}
}
===============================================jboss===========================================================
jboss的调用方式唯一需要修改的就亮点,一点,
HttpPost httppost = new HttpPost("http://127.0.0.1:8090/gioi/services/test12333WS"); //webservice服务地址
jboss的需要在路径后加上?wsdl,即
HttpPost httppost = new HttpPost("http://127.0.0.1:8090/gioi/services/test12333WS?wsdl"); //webservice服务地址
其实httppost对应的就是发布services中的 Endpoint address:,如果没有可以去具体的路径中找到< soap:address location="http://localhost:8080/cam-jsf/ws/criminalInfo"/>
第二点
sb.append(" xmlns:sam=\"http://wsinterface.webservice.lbs.com/\" "); //前缀,这一串由服务端提供
这里对应的就是
Target namespace
Target namespace: http://outinterface.huating.com/
第三点,可能别人返回的就是xml字符串,需要转义下
String str = StringEscapeUtils.unescapeXml("<xm>位大强</xm>
");
System.out.println(str);
引入的包
import org.apache.commons.lang.StringEscapeUtils;
=============================可能遇到的错误
本人遇到过一个,在main函数中运行没有问题,在tomcat下运行httpclient.execute()就报错,后面发现是jar包冲突了,有一个http-client4.0,还有一个4.3,吧4.3的删除掉即可
报错详情: