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

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


Try to tweak your Jackson object mapper:

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

Reference: Feature: JSON Views


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

Controller code:

@AutowiredJSONMapper objectMapper;@RequestMappingpublic 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

@Componentpublic 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);       }