我有以下接口作为 GET、PUT、POST 和 DELETE 的常见 Http 操作
@Validated
@ExecuteOn(TaskExecutors.BLOCKING)
public interface IHttpAction<T, R> {
@Get(value = "/{?searchCriteria*}")
HttpResponse<?> find(@QueryValue T searchCriteria);
@Get(uri = "/{id}")
HttpResponse<?> get(@Nonnull UUID id);
HttpResponse<?> post(@Body T request);
@Put(uri = "/{id}")
HttpResponse<?> put(@NotNull UUID id, @Body T request);
HttpResponse<?> delete(@NotNull UUID id);
}
现在我有如下的 micronuat 声明式客户端
public class TestHttpClient {
@Client("/tag")
public interface TagHttpClient extends IHttpAction<TagRequest, TagResponse> { }
}
现在我正在编写测试用例如下
@MicronautTest
public class TagControllerTest {
private final TestHttpClient.TagHttpClient testHttpClient;
public TagControllerTest(TestHttpClient.TagHttpClient testHttpClient) {
this.testHttpClient = testHttpClient;
}
@Test
@DisplayName("Should create a tag with valid name")
void shouldCreateATagWithValidName() {
var result = this.testHttpClient.get(UUID.randomUUID());
Assertions.assertNotNull(result);
}
}
控制器
@Controller("/tag")
@Version("1")
public class TagController implements IHttpAction<TagRequest, TagResponse> {
private final IServiceAction<TagRequest, TagResponse> iServiceAction;
public TagController(IServiceAction<TagRequest, TagResponse> iServiceAction) {
this.iServiceAction = iServiceAction;
}
@Override
public HttpResponse<?> find(@QueryValue TagRequest searchCriteria) {
var result = this.iServiceAction.find(searchCriteria);
return result.match(()-> HttpResponse.ok(result.value), ()->HttpResponse.notFound(result.exception.getMessage()));
}
@Override
public HttpResponse<?> get(@Nonnull UUID id) {
var result = this.iServiceAction.get(id);
return result.match(()-> HttpResponse.ok(result.value), ()->HttpResponse.notFound(result.exception.getMessage()));
}
// Other methods
}
当我运行测试项目时,使用方法
this.testHttpClient.get(UUID.randomUUID());
它应该到达get控制器方法,但是,调用是到find端点。
来自 HTTP 客户端的日志
15:01:19.253 [Test worker] INFO i.m.c.DefaultApplicationContext$RuntimeConfiguredEnvironment - Established active environments: [test]
15:01:19.778 [default-nioEventLoopGroup-1-2] DEBUG i.m.h.client.netty.DefaultHttpClient - Sending HTTP GET to http://localhost:56407/tag/?arg0=4311ca00-345b-4393-a2b5-3ef43f9b14f3
下面的CURL
curl --location 'http://localhost:8080/tag/0ea13638-b9d7-11ef-9cd2-0242ac120002'
与邮递员完美配合
接口中的get方法是不是少了一个@PathVariable?