asgallant
asgallant

Reputation: 26340

How do I setup/configure Jackson annotations to work in Spring MVC?

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"}

Upvotes: 3

Views: 7943

Answers (4)

asgallant
asgallant

Reputation: 26340

I ended up upgrading to Spring 3.2.2, which, along with the Jackson 2.2 libraries, fixed the problem. All of the Jackson annotations are working properly now. I assume that this means there was something wrong with the 3.1.1 installation that messed the Jackson stuff up, but who knows?

Thanks to everyone who responded.

Upvotes: 1

gregwhitaker
gregwhitaker

Reputation: 13420

You are missing Jackson dependencies on your classpath. You need to add dependencies on the following jars:

  1. jackson-core
  2. jackson-databind
  3. jackson-annotations

Once you have those dependencies added you can prevent fields from being serialized by placing the @JsonIgnore annotation on the getter like so:

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;
    }
}

Upvotes: 6

Ayub Malik
Ayub Malik

Reputation: 2578

Try using @JsonIgnoreProperties instead of @JsonIgnore

Upvotes: 1

Ayub Malik
Ayub Malik

Reputation: 2578

Add Jackson JARs to your classpath. If you using Maven add Jackson2 as a dependency, or copy the required JARs to your classpath manually. Spring will do the rest.

Upvotes: 1

Related Questions