I'm trying to make a property in a domain class hidden but it keeps appearing in the outputted JSON. I'm using Jackson 2.0 and Spring 3.1.1
Output of /users/1:
{"id":1,"password":null,"email":"[email protected]","firstName":"John","lastName":"Smith"}
My domain class:
@Entity
public class User {
private String mPassword;
...
@JsonIgnore
public String getPassword() {
return mPassword;
}
public void setPassword(String password) {
mPassword = password;
}
...
}
My springmvc config:
...
<mvc:annotation-driven/>
<bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
<property name="favorPathExtension" value="true" />
<property name="mediaTypes">
<map>
<entry key="json" value="application/json"/>
</map>
</property>
<property name="defaultContentType" value="application/json"/>
<property name="defaultViews">
<list>
<bean class="org.springframework.web.servlet.view.json.MappingJacksonJsonView" />
</list>
</property>
</bean>
...
And my controller:
@Controller
@RequestMapping("/users")
public class UserController {
private UserService mUserService;
public UserController(){}
@Inject
public void setUserController(UserService userService){
mUserService=userService;
}
@RequestMapping(value="/{id}", method=RequestMethod.GET)
public void getUser(@PathVariable("id") long id, Model model){
model.addAttribute(mUserService.getUser(id));
}
}
If you use Spring Boot, the jackson-databind dependency comes in the spring-boot-starter-json module (which also is included in other spring boot started moduled, like spring-boot-starter-web ).
Spring Framework and Spring Boot provide builtin support for Jackson based XML serialization/deserialization. As soon as you include the jackson-dataformat-xml dependency to your project, it is automatically used instead of JAXB2.
This annotation is used to specify a custom deserializer in order to unmarshall a JSON object. Using this annotation, we use a default value for deserializing an unknown enum value. Using this annotation, you can mark a property or a group of properties to be ignored. This is done at the class level.
The @JsonProperty annotation is used to map property names with JSON keys during serialization and deserialization. By default, if you try to serialize a POJO, the generated JSON will have keys mapped to the fields of the POJO.
Support for Jackson 2 has been added to Spring 3.1.2 (backported from Spring 3.2 - SPR-9507). Just upgrade your project from Spring 3.1.1 to Spring 3.1.2, and Jackson 2 works with the configuration you already have.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With