我在Java中编写REST API并使用Groovy和Spock进行测试。Groovy - Ambigous处理程序方法
方法在我的控制器:
@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);
}
和我的斯波克测试:
@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
}
当我运行测试,我得到以下异常:
java.lang.IllegalStateException:为HTTP路径'http://localhost:35287/endpoint/SomeName1'映射的模糊处理程序方法:{public org.springframework.http.ResponseEntity ExampleController.findByName(java.lang.String),public org。 springframework.http.ResponseEntity ExampleController.findById(java.lang.Long)}
它只是不能决定调用哪个方法,因为这两个连接到相同的路径'/' – daggett
对不起,我编辑的问题 – user
没有。问题出现在@GetMapping(value =“/ {id}”)和'@GetMapping(value =“/ {name}”)'。这些网址没有区别。例如:'/ 111' - 映射应该如何识别要调用'/ {id}'还是'/ {name}'? – daggett