@Override public String prepareResponse(String className, Object o) throws ParseException { try { Class clazz = Class.forName(className); JAXBContext jaxbContext = JAXBContext.newInstance(clazz.getPackage().getName()); Marshaller marshaller = jaxbContext.createMarshaller(); marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); marshaller.marshal(clazz.cast(o), byteArrayOutputStream); return byteArrayOutputStream.toString(); } catch (JAXBException e) { throw new ParseException( "Error occured during the parsing of request's XML body. The details of the exception " + e.toString(), 0); } catch (ClassNotFoundException e) { throw new ParseException( "Error occured during the parsing of request's XML body. The details of the exception " + e.toString(), 0); } }
@Override public Object parseRequest(String className) throws ParseException { try { Class clazz = Class.forName(className); JAXBContext jaxbContext = JAXBContext.newInstance(clazz.getPackage().getName()); Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller(); Source source = new StreamSource(new ByteArrayInputStream(body.getBytes())); JAXBElement<Object> root = jaxbUnmarshaller.unmarshal(source, clazz); return clazz.cast(root.getValue()); } catch (JAXBException e) { throw new ParseException( "Error occured during the parsing of request's XML body. The details of the exception " + e.toString(), 0); } catch (ClassNotFoundException e) { throw new ParseException( "Error occured during the parsing of request's XML body. The details of the exception " + e.toString(), 0); } }