3

I inherited a project in Spring MVC 3.1.1 and I need to use Jackson to serialize objects to JSON. I have an object class like:

public class User {
    Integer id;
    String name;

    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
}

and a controller like this:

@Controller
@RequestMapping(value =  "/home")
public class homeController {
    @RequestMapping(value = "/")
    public @ResponseBody User home() {
        User user;
        user = new User();

        user.setId(1);
        user.setName("Drew");

        return user;
    }
}

Navigate to /home and I get:

{"id":1,"name":"Drew"}

Great, that's the first step down. Now, if I want to ignore the "id" parameter, the Jackson documentation says I should use the @JsonIgnore annotation. The problem I am having is that NetBeans can't locate any of the annotation packages to import for Jackson, so I can't use the annotations. I tried downloading the Jackson 2.2 jars and adding those to my project (which then allows me to import the annotations), but the @JsonIgnore annotation has no effect when I do that.

I suspect that I'm missing either a jar file from Spring MVC or I need to configure something in the project's XML files, how would I go about finding out whether either (or neither) is the case? I'm relatively new to Java and I've never used Spring before, so if there's some additional information that would be helpful that I didn't know you would need, please ask and I will do my best to locate it. Thanks in advance for any assistance you can give!

edit:

To clarify, I have tried using annotations like this:

public class User {
    Integer id;
    String name;

    @JsonIgnore
    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    @JsonProperty("userName")
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
}

and like this:

@JsonIgnoreProperties({"id"})
public class User {
    Integer id;
    String name;

    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    @JsonProperty("userName")
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
}

and the returned JSON is always the same:

{"id":1,"name":"Drew"}
4

4 回答 4

5

您在类路径上缺少 Jackson 依赖项。您需要在以下 jar 上添加依赖项:

  1. 杰克逊核心
  2. 杰克逊数据绑定
  3. 杰克逊注解

添加这些依赖项后,您可以通过将@JsonIgnore注释放在 getter 上来防止字段被序列化,如下所示:

public class User {
    Integer id;
    String name;

    @JsonIgnore
    public Integer getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}
于 2013-07-20T14:30:40.630 回答
1

尝试使用 @JsonIgnoreProperties 而不是 @JsonIgnore

于 2013-07-20T12:23:01.107 回答
1

我最终升级到 Spring 3.2.2,它与 Jackson 2.2 库一起解决了这个问题。所有 Jackson 注释现在都可以正常工作。我认为这意味着 3.1.1 安装有问题导致杰克逊的东西搞砸了,但谁知道呢?

感谢大家的回应。

于 2013-07-22T21:18:53.817 回答
1

将 Jackson JAR 添加到您的类路径中。如果您使用 Maven 添加 Jackson2 作为依赖项,或者手动将所需的 JAR 复制到您的类路径中。春天会做剩下的。

于 2013-07-19T10:42:45.333 回答