So I'm trying to use Spring Data JPA to make some rest services using the repository interfaces. But I'm stuck trying to do something without having to create a custom controller.
Suppose this service only accepts PUT and GET requests. The PUT requests are used to create and update the resource. So the ID is generated client side.
The entity and repository would be something like this:
@Entity
public class Document {
@Id
private String name;
private String text;
//getters and setters
}
@RepositoryRestResource(collectionResourceRel = "documents", path = "documents")
public interface DocumentRepository extends PagingAndSortingRepository<Document, String> {
}
When I try to make a PUT request @ localhost:8080/documents/foo with the following body:
{
"text": "Lorem Ipsum dolor sit amet"
}
I get this message:
{
"timestamp": 1474930016665,
"status": 500,
"error": "Internal Server Error",
"exception": "org.springframework.orm.jpa.JpaSystemException",
"message": "ids for this class must be manually assigned before calling save(): hello.Document; nested exception is org.hibernate.id.IdentifierGenerationException: ids for this class must be manually assigned before calling save(): hello.Document",
"path": "/documents/foo"
}
So I have to send in the body:
{
"name": "foo",
"text": "Lorem Ipsum dolor sit amet"
}
so it returns 201 Created with
{
"text": "Lorem Ipsum dolor sit amet",
"_links": {
"self": {
"href": "http://localhost:8080/documents/foo"
},
"document": {
"href": "http://localhost:8080/documents/foo"
}
}
}
Is it possible to make the PUT without having to send the id (the name field) inside json body? Since I'm already sending it in the URI?
I know that I can create a RestController and some requestmapping with a /documents/{document.name}, and use it to set the name field before saving it, but I wanted to know if there is any annotation or something.