繁体   English   中英

如何使用Micronaut客户端注释映射errorType

[英]How to map errorType using Micronaut client annotation

如何使用Micronaut客户端注释来映射errorType,以编程方式提供成功或失败情况下,我们可以提供主体类型和errorType对象。

以编程方式调用客户端:

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中使用声明式客户端时进行声明,如下所示:

@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
}

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

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。 因此,如果基础客户端在发生异常的情况下提供了一个自定义错误对象作为响应主体,则可以使用此自定义错误类型来优雅地进行错误处理和日志记录。

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

根据Micronaut API文档,@ Client批注提供了errorType,它对我来说可以处理errorType响应。

https://docs.micronaut.io/latest/api/index.html https://docs.micronaut.io/latest/guide/index.html#clientAnnotation

@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=Object.class,//The type used to decode errors
        configuration=<? extends HttpClientConfiguration>//The http client configuration bean to use
        )
@Header(name="", value="")      
public interface TestClient {
    @Get("?id=123")
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
    Single<HttpResponse<User>> getUser();
   }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM