Код: Выделить всё
@Validated
@ExecuteOn(TaskExecutors.BLOCKING)
public interface IHttpAction {
@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);
}
Код: Выделить всё
public class TestHttpClient {
@Client("/tag")
public interface TagHttpClient extends IHttpAction { }
}
Код: Выделить всё
@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")
открытый класс TagController реализует IHttpAction {
частный финальный IServiceAction iServiceAction;
Код: Выделить всё
public TagController(IServiceAction 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()));
}
@Override
public HttpResponse post(TagRequest request) {
var result = this.iServiceAction.post(request);
return result.match(()-> HttpResponse.ok(result.value), ()->HttpResponse.notFound(result.exception.getMessage()));
}
@Override
public HttpResponse put(@NotNull UUID id, TagRequest request) {
var result = this.iServiceAction.put(id, request);
return result.match(()-> HttpResponse.ok(result.value), ()->HttpResponse.notFound(result.exception.getMessage()));
}
@Override
public HttpResponse delete(@NotNull UUID id) {
var result = this.iServiceAction.delete(id);
return result.match(()-> HttpResponse.ok(result.value), ()->HttpResponse.notFound(result.exception.getMessage()));
}
Когда я запускаю тестовый проект, с помощью метода this.testHttpClient.get(UUID.randomUUID()); он должен прийти к методу контроллера get, однако вызов был к конечной точке поиска.
Журналы 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 --location 'http://localhost:8080/tag/0ea13638-b9d7-11ef-9cd2-0242ac120002'
Подробнее здесь: https://stackoverflow.com/questions/792 ... http-verbs