Spring Data Pagination returns no results with JSONView

后端 未结 6 1635
广开言路
广开言路 2021-02-06 02:53

I am using Spring data pagination in my REST Controller and returning Paged entity. I would like to control the data returned as JSON with the help of JSONViews.

I am a

6条回答
  •  栀梦
    栀梦 (楼主)
    2021-02-06 03:26

    Try with below piece of code,

    @Configuration
    public class MyInterceptorConfig extends WebMvcConfigurerAdapter{
    
        @Override
        public void configureMessageConverters(List> converters) {
          MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
          ObjectMapper mapper = new ObjectMapper() {
            private static final long serialVersionUID = 1L;
            @Override
            protected DefaultSerializerProvider _serializerProvider(SerializationConfig config) {
              // replace the configuration with my modified configuration.
              // calling "withView" should keep previous config and just add my changes.
              return super._serializerProvider(config.withView(TravelRequestView.MyRequests.class));
            }        
          };
          mapper.configure(MapperFeature.DEFAULT_VIEW_INCLUSION, true);
          converter.setObjectMapper(mapper);
          converters.add(converter);
        }
    

    Although I don't want to take credit for this, It was a reference from

    Jackson JsonView not being applied

    It would retrieve all the variables of an entity which are annotated with jsonview (TravelRequestView.MyRequests.class) along with all the variables which are not annotated with jsonview. If you don't want certain properties of an object, annotated with different view.

提交回复
热议问题