@JsonView与Spring PagedResources

时间:2018-07-25 14:23:18

标签: java spring spring-mvc jackson

我有一个用Rest Controller公开的pojo。我需要为一个GET请求隐藏一些属性,因此我决定使用jackson的批注@JsonView。我找不到任何通过@JsonView和PagedResources实现的方法。

这是我的pojo:

public class Pojo {

     interface RestrictedPojo {}
     interface AllPojo extends RestrictedPojo {}

     @Id
     @JsonView(RestrictedPojo.class)
     private String identifier;

     @JsonView(AllPojo.class)
     private String someproperty;

     /**
      * Property I want to hide
      */
     @JsonView(RestrictedPojo.class)
     private String someHiddenProperty;
}

这是我的控制器:

@RepositoryRestController
@RequestMapping(value = "/pojo")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class PojoController {

    private final PojoService pojoService;

    private final IdentityUtils identityUtils;

    private final PagedResourcesAssembler<Pojo> pagedResourcesAssembler;

    @PreAuthorize("hasRole('SOME_ROLE')")
    @GetMapping
    @JsonView(Pojo.RestrictedPojo.class)
    public ResponseEntity<PagedResources<Resource<Pojo>>> getAllRestrictedPojos(final Pageable pageable) {
        final Page<Pojo> allPojo = pojoService.getAllRestrictedPojos(pageable);
        final PagedResources<Resource<Pojo>> resources = pagedResourcesAssembler.toResource(allPojo );
        return ResponseEntity.ok(resources);
    }

    @PreAuthorize("hasRole('SOME_ROLE')")
    @GetMapping
    @JsonView(Pojo.AllPojo.class)
    public ResponseEntity<PagedResources<Resource<Pojo>>> getAllPojos(final Pageable pageable) {
        final Page<Pojo> allPojo = pojoService.getAllRestrictedPojos(pageable);
        final PagedResources<Resource<Pojo>> resources = pagedResourcesAssembler.toResource(allPojo );
        return ResponseEntity.ok(resources);
    }
}

我没有编写特定的配置,这是一个基本的Spring Boot应用程序。

任何人都可以帮忙吗?

谢谢

0 个答案:

没有答案