用HttpClient访问CXF的RESTful接口

用CXF可以开发RESTful服务,服务接口的输入和输出支持xml和json两种格式。

 

工具类源码:

/**
 * 以get方式访问
 * 
 * @param url 接口url地址
 * @return
 */
public static String get(String url) {
	HttpClient httpClient = new DefaultHttpClient();
	try {
		HttpGet httpGet = new HttpGet(url); 
		httpGet.addHeader("accept", "application/xml");
		
		HttpResponse httpResponse = httpClient.execute(httpGet); 
		int statusCode = httpResponse.getStatusLine().getStatusCode();  
		  
		if (statusCode == HttpStatus.SC_OK) { 
			HttpEntity httpEntity = httpResponse.getEntity(); 
			String result = EntityUtils.toString(httpEntity);
			EntityUtils.consume(httpEntity); 
			return result;
		}
		
	} catch (Exception ex) {  
		ex.printStackTrace();
	} finally {  
		try {
			httpClient.getConnectionManager().shutdown();   
		} catch (Exception ex) {  
			ex.printStackTrace();
		}  
	} 
	return "";
}

/**
 * 以post方式访问
 * 
 * @param url 接口url地址
 * @param xmlBody xml格式的字符串
 * @return
 */
public static String post(String url, String xmlBody) {
	HttpClient httpClient = new DefaultHttpClient();
	try {
		HttpPost httpPost = new HttpPost(url); 
		
		StringEntity input = new StringEntity(xmlBody, "UTF-8");
		input.setContentType("application/xml");
		
		httpPost.setEntity(input);
		
		HttpResponse httpResponse = httpClient.execute(httpPost); 
		int statusCode = httpResponse.getStatusLine().getStatusCode();  
		  
		if (statusCode == HttpStatus.SC_OK) { 
			HttpEntity httpEntity = httpResponse.getEntity(); 
			String result = EntityUtils.toString(httpEntity);
			EntityUtils.consume(httpEntity); 
			return result;
		}
		
	} catch (Exception ex) {  
		ex.printStackTrace();
	} finally {  
		try {
			httpClient.getConnectionManager().shutdown();   
		} catch (Exception ex) {  
			ex.printStackTrace();
		}  
	} 
	return "";
}

/**
 * jaxb对象转成xml格式的字符串
 * 
 * @param jaxbElement jaxb对象,类必须加注解类 @XmlRootElement
 * @return
 */
public static String convertToXML(Object jaxbElement){
	String result = null;
	try{
		JAXBContext context = JAXBContext.newInstance(jaxbElement.getClass());
		Marshaller marshaller = context.createMarshaller();  
		marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);  
		marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");  

		StringWriter writer = new StringWriter();  
		marshaller.marshal(jaxbElement, writer);  
		result = writer.toString();
	}catch(Exception ex){
		ex.printStackTrace();
	}
	return result;
}

/**
 * 格式化xml字符串
 * 
 * @param xmlStr xml字符串
 * @return
 * @throws Exception
 */
public static String formatXML(String xmlStr) throws Exception {  
	SAXReader reader = new SAXReader();  
	Document document = reader.read(new StringReader(xmlStr));  
	String resultXml = null;  
	XMLWriter writer = null;  
	if(document != null){
		try {  
			StringWriter stringWriter = new StringWriter();  
			OutputFormat format = new OutputFormat("    ", true, "UTF-8");  
			writer = new XMLWriter(stringWriter, format);  
			writer.write(document);
			writer.flush();  
			resultXml = stringWriter.getBuffer().toString();  
		}finally{  
			if(writer != null){  
				try{  
					writer.close();
					writer = null;
				}catch(IOException ex){  
					ex.printStackTrace();
				}  
			}  
		}  
	}  
	return resultXml;  
} 

 

接口调用范例:

url = "http://localhost:8080/server/rest/Param/findOne/1/M";
String result = get(url);
System.out.println(formatXML(result));


url = "http://localhost:8080/server/rest/doingLog/save";
DoingLog doingLog = new DoingLog();
doingLog.setCountry("中国");
doingLog.setProvince("广东");
doingLog.setCity("广州");
doingLog.setDeviceType("M");
doingLog.setRemoteAddress("127.0.0.1");
doingLog.setCreateTime(new Date());
result = post(url, convertToXML(doingLog));
System.out.println(result);

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值