Unwrap a element in Jackson/Jaxb

后端 未结 3 1083
轻奢々
轻奢々 2020-12-19 16:43

I am using Jersey+Jackon to make a REST API which works with JSON.

Assume that I have a class as follows:

@XmlRootElement
public class A {
    public         


        
3条回答
  •  太阳男子
    2020-12-19 17:20

    Your requirement seems to be to drop the root element from json string. This can be configured in Jersey as follows. In Jersey, whether dropping root element is configured by JSONConfiguration.rootUnwrapping(). More details can be found in JSON support in Jersey and CXF.

    Here's a sample code that does this.

       @Provider
       public class MyJAXBContextResolver implements ContextResolver {
    
           private JAXBContext context;
           private Class[] types = {StatusInfoBean.class, JobInfoBean.class};
    
           public MyJAXBContextResolver() throws Exception {
               this.context = new JSONJAXBContext(
                       JSONConfiguration.mapped()
                                          .rootUnwrapping(true)
                                          .arrays("jobs")
                                          .nonStrings("pages", "tonerRemaining")
                                          .build(),
                       types);
           }
    
           public JAXBContext getContext(Class objectType) {
               return (types[0].equals(objectType)) ? context : null;
           }
       }
    

提交回复
热议问题