Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jackson annotations being ignored in Spring

Tags:

spring

jackson

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));
    }
}
like image 605
Mark Horgan Avatar asked Apr 10 '12 07:04

Mark Horgan


People also ask

Is Jackson Databind included in spring boot?

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 ).

Does spring use Jackson by default?

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.

What is the use of Jackson annotations?

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.

What is JsonProperty annotation in spring boot?

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.


1 Answers

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.

like image 104
Aleksander Blomskøld Avatar answered Sep 18 '22 15:09

Aleksander Blomskøld