JsonIgnore不工作

时间:2013-08-09 19:35:18

标签: java

我正在努力使这些属性不被序列化,但出于某种原因,无论我做什么,他们仍然被序列化并发送到客户端。我尝试过只使用@JsonIgnoreProperties({"doBefore","doAfter","doAfterComplete"}),我尝试在getter上使用@JsonIgnore,我已经尝试了两者,但似乎没有任何区别。这是我班级的样子:

@JsonIgnoreProperties({"doBefore","doAfter","doAfterComplete"})
public class Action
{
    //for reordering actions with the default actions
    private String doBefore;
    private String doAfter;
    private String doAfterComplete;

    private String eventId;
    private List<ActionArgument> arguments;
    //action to perform when this action is done
    private List<Action> onCompleteActions;

    public Action() {}

    public Action(String eventId, List<ActionArgument> arguments, List<Action> onCompleteActions)
    {
        this.eventId = eventId;
        this.arguments = arguments;
        this.onCompleteActions = onCompleteActions;
    }

    public String getEventId()
    {
        return eventId;
    }
    public void setEventId(String eventId)
    {
        this.eventId = eventId;
    }
    public List<ActionArgument> getArguments()
    {
        return arguments;
    }
    public void setArguments(List<ActionArgument> arguments)
    {
        this.arguments = arguments;
    }
    public List<Action> getOnCompleteActions()
    {
        return onCompleteActions;
    }
    public void setOnCompleteActions(List<Action> onCompleteActions)
    {
        this.onCompleteActions = onCompleteActions;
    }

    @JsonIgnore public String getDoBefore()
    {
        return doBefore;
    }

    public void setDoBefore(String doBefore)
    {
        this.doBefore = doBefore;
    }

    @JsonIgnore public String getDoAfter()
    {
        return doAfter;
    }

    public void setDoAfter(String doAfter)
    {
        this.doAfter = doAfter;
    }

    @JsonIgnore public String getDoAfterComplete()
    {
        return doAfterComplete;
    }

    public void setDoAfterComplete(String doAfterComplete)
    {
        this.doAfterComplete = doAfterComplete;
    }
}

1 个答案:

答案 0 :(得分:1)

您可以在每个getter和setter上使用@JsonIgnore。我喜欢这个,因为似乎更容易看出哪些被忽略了。另外,您可以为序列化忽略属性,而不是忽略反序列化。 (这确实假设远程客户端没有使用相同的代码,不是。)

泽西/杰克逊对我有用。你使用什么jaxrs框架?

对于Jersey,您还需要web.xml文件中的一个部分:

<servlet>
    <servlet-name>MyJaxRSServlet</servlet-name>
    <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
    <init-param>
        <param-name>com.sun.jersey.config.property.packages</param-name>
        <param-value>com.mycompany.transformation</param-value>
    </init-param>
    <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>

这是您告诉它扫描包com.mycompany.transformation.**以获取注释的地方。