创建webservice服务, dom4j解析soap报文

创建webservice服务

@WebService(name="prizesWebservice")
public class PrizesWebservice {

    /**
     * 获取目标辨认列表
     * 供系统实现目标列表对外对接,同步接口,立即返回
     * @return
     */
    public String getPrizesAll(){
        return StringUs.PRIZES;  //模拟接口返回XML格式数据
    }
    
    /**
     * 通过输入主键编号,返回视频的http地址
     * 供系统实现返回视频地址对外对接,同步接口,立即返回
     * @param id 主键编号
     * @return
     */
    public String getPrizesFile(String id){
    	return StringUs.PRIZESFile; //模拟接口返回XML格式数据
    }

    //通过EndPoint(端点服务)发布一个WebService
    public static void main(String[] args) {
     /*参数:1,本地的服务地址;
           2,提供服务的类;
      */
     Endpoint.publish("http://192.168.1.110/vicpln/services/prizesWebservice", new PrizesWebservice());
     System.out.println("发布成功!");
    }
}
浏览器访问http://192.168.1.110/vicpln/services/prizesWebservice?wsdl得到如上说明服务正确发布。


使用SOAP获取服务

public class FileServer {
	
	private static final Logger log = Logger.getLogger(FileServer.class);
	
	/**
	 * 使用SOAP响应ws服务接口
	 * @param iName  接口名 
	 * @param parame   参数
	 * @return 
	 */
	public String getFile(String iName,String parame){
		
		//ws服务地址
		String wsURL = "http://192.168.1.110/vicpln/services/prizesWebservice?wsdl";
		//命名空间
		String nameSpace = "http://serverTest.server.box.vanda.com/";
		
		String rtXML = "";
		try {
			String wsdl = wsURL;
	        int timeout = 10000;
	        //SOAP请求
	        StringBuffer sb = new StringBuffer("");
	        sb.append("");
	        sb.append("");
	        sb.append("");
	        sb.append("");
	        
	        //方法是否有参数
	        if(parame==null || "".equals(parame)){
	        	sb.append("ls");
	        }else{
	        	sb.append(""+parame+"");
	        }
	        
	        sb.append("");
	        sb.append("");
	        sb.append("");
	        
	        // HttpClient发送SOAP请求
	        System.out.println("HttpClient 发送SOAP请求");
	        HttpClient client = new HttpClient();
	        PostMethod postMethod = new PostMethod(wsdl);
	        // 设置连接超时
	        client.getHttpConnectionManager().getParams().setConnectionTimeout(timeout);
	        // 设置读取时间超时
	        client.getHttpConnectionManager().getParams().setSoTimeout(timeout);
	        // 然后把Soap请求数据添加到PostMethod中
	        RequestEntity requestEntity = new StringRequestEntity(sb.toString(), "text/xml", "UTF-8");
	        //设置请求头部,否则可能会报 “no SOAPAction header” 的错误
	        postMethod.setRequestHeader("SOAPAction","");
	        // 设置请求体
	        postMethod.setRequestEntity(requestEntity);
	        int status = client.executeMethod(postMethod);
	        // 打印请求状态码
	        System.out.println("status:" + status);
	        // 获取响应体输入流
	        InputStream is = postMethod.getResponseBodyAsStream();
	        // 获取请求结果字符串
	        String result = IOUtils.toString(is);
	        System.out.println("result: " + result);
	
	        rtXML = result;
	        
	        // HttpURLConnection 发送SOAP请求
	        System.out.println("HttpURLConnection 发送SOAP请求");
	        URL url = new URL(wsdl);
	        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
	
	        conn.setRequestProperty("Content-Type", "text/xml; charset=utf-8");
	        conn.setRequestMethod("POST");
	        conn.setUseCaches(false);
	        conn.setDoInput(true);
	        conn.setDoOutput(true);
	        conn.setConnectTimeout(timeout);
	        conn.setReadTimeout(timeout);
	        
	        DataOutputStream dos = new DataOutputStream(conn.getOutputStream());
	        dos.write(sb.toString().getBytes("utf-8"));
	        dos.flush();
	        
	        
	        BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream(), "utf-8"));
	        String line = null;
	        StringBuffer strBuf = new StringBuffer();
	        while ((line = reader.readLine()) != null) {
	            strBuf.append(line);
	        }
	        dos.close();
	        reader.close();
	        
	        System.out.println(strBuf.toString());
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		//解析返回的SOAP响应数据rt,剔除无用数据
		String fileXml = "";
		try {
			Document document  = DocumentHelper.parseText(rtXML);
			DefaultXPath xpath = new DefaultXPath("//return");
			System.out.println("xpath:"+xpath);
			xpath.setNamespaceURIs(Collections.singletonMap("ns2", "http://schemas.xmlsoap.org/soap/envelope/"));
			Element ele = (Element)xpath.selectSingleNode(document);
			
			System.out.println("ele:"+ele.getText());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return fileXml;
	}
}



	
		
		     
		
	

测试类

public class Test {

	public static void main(String[] args) {
		
		FileServer fs = new FileServer();
		//没有参数
		String rt = fs.getFile("getPrizesAll",null);
		System.out.println(rt);
		//有参数
		rt = fs.getFile("getPrizesFile","123");
		System.out.println(rt);
		
	}

}

文章参考:

https://blog.csdn.net/peng2mengmeng/article/details/78683000

https://www.cnblogs.com/redjh/p/6841665.html

感谢前辈经验

你可能感兴趣的:(WebService)