2

Currently we have exposed our methods like this

@RestController
@RequestMapping("/app/person")
public class PersonResource {

    @Timed
    public void delete(@PathVariable Long id) {
        log.debug("REST request to delete Person: {}", id);
        personRepository.delete(id);
    }
}

The operations of this method, in terms of input and output, are very clear to the user developer.

This article http://spring.io/guides/gs/accessing-data-rest/ shows how to expose JPARepositories directly obviating the need of a service layer.

@RepositoryRestResource(collectionResourceRel="people", path="people")
public interface PersonRepository extends JpaRepository<PersonEntity, Long> {

}

It is not obvious to me how I can make a "delete operation" available with PathVariable Long id.

There is an excellent article on this topic. https://github.com/spring-projects/spring-data-rest/wiki/Configuring-the-REST-URL-path But it actually shows how to supress export of a delete operation.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
user721025
  • 172
  • 5
  • 15

1 Answers1

3

As documented here, Spring Data REST will expose item resources for the repository you declare. Thus, all you need to do is discover the URI of the resource to delete and issue a DELETE request to it.

Oliver Drotbohm
  • 80,157
  • 18
  • 225
  • 211
  • Pardon my ignorance Oliver, how do you mean "you need to do is discover the URI of the resource to delete" ? – user721025 Jan 05 '15 at 16:21
  • He means you need to get the URL on the server for that object so you can delete it like this by sending a DELETE to it: DELETE http://localhost/app/persons/user721025 where user721025 is your user id / long. – Adam Jan 19 '17 at 11:38
  • Is it possible to send a message to client when an entity is deleted using RepositoryEventHandler? – Charlie May 10 '17 at 13:58