忽略jax-rs中传入的json元素 [英] Ignoring incoming json elements in jax-rs

查看:92
本文介绍了忽略jax-rs中传入的json元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道将@JsonIgnoreProperties(ignoreUnknown = true)放在Java REST API中的何处.我有以下课程:

I'm wondering where to put the @JsonIgnoreProperties(ignoreUnknown = true) in a Java REST API. I have the following class:

import org.codehaus.jackson.annotate.JsonIgnoreProperties;
import org.codehaus.jackson.map.ObjectMapper;

@JsonIgnoreProperties(ignoreUnknown = true)
public class QueuePayload {
    private String message;
    private String id;

    public String getId() {
        return this.id;
    }

    public String getMessage() {
        return this.message;
    }

    public void setId(String id) {
        this.id = id;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public String serialize() throws JsonProcessingException {
        ObjectMapper mapper = new ObjectMapper();
        return mapper.writeValueAsString(this);
    }
}

我在像这样的JAX-RS servlet中使用哪个:

Which I use in a JAX-RS servlet like this:

import javax.jms.JMSException;
import javax.naming.NamingException;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;



@Path("/v1")
@Produces(MediaType.APPLICATION_JSON)
public class ServiceApiV1 {

    @GET
    public Response getApiRoot() {
        String result = "{\"notice\" : \"It is alive!\"}";
        return Response.status(Status.OK).entity(result).build();
    }

    @POST
    @Path("/update")
    @Consumes(MediaType.APPLICATION_JSON)
    public Response update(UpdatePayload message) {
        return Response.status(Status.OK).entity(message).build();
    }
}

当我发布{ "message" : "Test", "id" : "one" }时,它就像一个符咒,但是当我发布{ "message" : "Test", "id" : "one", "sneaked" : "in" }时,我得到了:

When I post { "message" : "Test", "id" : "one" } it works like a charm, but when I post { "message" : "Test", "id" : "one", "sneaked" : "in" } I get:


SRVE0315E: An exception occurred:
com.ibm.ws.webcontainer.webapp.WebAppErrorReport:
   javax.servlet.ServletException:
     org.codehaus.jackson.map.exc.UnrecognizedPropertyException:
     Unrecognized field "sneaked";
     (Class test.QueuePayload), not marked as ignorable
 at [Source: com.ibm.ws.webcontainer.srt.SRTInputStream@b7328421; line: 1, column: 7] (through reference chain: test.QueuePayload["sneaked"])

尽管我@JsonIgnoreProperties(ignoreUnknown = true)正是为此用例设计的.我还尝试了在servlet中没有该属性和各种排列.我检查了这个问题,并确保在两个类中都导入了相同的API版本.

I though @JsonIgnoreProperties(ignoreUnknown = true) was designed exactly for this use case. I also tried without the property in the servlet and the various permutations. I checked this question and made sure to have imported the same API version in both classes.

我想念什么?

更新

将导入的分类更改为代码库(参见上文)并运行此测试):

Change the imported classed to codehouse (see above) and ran this test):

public static void main(String[] args) throws IOException {
    // TODO Auto-generated method stub
    QueuePayload qp = new QueuePayload();
    qp.setPayload("PayLoad");
    qp.setQueueId("ID-of-Q");
    qp.setStatus("Some Status");
    System.out.println(qp.serialize());

    ObjectMapper om = new ObjectMapper();
    QueuePayload qp2 = om.readValue("{\"payload\":\"PayLoad\",\"queueId\":\"ID-of-Q\",\"newstatus\":\"New Status\"}",
                QueuePayload.class);
    System.out.println(qp2.serialize());
}

那行得通. servlet没有

That worked. The servlet doesn't

推荐答案

问题似乎是服务器使用Jackson 1.x(codehaus).您正在尝试使用Jackson 2.x注释(fasterxml).他们不互动.您可以通过例外告诉您它是codehaus例外,这意味着实际使用了较旧的Jackson.

Problem seems to be the server uses Jackson 1.x (codehaus). You are trying to use Jackson 2.x annotations (fasterxml). They don't interact. You can tell by the exception that it's a codehaus exception, meaning the older Jackson is actual used.

您可以尝试在服务器中查找确切的版本,也可以尝试使用随机的1.x版本(当然,在提供的范围内,没有冲突),就像这样

You can look in the server an try and find the exact version, or you can just try and use an random 1.x version (of course in a provided scope, to there's no conflict), like this one

<dependency>
    <groupId>org.codehaus.jackson</groupId>
    <artifactId>jackson-core-asl</artifactId>
    <version>1.9.13</version>
    <scope>provided</scope>
</dependency>

然后在模型类(而不是资源类)上使用org.codehaus.jackson.annotate.JsonIgnoreProperties.

Then use org.codehaus.jackson.annotate.JsonIgnoreProperties on the model class (not the resource class).

这篇关于忽略jax-rs中传入的json元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆