我正在尝试将 Micronaut-servlet API 用于现有的微服务。如果没有 servlet API,以下工作非常好
@Test
public void
saved_movies_are_returned_in_my_list() {
try (RxHttpClient client = embeddedServer.getApplicationContext()
.createBean(RxHttpClient.class, embeddedServer.getURL())) {
Movie movie = createMovie();
Response<Movie> response = client.toBlocking().retrieve(
HttpRequest.POST("/movies", movie).bearerAuth(idToken),
Argument.of(Response.class, Movie.class));
Response<List<Movie>> myMovies = client.toBlocking().retrieve(
HttpRequest.GET("/movies").bearerAuth(idToken),
Argument.of(Response.class, Argument.listOf(Movie.class)));
assertThat(myMovies.data).contains(movie);
}
}
这是控制器
@Post("/")
public Response<Movie> save(Movie movie) {
return movieService.save(movie);
}
并且该服务具有逻辑,如果电影<->用户映射已经存在,则不保存任何数据并返回现有映射
public Response<Movie> save(Movie movie) {
Optional<Movie> byUserIdAndTmdbId = movieRepository
.findByUserIdAndTmdbId(movie.getUserId(), movie.getTmdbId());
Response<Movie> movieResponse = null;
if (!byUserIdAndTmdbId.isPresent()) {
movieResponse = new Response<>("Movie Saved Successfully",
State.SUCCESS, movieRepository.save(movie));
return movieResponse;
} else {
return new Response<>("Movie already in list",
State.SUCCESS, byUserIdAndTmdbId.get());
}
}
存储库也很简单
@Repository
public interface MovieRepository extends CrudRepository<Movie,Long> {
List<Movie> findByUserId(String userId);
Optional<Movie> getByUserIdAndTmdbId(String userId, Long timdbId);
}
在我删除之前,所有这些都可以正常工作
implementation "io.micronaut:micronaut-http-server-netty"
并将其替换为
implementation("io.micronaut.servlet:micronaut-http-server-jetty:1.0.0.M1")
更改后出现以下异常
Failed to convert argument [Movie] for value [io.micronaut.core.convert.value.ConvertibleValuesMap@17bae1ca] due to: Cannot convert an iterable with more than 1 value to a non collection object
io.micronaut.http.client.exceptions.HttpClientResponseException: Failed to convert argument [Movie] for value [io.micronaut.core.convert.value.ConvertibleValuesMap@17bae1ca] due to: Cannot convert an iterable with more than 1 value to a non collection object
at io.micronaut.http.client.DefaultHttpClient$10.channelRead0(DefaultHttpClient.java:1831)
如果我尝试保留这两个依赖项,那么我会得到 HTTPClient 注入的重复 bean 异常!