Tatu,In context of DropWizard, how does one register a custom XML serializer (I assume an implementation of XmlAdapter) with Jersey. I have not been able to get the custom implementation I wrote be used. Specifically I'm trying to serialize generic Maps to XML. Standard POJO entities work fine.
On Sunday, June 3, 2012 7:30:50 PM UTC-7, Tatu Saloranta wrote:On Sat, Jun 2, 2012 at 11:40 PM, Arul Dhesiaseelan <ar...@acm.org> wrote:
> I believe you may want to add JAXB annotation @XmlRootElement to your model
> and perform negotiation using Accept headers.
Another alternative is to use Jackson's XML output module
(https://github.com/FasterXML/jackson-dataformat-xml) for
serialization, and get it registered as JAX-RS provider using
(https://github.com/FasterXML/jackson-jaxrs-xml-provider) for
DropWizard/Jersey to use it.
This does not require @XmlRootElement, and can use the same
annotations Jackson uses for JSON (and/or JAXB annotations via JAXB
module).
While JAXB is more powerful for producing XML (in areas of XML Schema
support and exotic XML features), Jackson XML module has more powerful
data binding features, same as standard JSON part has. So it may be
worth investigating.
-+ Tatu +-
--
You received this message because you are subscribed to the Google Groups "dropwizard-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to dropwizard-us...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
That's good info, thanks...I would prefer to use Jackson XML (using jackson-dataformat-xml, I assume) and a custom serializer. But every search I do for jackson custom XML serializer turns up JSON examples, mostly at the scalar level. I haven't yet found an example for root objects in XML.
Do you have any links handy, or just what class I should be extending/implementing (which I'm pretty sure I can figure out how to register with the mapper in DW)?
Greetings!Using XmlMapper does everything I want it to, as you describe below.
I have jackson-jaxrs-xml-provider:2.1.4 in my classpath (versioned to match the other Jackson dependencies in DW 1.6.2).As far I as I can tell, DW is still using the old JAXB stuff. I am not getting the same output via the Resource request as I am from the XML mapper directly.Is there something special I can do to confirm that I'm using JAXB vs Jackson?
import java.util.HashMap;import javax.xml.bind.annotation.XmlRootElement;import com.fasterxml.jackson.dataformat.xml.XmlMapper;
public class XmlTest {@XmlRootElement(name="AppConfig")public static class MyMap extends HashMap<String,Object> {public Object getValue() {return this.get("value");}public void setValue(Object value) {this.put("value", value);}}public static void main(String[] args) throws Exception {XmlMapper xmapper = new XmlMapper();MyMap root2 = new MyMap();root2.setValue("value4");root2.put("otherKey", "otherValue");MyMap part3 = new MyMap();part3.setValue("value5");part3.put("otherKey", "otherValue");root2.put("part3", part3);MyMap part4 = new MyMap();part4.setValue("value6");part4.put("otherKey", "otherValue");root2.put("part4", part4);System.out.println(xmapper.writeValueAsString(root2));}}
<MyMap xmlns="">....</MyMap>
Tatu,Thank you! That's exactly what I needed: MessageBodyWriter. I wrote an implementation that used XmlMapper under the covers and it seems to work!
Also important: Register the provider with Dropwizard, including ResourceTest (which I use a lot). I think the reason I was getting so confused is because I did not register the MessageBodyWriter as a provider for ResourceTest.
Now, I'm have one last question (with a specific example): Why isn't @XmlRootElement setting the root element name properly?
--
Excellent! I hope you don't mind my adding a link form Jackson XML module's readme page to this project. It sounds like great addition.
Another possibly interesting addition would be CSV module; and I think it makes sense to add convenience extension to properly configure these for use with DW, given that there are additional standard types that need to be supported.
-+ Tatu +-