View Javadoc
1   package org.kuali.asr.handler;
2   
3   import org.apache.log4j.Logger;
4   import org.codehaus.jackson.map.ObjectMapper;
5   
6   import javax.xml.bind.JAXBContext;
7   import javax.xml.bind.Marshaller;
8   import java.io.StringWriter;
9   
10  /**
11   * Created with IntelliJ IDEA.
12   * User: maheswarang
13   * Date: 12/9/13
14   * Time: 6:03 PM
15   * To change this template use File | Settings | File Templates.
16   */
17  public class ResponseHandler {
18  
19      private static final Logger LOG = Logger.getLogger(ResponseHandler.class);
20      public String  marshalObjectToXml(Object object){
21          String response=null;
22          try{
23              StringWriter writer = new StringWriter();
24              JAXBContext context = JAXBContext.newInstance(object.getClass());
25               Marshaller marshaller = context.createMarshaller();
26              marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
27              marshaller.marshal(object, writer);
28              response = writer.toString();
29          }catch (Exception e){
30              e.printStackTrace();
31          }
32          return response;
33      }
34  
35  
36    public String marshalObjectToJson(Object object){
37        ObjectMapper objectMapper = new ObjectMapper();
38        String jsonContent ="";
39        try {
40            jsonContent = objectMapper.writeValueAsString(object);
41            System.out.println(objectMapper.writeValueAsString(object));
42  
43        } catch (Exception e) {
44            e.printStackTrace();
45        }
46        return jsonContent;
47  
48    }
49  
50  }