How do I use the Jersey JSON POJO support?

前端 未结 11 1854
离开以前
离开以前 2020-11-27 12:24

I have an object that I\'d like to serve in JSON as a RESTful resource. I have Jersey\'s JSON POJO support turned on like so (in web.xml):

  
         


        
11条回答
  •  感动是毒
    2020-11-27 12:58

    Jersey-json has a JAXB implementation. The reason you're getting that exception is because you don't have a Provider registered, or more specifically a MessageBodyWriter. You need to register a proper context within your provider:

    @Provider
    public class JAXBContextResolver implements ContextResolver {
        private final static String ENTITY_PACKAGE = "package.goes.here";
        private final static JAXBContext context;
        static {
            try {
                context = new JAXBContextAdapter(new JSONJAXBContext(JSONConfiguration.mapped().rootUnwrapping(false).build(), ENTITY_PACKAGE));
            } catch (final JAXBException ex) {
                throw new IllegalStateException("Could not resolve JAXBContext.", ex);
            }
        }
    
        public JAXBContext getContext(final Class type) {
            try {
                if (type.getPackage().getName().contains(ENTITY_PACKAGE)) {
                    return context;
                }
            } catch (final Exception ex) {
                // trap, just return null
            }
            return null;
        }
    
        public static final class JAXBContextAdapter extends JAXBContext {
            private final JAXBContext context;
    
            public JAXBContextAdapter(final JAXBContext context) {
                this.context = context;
            }
    
            @Override
            public Marshaller createMarshaller() {
                Marshaller marshaller = null;
                try {
                    marshaller = context.createMarshaller();
                    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
                } catch (final PropertyException pe) {
                    return marshaller;
                } catch (final JAXBException jbe) {
                    return null;
                }
                return marshaller;
            }
    
            @Override
            public Unmarshaller createUnmarshaller() throws JAXBException {
                final Unmarshaller unmarshaller = context.createUnmarshaller();
                unmarshaller.setEventHandler(new DefaultValidationEventHandler());
                return unmarshaller;
            }
    
            @Override
            public Validator createValidator() throws JAXBException {
                return context.createValidator();
            }
        }
    }
    

    This looks up for an @XmlRegistry within the provided package name, which is a package that contains @XmlRootElement annotated POJOs.

    @XmlRootElement
    public class Person {
    
        private String firstName;
    
        //getters and setters, etc.
    }
    

    then create an ObjectFactory in the same package:

    @XmlRegistry
    public class ObjectFactory {
       public Person createNewPerson() {
          return new Person();
       }
    }
    

    With the @Provider registered, Jersey should facilitate the marshalling for you in your resource:

    @GET
    @Consumes(MediaType.APPLICATION_JSON)
    public Response doWork(Person person) {
       // do work
       return Response.ok().build();
    }
    

提交回复
热议问题