{"id":998,"date":"2009-08-20T10:55:28","date_gmt":"2009-08-20T14:55:28","guid":{"rendered":"http:\/\/adterrasperaspera.com\/blog\/?p=998"},"modified":"2013-11-12T14:33:55","modified_gmt":"2013-11-12T19:33:55","slug":"feeding-marshalled-jaxb-data-to-jersey","status":"publish","type":"post","link":"https:\/\/adterrasperaspera.com\/blog\/2009\/08\/20\/feeding-marshalled-jaxb-data-to-jersey\/","title":{"rendered":"Feeding marshalled JAXB data to Jersey"},"content":{"rendered":"<p>Although Jersey supports eating JAXB&#8217;ed classes fine, sometimes you want to manually alter the data, such as including a processing instruction for XSL stylesheets. There probably should be a less verbose way to do this.<\/p>\n<p>The object should be an <code>@XMLRootElement<\/code> annotated object.<\/p>\n<pre><code>@GET\r\n@Produces(\"application\/xml\")\r\npublic static StreamingOutput outputXMLwithXSL() {\r\n  return new StreamingOutput() {\r\n    public void write(OutputStream output) throws IOException,\r\n    WebApplicationException {\r\n      Object object = yourJAXBObject();\r\n\r\n      JAXBContext jc = null;\r\n      try { jc = JAXBContext.newInstance(object.getClass()); } \r\n      catch (JAXBException e) { e.printStackTrace(); }\r\n\r\n      Marshaller m = null;\r\n      try { m = jc.createMarshaller(); }\r\n      catch (JAXBException e) { e.printStackTrace(); }\r\n\r\n      PrintStream ps = new PrintStream(output);\r\n      ps.println(\"&lt;?xml version=\\\"1.0\\\" encoding=\\\"UTF-8\\\"?&gt;\");\r\n      ps.println(\"&lt;?xml-stylesheet type=\\\"text\/xsl\\\" href=\\\"your.xsl\\\"?&gt;\"); \r\n\r\n      try { m.setProperty(Marshaller.JAXB_FRAGMENT, true); }\r\n      catch (PropertyException e) { e.printStackTrace(); }\r\n\r\n      try { m.marshal(object, output); }\r\n      catch (JAXBException e) { e.printStackTrace(); }\r\n    }\r\n  };\r\n}\r\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Although Jersey supports eating JAXB&#8217;ed classes fine, sometimes you want to manually alter the data, such as including a processing instruction for XSL stylesheets. There probably should be a less verbose way to do this. The object should be an @XMLRootElement annotated object. @GET @Produces(&#8220;application\/xml&#8221;) public static StreamingOutput outputXMLwithXSL() { return new StreamingOutput() { public [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[405],"tags":[],"_links":{"self":[{"href":"https:\/\/adterrasperaspera.com\/blog\/wp-json\/wp\/v2\/posts\/998"}],"collection":[{"href":"https:\/\/adterrasperaspera.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/adterrasperaspera.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/adterrasperaspera.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/adterrasperaspera.com\/blog\/wp-json\/wp\/v2\/comments?post=998"}],"version-history":[{"count":17,"href":"https:\/\/adterrasperaspera.com\/blog\/wp-json\/wp\/v2\/posts\/998\/revisions"}],"predecessor-version":[{"id":2240,"href":"https:\/\/adterrasperaspera.com\/blog\/wp-json\/wp\/v2\/posts\/998\/revisions\/2240"}],"wp:attachment":[{"href":"https:\/\/adterrasperaspera.com\/blog\/wp-json\/wp\/v2\/media?parent=998"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/adterrasperaspera.com\/blog\/wp-json\/wp\/v2\/categories?post=998"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/adterrasperaspera.com\/blog\/wp-json\/wp\/v2\/tags?post=998"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}