如何使用 Micronaut 客户端注释映射 errorType [英] How to map errorType using Micronaut client annotation

查看:30
本文介绍了如何使用 Micronaut 客户端注释映射 errorType的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用 Micronaut 客户端注释映射 errorType,在编程的情况下,我们可以在成功和失败的情况下提供 body type 和 errorType 对象.

How to map errorType using Micronaut client annotation, In case of programatically we can provide body type and errorType objects in case of success and failure.

以编程方式调用客户端:

Programmatically calling client:

import io.micronaut.core.type.Argument;
import io.micronaut.http.HttpRequest;
import io.micronaut.http.HttpResponse;
import io.micronaut.http.MediaType;
import io.micronaut.http.client.DefaultHttpClient;
import io.micronaut.http.client.exceptions.HttpClientResponseException;
import io.micronaut.http.uri.UriBuilder;
import io.reactivex.Single;
import java.net.URL;

@Singleton
public class Test{
    public User getUser(String id) {
        try {
            String uriPath = UriBuilder.of("url")
                            .queryParam("id", id)
                            .toString();

        DefaultHttpClient httpClient = new DefaultHttpClient(new URL(""),httpClientConfiguration);

        Single<HttpResponse<User>> single = Single.fromPublisher(httpClient.exchange(
        HttpRequest.GET(uriPath).header(X_REQUEST_ID, REQUEST_ID).accept(MediaType.APPLICATION_JSON_TYPE),
        Argument.of(User.class), //bodyType
        Argument.of(Object.class) //errorType
        ));

        HttpResponse<User> response = single.blockingGet();
        User user = response.body();
        return user;            
        } catch (HttpClientResponseException | Exception e ) {              
        } 
    }
}

使用注解调用客户端

import io.micronaut.http.HttpResponse;
import io.micronaut.http.MediaType;
import io.micronaut.http.annotation.Consumes;
import io.micronaut.http.annotation.Get;
import io.micronaut.http.annotation.Header;
import io.micronaut.http.client.annotation.Client;
import io.reactivex.Single;
@Client(value = "url",
path = "/user")
public interface TestClient {
    @Get("?id=123")
    @Consumes(MediaType.APPLICATION_JSON)
        Single<HttpResponse<User>> getUser();
   }

推荐答案

如果你想定义自己的自定义对象为errorType,可以在micronaut中使用declarative client时声明如下:

If you want to define your own custom object as errorType, you can declare when using declarative client in micronaut as follows:

@Client(id="",//The ID of the client
            value = "url", //The URL or service ID of the remote service
            path = "/user",//The base URI for the client. Only to be used in conjunction with id().
            errorType=YourCustomObject.class,//The type used to decode errors
            configuration=<? extends HttpClientConfiguration>//The http client configuration bean to use
            )
public interface ExternalCallClient{
    //some API method
}

然后在您的连接器客户端类:

Then at your connector client class:

class Connect{

@Inject
private ExternalCallClient externalCallClient;

call(){

    try{
        //call to external API method using externalCallClient
      }catch(HttpClientResponseException e){

         Optional<YourCustomObject> error = e.getResponse()
                                             .getBody(YourCustomObject.class)
            }
        }
    }

Micronaut 客户端为 HTTP 代码(400 及以上 400(404 除外))抛出 HttpClientResponseException,以防底层客户端出现异常.因此,如果底层客户端在异常情况下提供自定义错误对象作为响应主体,则此自定义错误类型可用于优雅的错误处理和日志记录.

Micronaut client throw a HttpClientResponseException for HTTP code(400 and above 400(except 404)) in case of exception from underlying client. So if the underlying client provides a custom error object in case of exception as a response body, this custom error type can be used for gracefully error handling and logging.

类似的方法也可以用于 DefaultHttpClient.

Similar approach can be used for DefaultHttpClient too.

这篇关于如何使用 Micronaut 客户端注释映射 errorType的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆