HttpClientResponseException:空主体

时间:2019-08-09 08:43:17

标签: micronaut

我对使用declarative http client感到困惑。

我正在尝试创建一个declarative http client。但是我遇到了这个错误。

15:34:16.455 [pool-1-thread-1] ERROR i.m.r.intercept.RecoveryInterceptor - Type [micronaut.http.client.repository.ItunesClient$Intercepted] executed with error: Empty body
io.micronaut.http.client.exceptions.HttpClientResponseException: Empty body
        at io.micronaut.http.client.BlockingHttpClient.lambda$retrieve$0(BlockingHttpClient.java:140)
        at java.util.Optional.orElseThrow(Optional.java:290)
        at io.micronaut.http.client.BlockingHttpClient.retrieve(BlockingHttpClient.java:140)
        at io.micronaut.http.client.interceptor.HttpClientIntroductionAdvice.intercept(HttpClientIntroductionAdvice.java:535)
        at io.micronaut.aop.MethodInterceptor.intercept(MethodInterceptor.java:40)
        at io.micronaut.aop.chain.InterceptorChain.proceed(InterceptorChain.java:146)
        at io.micronaut.retry.intercept.RecoveryInterceptor.intercept(RecoveryInterceptor.java:73)
        at io.micronaut.aop.MethodInterceptor.intercept(MethodInterceptor.java:40)
        at io.micronaut.aop.chain.InterceptorChain.proceed(InterceptorChain.java:146)
        at micronaut.http.client.repository.ItunesClient$Intercepted.search(Unknown Source)
        at micronaut.http.client.controller.MusicController.search(MusicController.java:37)
        at micronaut.http.client.controller.$MusicControllerDefinition$$exec1.invokeInternal(Unknown Source)
        at io.micronaut.context.AbstractExecutableMethod.invoke(AbstractExecutableMethod.java:144)
        at io.micronaut.context.DefaultBeanContext$BeanExecutionHandle.invoke(DefaultBeanContext.java:2694)
        at io.micronaut.web.router.AbstractRouteMatch.execute(AbstractRouteMatch.java:295)
        at io.micronaut.web.router.RouteMatch.execute(RouteMatch.java:122)
        at io.micronaut.http.server.netty.RoutingInBoundHandler.lambda$buildResultEmitter$17(RoutingInBoundHandler.java:1358)

这是我的代码。 https://github.com/akyong/micronaut-http-client/blob/master/src/main/java/micronaut/http/client/controller/MusicController.java#L25-L37

如果我像这样将List<Album>更改为String,就可以使用。

public String search(String searchTerm, @Nullable @QueryValue String maxResults) {
        log.info("searchTerm : {}",searchTerm);
        log.info("maxResults : {}",maxResults);
        final int limit;
        if(StringUtils.isDigits(maxResults)) {
            limit = Math.min(25, Integer.parseInt(maxResults));
        } else {
            limit = 25;
        }

        String result = itunesClient.search(searchTerm, limit);
        return result;
    }

ItunesClient

@Get("/search?limit={maxResults}&media=music&entity=album&term={term}")
    String search(String term, int maxResults);//From SearchResults to String

1 个答案:

答案 0 :(得分:1)

src/main/resources/application.yml中设置此...

micronaut:
  codec:
    json:
      additionalTypes:
        - text/javascript

这是必需的,因为响应内容类型是从Apple返回的。有关示例,请参见https://github.com/jeffbrown/music-search/blob/30cda075cb8e9450ff920349ca5d5e986db32fbf/music-service/src/main/resources/application.yml#L13-L16