I would advise against mixing JPA and HATEROAS Exposed Resources objects. Below is my typical setup:
Data Object:
@Entity public class MyEntity {
Repository:
public interface MyEntityRepository extends CrudRepository<MyEntity, Long> {
HATEOAS Resource:
public class MyResource {
Service implementation (interface not shown):
@Service public class MyServiceImpl implements MyService { @Autowired private Mapper mapper;
Finally, the controller that provides the resource:
@Controller @RequestMapping("/myresource") @ExposesResourceFor(MyResource.class) public class MyResourceController { @Autowired private MyResourceService service; @Autowired private EntityLinks entityLinks; @GetMapping(value = "/{id}") public HttpEntity<Resource<MyResource>> getMyResource(@PathVariable("id") Long id) { MyResource myResource = service.getMyResource(id); Resource<MyResource> resource = new Resource<>(MyResource.class); Link link = entityLinks.linkToSingleResource(MyResource.class, profileId); resource.add(link); return new ResponseEntity<>(resource, HttpStatus.OK); } }
The @ExposesResourceFor allows you to add logic to your controller to expose different resource objects.
source share