I am trying to apply a projection on an entity class called Institute
.
I have the following projection class defined.
@Projection(name = "instituteProjection", types = { Institute.class })
public interface InstituteProjection {
String getOrganizationName();
Contact getContact();
Address getRegisteredAddress();
Address getMailingAddress();
}
I followed an answer by Oliver Gierke link and was able to return a collection resource with the projections when http://localhost:8080/institutes
is called. I did this by implementing the following method in the service layer and then calling it using a REST controller.
@Autowired
private ProjectionFactory projectionFactory;
@Autowired
InstituteTypeRepository instituteTypeRepo;
@Override
public PagedResources<Institute> getAllInstitutes(Pageable page) {
Page<?> instituteList = instituteRepo.findAll(page).
map(institute -> projectionFactory.createProjection(InstituteListProjection.class, institute));
PagedResources<Institute> instituteListPaged = pagedResourcesAssembler.toResource(instituteList);
return instituteListPaged;
}
Now how do I apply the same projection to an item resource when http://localhost:8080/institutes/1
is called?
UPDATE 1:
Controller method to get a single resource
@RequestMapping(value = "institutes/{instituteId}", method = RequestMethod.GET,
produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<?> getInstitute(@PathVariable Long instituteId) {
Institute institute = service.getInstitute(instituteId);
return new ResponseEntity<>(institute, HttpStatus.OK);
}
UPDATE 2:
Service layer method to get the single resource from the repository
@Override
public Institute getInstitute(Long instituteId) {
Institute institute = instituteRepo.findOne(instituteId);
return institute;
}