Spring Data Rest:@RepositoryRestController和projection

时间:2015-12-07 15:58:18

标签: java spring spring-data-rest spring-hateoas

假设我有一个非常简单的模型:

@Entity
public class Person {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private long id;

    private String name;

    private String email;

    // ...getters and setters...
}

及其存储库

@RepositoryRestResource(path = "persons")
public interface PersonRepository extends PagingAndSortingRepository<Person, Long>{
}

我需要对返回的资源进行一些自定义过滤/按摩,因此我需要@RepositoryRestController,如下所示:

@RepositoryRestController
@ExposesResourceFor(Person.class)
@SuppressWarnings("rawtypes")
public class PersonsController {

    private PersonRepository personRepository;

    private PagedResourcesAssembler pagedResourcesAssembler;

    @Autowired
    public PersonsController(PersonRepository personRepository, PagedResourcesAssembler pagedResourcesAssembler) {
        super();
        this.personRepository = personRepository;
        this.pagedResourcesAssembler = pagedResourcesAssembler;
    }

    @RequestMapping(method = RequestMethod.GET, value = "/persons/{id}/neighbors")
    @ResponseBody
    public Resources<?> getNeighbors(@PathVariable(value = "id") long id, Pageable pageable, PersistentEntityResourceAssembler resourceAssembler) {

        Page<Person> persons = personRepository.findAll(pageable);

        // ...do things here to filter, log, check permissions etc...

        return pagedResourcesAssembler.toResource(persons, resourceAssembler); 
    }
}

问题:如何设置必须用于导出Projection资源的Person?我需要隐藏(仅在这种情况下)特定字段。有没有办法或者我必须为了这个非常特殊的目的而回归Jackson自定义序列化器?

谢谢,卢卡

0 个答案:

没有答案