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