用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);

猜你喜欢

转载自chenjumin.iteye.com/blog/2299080
今日推荐