Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

@JsonView not filtering properties (Spring 4.1.0.RC2, Jackson 2.3.2)

I have an entity (using lombok) with some annotated @JsonView annotation.

@Entity
@Table(name = "`order`")
@Getter
@Setter
@ToString
@Description("Приказ")
public class Order extends Auditable {


    private static final long serialVersionUID = -1299630493411381582L;

    @JsonView(JsonViews.OrderAdvancedSearch.class)
    @ManyToOne
    private School school;

    @Column(length = 50)
    private String number;
}

There's a controller method annotated with @JsonView annotation.

@Secured(value = {"ROLE_AUTHENTICATED_USER"})
@RequestMapping(value = "/order", method = RequestMethod.GET, headers = {"Content-Type=application/json"})
@JsonView(JsonViews.OrderAdvancedSearch.class)
@ResponseBody
public ResponseEntity<Order> getOrder(HttpServletRequest request) throws IOException, DnevnikException, RestException {
    Order order = orderRepository.findOne(292L); // just for example
    return new ResponseEntity<>(order,HttpStatus.OK);
}

I've expected that input will contain only fields annotated with @JsonView. But I've full of fields.

I'm trying to debug spring and jackson sources. In com.fasterxml.jackson.databind.SerializationConfig I see that active view is my class JsonViews.OrderAdvancedSearch.class But in com.fasterxml.jackson.databind.ser.std.BeanSerializerBase variable filteredProps always has all properties of my entity.

like image 277
Pavel Varchenko Avatar asked Aug 29 '14 07:08

Pavel Varchenko


1 Answers

Try to tweak your Jackson object mapper:

// disable this feature so that attributes with no view definition
// do not get serialized / deserialized
mapper.disable(MapperFeature.DEFAULT_VIEW_INCLUSION);

Reference: Feature: JSON Views

like image 149
Attila T Avatar answered Oct 21 '22 13:10

Attila T