简体   繁体   English

Quarkus - Rest 客户端 - 来自响应 object 的实体是 null

[英]Quarkus - Rest Client - entity from response object is null

Basicaly, I have 2 services but only one of them has rest api.基本上,我有 2 项服务,但其中只有一项具有 rest api。 I need to send a request to this rest api from another service and now I'm using the quarkus RestClient, it kind of works, because he prints the console the correct response to the console, but when I try extract it from the object it is null.我需要从另一个服务向这个 rest api 发送请求,现在我正在使用 quarkus RestClient,它有点工作,因为他打印控制台对控制台的正确响应,但是当我尝试从 ZA669CFDE63191CBD5BEB6 提取它时是 null。 I don't understand this, I have everything right in pom's as a quarkus guide says.我不明白这一点,正如 quarkus 指南所说,我在 pom 中的一切都是正确的。 I've already tried using the HTTP Request manually using HttpURLConnection and it works perfectly.我已经尝试使用 HttpURLConnection 手动使用 HTTP 请求,并且效果很好。 And the RestClient kind of works too, because it prints to the console, but the object is still null. RestClient 也可以工作,因为它打印到控制台,但 object 仍然是 null。

console print控制台打印

Sample code:示例代码:

Service with rest api rest api 服务

TestResources测试资源

@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
@Path("test")
public interface TestResources {
    @GET
    @Path("/")
    Response getAll();
}

TestClientResources.java TestClientResources.java

@RegisterRestClient(configKey = "config.key")
@RegisterProvider(HttpLoggingFilter.class)
public interface TestClientResources extends TestResources {
}

TestService.java测试服务.java

@ApplicationScoped
public class TestService implements TestResources {
    @Override
    public Response getAll() {
         Object object;        
         (code)
         return Response.ok(obj).build(); // The obj never is null
    }
}

Service to call the api服务电话 api

@ApplicationScoped
public class Client {
    @Inject
    @RestClient
    TestClientResources testClient;

    void onStart(@Observes StartupEvent ev) {
         Response response = testClient.getAll(); // this return a ManualClosingApacheHttpClient
         Object obj = response.getEntity(); // this is null

         try {
             URL url = new URL("API");
             HttpURLConnection con = (HttpURLConnection) url.openConnection();
             con.setRequestMethod("GET");
             con.setRequestProperty("accept", "application/json");
             InputStream i = con.getInputStream();
             Object json = (new ObjectMapper()).readValue(i, Object.class); // this works, so the problem is not the api, i guess
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

}

response object响应 object

I have not checked the full implementation of the Quarkus rest client, but from what I see it may be related to bad typing of the returned object.我尚未检查 Quarkus rest 客户端的完整实现,但据我所知,这可能与返回的 object 的错误类型有关。

Documentation states:文档指出:

The client will handle all the networking and marshalling leaving our code clean of such technical details.客户将处理所有的网络和编组,让我们的代码清除这些技术细节。

I would try something like this:我会尝试这样的事情:

@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
@Path("test")
public interface TestResources {
    @GET
    @Path("/")
    ObjectExpected getAll();
}

Then:然后:

@ApplicationScoped
public class Client {
    @Inject
    @RestClient
    TestClientResources testClient;

    void onStart(@Observes StartupEvent ev) {
        ObjectExpected obj = testClient.getAll();
        ...
    }

Additionally, documentation also says:此外,文档还说:

When a JSON extension is installed such as quarkus-rest-client-jackson or quarkus-rest-client-jsonb , Quarkus will use the application/json media type by default for most return values, unless the media type is explicitly set via @Produces or @Consumes annotations (there are some exceptions for well known types, such as String and File, which default to text/plain and application/octet-stream respectively).当安装了 JSON 扩展(例如quarkus-rest-client-jacksonquarkus-rest-client-jsonb )时,Quarkus 将默认使用 application/json 媒体类型作为大多数返回值,除非通过 @Produces 显式设置媒体类型或@Consumes 注释(对于众所周知的类型有一些例外,例如 String 和 File,它们分别默认为 text/plain 和 application/octet-stream)。

As you seem to use a POJO, you could also clear this part from your rest client interface:由于您似乎使用 POJO,您也可以从 rest 客户端界面中清除此部分:

@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)

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

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