Java @带Spring PagedResources的JsonView
我用Rest控制器暴露了一个pojo。我需要为一个GET请求隐藏一些属性,所以我决定使用jackson的注释@JsonView。我找不到使用@JsonView和PagedResources的任何方法 这是我的pojo:Java @带Spring PagedResources的JsonView,java,spring,spring-mvc,jackson,Java,Spring,Spring Mvc,Jackson,我用Rest控制器暴露了一个pojo。我需要为一个GET请求隐藏一些属性,所以我决定使用jackson的注释@JsonView。我找不到使用@JsonView和PagedResources的任何方法 这是我的pojo: public class Pojo { interface RestrictedPojo {} interface AllPojo extends RestrictedPojo {} @Id @JsonView(RestrictedPo
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);
}
}
@RepositoryRestController
@请求映射(value=“/pojo”)
@RequiredArgsConstructor(onConstructor=@_u(@Autowired))
公共类控制器{
专用最终PojoService PojoService;
私人最终身份直到身份;
专用最终页面数据源汇编页面数据源汇编;
@预授权(“hasRole('SOME_ROLE'))
@GetMapping
@JsonView(Pojo.RestrictedPojo.class)
公共响应全部受限POJO(最终可分页){
最终页面allPojo=pojoService.getAllRestrictedPojos(可分页);
final PagedResources resources=pagedResourcesAssembler.toResource(allPojo);
返回ResponseEntity.ok(资源);
}
@预授权(“hasRole('SOME_ROLE'))
@GetMapping
@JsonView(Pojo.AllPojo.class)
公共响应getAllPojos(最终可分页){
最终页面allPojo=pojoService.getAllRestrictedPojos(可分页);
final PagedResources resources=pagedResourcesAssembler.toResource(allPojo);
返回ResponseEntity.ok(资源);
}
}
我没有编写特定的配置,它是一个基本的spring启动应用程序
有人能帮忙吗
谢谢当您将
@JsonView(RestrictedPojo.class)
放在字段上时,您将它放在了json中。如果你想隐藏这个字段,你不需要给他加注释。也许不清楚。我需要实施此处解释的解决方案: