小编典典

如何使用Jersey JSON POJO支持?

json

我有一个对象,希望在JSON中作为RESTful资源使用。我已经启用了Jersey的JSON POJO支持(在web.xml中):

<servlet>  
    <servlet-name>Jersey Web Application</servlet-name>  
    <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
    <init-param>
        <param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
        <param-value>true</param-value>
    </init-param>

    <load-on-startup>1</load-on-startup>  
</servlet>

但是,当我尝试访问资源时,出现以下异常:

SEVERE: A message body writer for Java type, class com.example.MyDto, and MIME media type, application/json, was not found
SEVERE: Mapped exception to response: 500 (Internal Server Error)
javax.ws.rs.WebApplicationException
...

我要提供的类并不复杂,它只有一些公共final字段和一个设置所有这些的构造函数。字段是所有字符串,基元,与此类似的类或它们的列表(我尝试使用普通列表而不是通用List
,但无济于事)。有谁知道给什么?谢谢!

Java EE 6

泽西岛1.1.5

GlassFish 3.0.1


阅读 330

收藏
2020-07-27

共1个答案

小编典典

Jersey-
json具有JAXB实现。收到该异常的原因是因为您没有注册Provider,更具体地说是未注册MessageBodyWriter。您需要在提供商中注册适当的上下文:

@Provider
public class JAXBContextResolver implements ContextResolver<JAXBContext> {
    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();
        }
    }
}

这将@XmlRegistry在提供的程序包名称中查找,这是一个包含带@XmlRootElement注释的POJO 的程序包。

@XmlRootElement
public class Person {

    private String firstName;

    //getters and setters, etc.
}

然后在同一包中创建一个ObjectFactory:

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

通过@Provider注册,Jersey应该在您的资源中为您编组提供便利:

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