使用json输入参数的Jersey 2.x post调用抛出MessageBodyProviderNotFoundException

时间:2016-07-05 11:10:25

标签: java json jersey jersey-2.0

我尝试直接映射到json。对于GET请求,这是顺利的。对于POST请求,会发生异常。

资源方法:

@GET
@Path("testget")
@Produces(MediaType.APPLICATION_JSON)
public Response test(@Context HttpServletRequest request) {
    Password password = new Password();
    password.setPassword("testpassword");
    return Response.status(Response.Status.OK).entity(password).build();
}

@POST
@Path("testpost")
@Consumes(MediaType.APPLICATION_JSON)
public Response test(Password password) {
    return Response.status(Response.Status.OK).entity(password.getPassword()).build();
}

客户端:

    Response reponse = target.request(MediaType.APPLICATION_JSON).get();

    Password password = new Password();
    password.setPassword("newTestPasswort");
    reponse = target.request(MediaType.APPLICATION_JSON).post(Entity.json(password));

异常:

    SEVERE: MessageBodyWriter not found for media type=application/json,        type=class com.entities.Password, genericType=class      com.entities.Password.
Exception in thread "main" org.glassfish.jersey.message.internal.MessageBodyProviderNotFoundException: MessageBodyWriter not found for media type=application/json, type=class com.entities.Password, genericType=class com.entities.Password.
    at org.glassfish.jersey.message.internal.WriterInterceptorExecutor$TerminalWriterInterceptor.aroundWriteTo(WriterInterceptorExecutor.java:247)
    at org.glassfish.jersey.message.internal.WriterInterceptorExecutor.proceed(WriterInterceptorExecutor.java:162)
    at org.glassfish.jersey.message.internal.MessageBodyFactory.writeTo(MessageBodyFactory.java:1130)
    at org.glassfish.jersey.client.ClientRequest.writeEntity(ClientRequest.java:502)
    at org.glassfish.jersey.client.internal.HttpUrlConnector._apply(HttpUrlConnector.java:388)
    at org.glassfish.jersey.client.internal.HttpUrlConnector.apply(HttpUrlConnector.java:285)
    at org.glassfish.jersey.client.ClientRuntime.invoke(ClientRuntime.java:255)
    at org.glassfish.jersey.client.JerseyInvocation$1.call(JerseyInvocation.java:684)
    at org.glassfish.jersey.client.JerseyInvocation$1.call(JerseyInvocation.java:681)
    at org.glassfish.jersey.internal.Errors.process(Errors.java:315)
    at org.glassfish.jersey.internal.Errors.process(Errors.java:297)
    at org.glassfish.jersey.internal.Errors.process(Errors.java:228)
    at org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:444)
    at org.glassfish.jersey.client.JerseyInvocation.invoke(JerseyInvocation.java:681)
    at org.glassfish.jersey.client.JerseyInvocation$Builder.method(JerseyInvocation.java:437)
    at org.glassfish.jersey.client.JerseyInvocation$Builder.post(JerseyInvocation.java:343)
    at com.clientsql.RegistrationTest.setNewPassword(RegistrationTest.java:61)
    at com.clientsql.RegistrationTest.main(RegistrationTest.java:44)

依赖关系:

<dependency>
        <groupId>org.glassfish.jersey.containers</groupId>
        <artifactId>jersey-container-servlet</artifactId>
        <version>2.23.1</version>
    </dependency>

对于映射我尝试了各种依赖项。对于每个只有get函数有效:

 <dependency>
        <groupId>com.owlike</groupId>
        <artifactId>genson</artifactId>
        <version>1.4</version>
 </dependency>

<!--        <dependency>
    <groupId>org.glassfish.jersey.media</groupId>
    <artifactId>jersey-media-json-jackson</artifactId>
    <version>2.23.1</version>
</dependency>-->
<!--        <dependency>
    <groupId>org.glassfish.jersey.media</groupId>
    <artifactId>jersey-media-moxy</artifactId>
    <version>2.23.1</version>
</dependency>-->

1 个答案:

答案 0 :(得分:0)

您需要告诉jersey如何将对象转换为JSON或从JSON转换。

在球衣中支持JSON有多种方法。

查看官方Jersey文档,了解支持JSON的不同方法:https://jersey.java.net/documentation/latest/media.html#d0e7963

以下两种常用方法:

  1. 基于JAXB的JSON绑定支持

    为POJO添加@XmlRootElement注释。

    @XmlRootElement
    class EntityX {
        // data
    }
    
  2. 基于POJO的JSON绑定支持

    您需要注册实现ContextResolver的提供程序。使用杰克逊你可以做到这一点。

    @Provider
    @Produces( { MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
    public class JacksonContextResolver implements ContextResolver<ObjectMapper> {
    
        private ObjectMapper objectMapper;
    
        public JacksonContextResolver() throws Exception {
            this.objectMapper = buildMapper(false);
        }
    
        public static ObjectMapper buildMapper(boolean indent) {
            ObjectMapper objectMapper = new ObjectMapper();
            objectMapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", Locale.ENGLISH));
            AnnotationIntrospector primary = new JacksonAnnotationIntrospector();
            AnnotationIntrospector secondary = new JaxbAnnotationIntrospector();
            AnnotationIntrospector pair = new AnnotationIntrospector.Pair(primary, secondary);
    
            SerializationConfig sc = objectMapper.getSerializationConfig()
                .withAnnotationIntrospector(pair)
                .withSerializationInclusion(JsonSerialize.Inclusion.NON_NULL);
            objectMapper.setSerializationConfig(sc);
    
            if (indent) {
                objectMapper.configure(SerializationConfig.Feature.INDENT_OUTPUT, true);
            }
    
            return objectMapper;
        }
    
        /**
         * @see javax.ws.rs.ext.ContextResolver#getContext(java.lang.Class)
         */
        public ObjectMapper getContext(Class<?> objectType) {
            return objectMapper;
        }
    }
    

    点击此链接了解详情:https://jersey.java.net/documentation/latest/media.html#json.jackson