Ich schreibe REST API in Java und testen Sie mit Groovy und Spock.Groovy - Ambigous Handler Methoden
Methods in meinem Controller:
@GetMapping(value = "/{id}")
public ResponseEntity<ExampleObj> findById(@PathVariable Long id) {
final ExampleObj dto = service.findById(id);
if (dto != null) {
return new ResponseEntity<ExampleObj>(dto, HttpStatus.OK);
}
return new ResponseEntity<ExampleObj>(dto, HttpStatus.NOT_FOUND);
}
@GetMapping(value = "/{name}")
public ResponseEntity<ExampleObj> findByName(@PathVariable String name) {
final ExampleObj dto = service.findByName(name);
if (dto != null) {
return new ResponseEntity<ExampleObj>(dto, HttpStatus.OK);
}
return new ResponseEntity<ExampleObj>(dto, HttpStatus.NOT_FOUND);
}
Und mein Test in Spock:
@Unroll
'findByName test'() {
when:
def response = restTemplate.getForEntity(url, ExampleObj)
then:
response.getStatusCode() == statusCode
where:
url | statusCode
'/endpoint/SomeName1' | HttpStatus.OK
'/endpoint/NotExistingName' | HttpStatus.NOT_FOUND
}
@Unroll
'findById test'() {
when:
def response = restTemplate.getForEntity(url, ExampleObj)
then:
response.getStatusCode() == statusCode
where:
url | statusCode
'/endpoint/1' | HttpStatus.OK
'/endpoint/2' | HttpStatus.NOT_FOUND
}
Und wenn ich laufe Test ich die folgende Ausnahme erhalten:
java.lang.IllegalStateException: Für den HTTP-Pfad 'http://localhost:35287/endpoint/SomeName1' wurden mehrdeutige Handlermethoden zugeordnet: {public org.springframework.http.ResponseEntity ExampleController.findByName (java.lang.String), öffentliche Organisation. springframework.http.ResponseEntity ExampleController.findById (java.lang.Long)}
es kann einfach nicht entscheiden, welche Methode, da beide auf den gleichen Pfad verknüpft nennen '/' – daggett
leid, dass ich eine Frage bearbeitet – user
Nr. Das Problem ist in '@GetMapping (value ="/{id} ")' und '@GetMapping (value ="/{name} ")'. Es gibt keinen Unterschied in diesen URLs. zum Beispiel: '/ 111' - wie sollte das Mapping identifizieren, was'/{id} 'oder'/{name} 'heißt? – daggett