001package org.kuali.asr.handler; 002 003import org.apache.log4j.Logger; 004import org.codehaus.jackson.map.ObjectMapper; 005 006import javax.xml.bind.JAXBContext; 007import javax.xml.bind.Marshaller; 008import java.io.StringWriter; 009 010/** 011 * Created with IntelliJ IDEA. 012 * User: maheswarang 013 * Date: 12/9/13 014 * Time: 6:03 PM 015 * To change this template use File | Settings | File Templates. 016 */ 017public class ResponseHandler { 018 019 private static final Logger LOG = Logger.getLogger(ResponseHandler.class); 020 public String marshalObjectToXml(Object object){ 021 String response=null; 022 try{ 023 StringWriter writer = new StringWriter(); 024 JAXBContext context = JAXBContext.newInstance(object.getClass()); 025 Marshaller marshaller = context.createMarshaller(); 026 marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE); 027 marshaller.marshal(object, writer); 028 response = writer.toString(); 029 }catch (Exception e){ 030 e.printStackTrace(); 031 } 032 return response; 033 } 034 035 036 public String marshalObjectToJson(Object object){ 037 ObjectMapper objectMapper = new ObjectMapper(); 038 String jsonContent =""; 039 try { 040 jsonContent = objectMapper.writeValueAsString(object); 041 System.out.println(objectMapper.writeValueAsString(object)); 042 043 } catch (Exception e) { 044 e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates. 045 } 046 return jsonContent; 047 048 } 049 050}