How to conditionally serialize with JAXB or Jackson? External View vs Internal View

后端 未结 2 1720
失恋的感觉
失恋的感觉 2021-01-19 07:05

I\'m building a RESTful API and I have a use case where I need to be able to render two different views of my data. One that we can use internally and one that we will expo

相关标签:
2条回答
  • 2021-01-19 07:17

    I was able to get this working by adding a new library to override the default:

            <!-- Used to Convert our objects to JSON and XML -->
            <dependency>
                <groupId>com.fasterxml.jackson.core</groupId>
                <artifactId>jackson-databind</artifactId>
                <version>2.0.6</version>
            </dependency>
            <dependency>
                <groupId>com.fasterxml.jackson.dataformat</groupId>
                <artifactId>jackson-dataformat-xml</artifactId>
                <version>2.0.5</version>
            </dependency>
            <dependency>
                <groupId>com.fasterxml</groupId>
                <artifactId>aalto-xml</artifactId>
                <version>0.9.8</version>
            </dependency>
    

    So now I can serialize to JSON and XML using Jackson and their @JsonView functionality. Very clean! The one I added was aalto-xml.

    0 讨论(0)
  • 2021-01-19 07:40

    Note: I'm the EclipseLink JAXB (MOXy) lead and a member of the JAXB (JSR-222) expert group.

    EclipseLink JAXB (MOXy) offers an external mapping file. This mapping file can augment or completely replace the metadata supplied via annotations. Below is an example where the same object model is mapped to two different weather services (Google and Yahoo)

    • http://blog.bdoughan.com/2011/09/mapping-objects-to-multiple-xml-schemas.html

    MOXy also supports both XML and JSON binding:

    • http://blog.bdoughan.com/2011/08/binding-to-json-xml-geocode-example.html

    MOXy also integrates easily with JAX-RS implementations:

    • http://blog.bdoughan.com/2012/05/moxy-as-your-jax-rs-json-provider.html
    0 讨论(0)
提交回复
热议问题