11

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.

Alexey Gavrilov
  • 10,593
  • 2
  • 38
  • 48
Pavel Varchenko
  • 727
  • 1
  • 11
  • 21
  • 1
    Did you find an answer to this? I'm having the same issue with Spring 4.1 RC2 and Jackson 2.3.2. When I use ObjectMapper directly, I can get the @JsonView annotations to work but not automatically via Spring. – Christopher Sep 08 '14 at 19:26

2 Answers2

2

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

Attila T
  • 577
  • 1
  • 4
  • 18
2

@Attila T's answer is sufficient but I have posted code for how to tweak you object mapper and use it everywhere

Controller code:

@Autowired
JSONMapper objectMapper;

@RequestMapping
public ResponseEntity<> getSchoolDetails(ParameterMapper mapper,HttpServletResponse           response) throws JsonGenerationException, JsonMappingException, IOException
{
     Order order =  orderRepository.findOne(292L);
    ObjectWriter w = objectMapper.writerWithView(SomeClass.class);
    objectWriter.writeValue(response.getWriter(),order);
  return new ResponseEntity<>(order,HttpStatus.OK);
}

Custom Object Mapper

@Component
public class JSONMapper extends ObjectMapper {


public JSONMapper() {

    Hibernate4Module hm = new Hibernate4Module();
    registerModule(hm);
    configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
    configure(SerializationFeature.INDENT_OUTPUT , false);
    configure(MapperFeature.DEFAULT_VIEW_INCLUSION, false);
    setSerializationInclusion(Include.NON_EMPTY);  
}
}

Dispatcher Configuration (change accordingly for xml based configuration)

   @Override
        public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {

            MappingJackson2HttpMessageConverter jsonConvertor = new MappingJackson2HttpMessageConverter(new JSONMapper());
            List<MediaType> jsonMediaTypes =new ArrayList<MediaType>();
            jsonMediaTypes.add(MediaType.APPLICATION_JSON);
            jsonConvertor.setSupportedMediaTypes(jsonMediaTypes);
            converters.add(jsonConvertor);    
            addDefaultHttpMessageConverters(converters);

       }
Pushpendra Jaiswal
  • 450
  • 1
  • 3
  • 15