0

I'm faced with a scenario where the custom @RepositoryRestResource interface-method is involved by the wrong HTTP-Method. For example:

@RepositoryRestResource(path = "matches", collectionResourceRel = "matches")
public interface MatchRepo extends Neo4jRepository<Match, Long> {

    Collection<Match> findAllByCodeName(@Param("codeName") String codeName);

    @Transactional
    Long deleteAllByCodeName(@Param("codeName") String codeName);
}

Request:

curl  -i -X GET 'http://localhost:8003/spring-data/api/v1/matches/search/findAllByCodeName?codeName=Test-CodeName-1'

Note the above GET HTTP-Method. This is expected, & i'm happy with the Response:

HTTP/1.1 200 
Content-Type: application/hal+json;charset=UTF-8
Transfer-Encoding: chunked
Date: Tue, 20 Nov 2018 15:32:49 GMT

{
  "_embedded" : {
    "matches" : [ {
      "id" : "1",
      "codeName" : "Test-CodeName-1",
      "round" : 1,
      "me" : "ROCK",
      "pc" : "ROCK",
      "result" : "D",
      "timestamp" : "Nov 20, 2018, 05:32:27 AM",
      "lastUpdated" : "Nov 20, 2018, 05:32:27 AM",
      "created" : "Nov 20, 2018, 05:32:27 AM",
      "_links" : {
        "self" : {
          "href" : "http://localhost:8003/spring-data/api/v1/matches/22"
        },
        "match" : {
          "href" : "http://localhost:8003/spring-data/api/v1/matches/22"
        }
      }
    } ]
  },
  "_links" : {
    "self" : {
      "href" : "http://localhost:8003/spring-data/api/v1/matches/search/findAllByCodeName?codeName=Test-CodeName-1"
    }
  }
}%   

This is what appears on the Intelli-J Console-Mappings:

http://localhost:8003/spring-data/api/v1/{repository}/search

& I implemented the request as indicated in the mappings, as shown below. But the problem becomes evident when I am deleting a resource with a GET HTTP-Method as shown below:

Request:

curl -i -X GET 'http://localhost:8003/spring-data/api/v1/matches/search/deleteAllByCodeName?codeName=Test-CodeName-1'

Response:

HTTP/1.1 200 
Content-Type: application/hal+json;charset=UTF-8
Transfer-Encoding: chunked
Date: Tue, 20 Nov 2018 15:51:33 GMT

{
   "10": 
}

I need to find a way to make my custom deleteAllByCodeName(@Param) interface-method from the MatchRepo class to execute with the correct HTTP-Method. Must use DELETE HTTP-Method & not the GET HTTP-Method and adhere to the REST-API Design Principles.

S34N
  • 7,469
  • 6
  • 34
  • 43

1 Answers1

1

The manual notes that search resources only support GET requests.

https://docs.spring.io/spring-data/rest/docs/3.1.2.RELEASE/reference/html/#repository-resources.search-resource

You can prevent this repo method from being exported:

@RestResource(exported = false)
Long deleteAllByCodeName(@Param("codeName") String codeName);

and create a normal Spring MVC controller that handles the delete request.

Alan Hay
  • 22,665
  • 4
  • 56
  • 110
  • Thank you for your reply & pointing out the ref-doc. However, That's (exported = false) not what I want. Is there a cleaner way to achieve the above without explicitly writing out a '@RestController, & only stick to Spring Data Rest? In addition, I shall stick to my ugly hack (because it works) for now until when a better solution comes out in the future releases. – S34N Nov 20 '18 at 21:08
  • 1
    Search resources are for searching. There won't be a solution in a future release. Write a 2 line controller in about 30 seconds. – Alan Hay Nov 20 '18 at 21:14
  • I understand that but doesn't that defeat the purpose of using Spring Data Rest if I am to explicitly writing out a RestController? In which case, I would conform to a single standard, than to maintain double standards that could be potentially confusing to other developers maintaining the code. – S34N Nov 20 '18 at 21:40
  • Spring Data Rest offers an opinionated (convention over configuration) out-of-the-box solution for creating a REST api. But it also lets to customize your API when something does not fit in your box. So write your own custom controller, and document your code so that other developer won't be confused. Or better, do not use confusing things such as search resource for deletion. – Marc Tarin Nov 21 '18 at 09:45