How to reuse Jersey's JSON/JAXB for serialization?

前端 未结 7 1320
失恋的感觉
失恋的感觉 2020-11-29 23:48

I have a JAX-RS REST service implemented using Jersey. One of the cool features of JAX-RS/Jersey is how easily a POJO can be turned into a REST service, simply by sprinkling

相关标签:
7条回答
  • 2020-11-30 00:50

    JAXB annotations work fine when serializing to XML. The main problem is that JAXB does not support empty arrays. So when serializing something like this...

    List myArray = new ArrayList();
    

    ...to json via jaxb anottations all your empty arrays become null instead of [].

    To solve this you can just serialize your pojos directly to json via jackson.

    Take a look at this from Jersey's user guide: http://jersey.java.net/nonav/documentation/latest/user-guide.html#d0e1959

    This is the best way to use Jackson provider without JAXB. Moreover, you can always use the latest version of jackson by downlaoding jackson-all-x.y.z-jar from its web.

    This method will not interfere with your jaxb annotations so I would suggest to have a try!

    0 讨论(0)
提交回复
热议问题