Spring Data Pagination在JSONView中没有返回任何结果 [英] Spring Data Pagination returns no results with JSONView

查看:433
本文介绍了Spring Data Pagination在JSONView中没有返回任何结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在REST控制器中使用Spring数据分页并返回Paged实体。我想在JSONViews的帮助下控制作为JSON返回的数据。

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.

当我返回单个对象时,我能够实现结果。但是当我返回Page时,我收到空白JSON作为回复。

I am able to achieve the result when I return a single object. But when I return Page, I am receiving blank JSON as response.

以下是我的方法签名。

@JsonView(TravelRequestView.MyRequests.class)
@RequestMapping("/travel/requests")
public Page<TravelRequest> getUserTravelRequests(
            @RequestParam("ps") int pageSize, @RequestParam("p") int page,
            @RequestParam(defaultValue = "", value = "q") String searchString)

当我删除@JsonView注释时,我能够收到响应。

I am able to receive response when I remove @JsonView annotation.

推荐答案

尝试下面的代码,

@Configuration
public class MyInterceptorConfig extends WebMvcConfigurerAdapter{

    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> 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未被应用

它将检索用jsonview(TravelRequestView.MyRequests.class)注释的实体的所有变量以及所有变量没有用jsonview注释。如果您不想要对象的某些属性,请使用不同的视图进行注释。

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.

这篇关于Spring Data Pagination在JSONView中没有返回任何结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆