【问题标题】:RestClient Error javax.ws.rs.WebApplicationException: Unknown error, status code 404RestClient 错误 javax.ws.rs.WebApplicationException:未知错误,状态码 404
【发布时间】:2022-01-28 20:06:52
【问题描述】:

我正在尝试从 Quarkus 的 api 向 Jersey 的 api 请求,但 Jersey API 返回 404 错误:

javax.ws.rs.WebApplicationException:未知错误,状态码 404

看来 quarkus rest 客户端无法识别或无法解析 payload json。

你已经得到类似的东西了吗?

有效载荷应该是这样的:

{
    "code": 404,
    "description": "some description....",
    "label": "API_ERROR_OBJECT_NOT_FOUND",
    "message": "Requested Object not found"
}

代码:

@ApplicationScoped
public class MachineService {

    @Inject
    @RestClient
    ICoreSummaryRest iCoreSummaryRest;

    public Boolean transferDatacollector(ObjectNode transferDatacollector) {
        try {
            String resp = iCoreSummaryRest.updateDataCollectosTransfer
                (transferDatacollector.toString());
            return Boolean.valueOf(resp);
        }catch (Exception e){
            return null;
        }
    }

界面

import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import org.eclipse.microprofile.rest.client.annotation.RegisterClientHeaders;
import org.eclipse.microprofile.rest.client.annotation.RegisterProvider;
import org.eclipse.microprofile.rest.client.inject.RegisterRestClient;

import javax.ws.rs.GET;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;


@Path("/")
@RegisterRestClient(configKey="country-api")
@RegisterClientHeaders(CustomHeadersRest.class)
public interface ICoreSummaryRest {

@PUT
    @Produces(MediaType.APPLICATION_JSON)
    @Path("datacollectors/transfer/")
    public String updateDataCollectosTransfer(String transferDatacollectorJSON);

}

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
                      http://maven.apache.org/xsd/maven-4.0.0.xsd">

    <modelVersion>4.0.0</modelVersion>

    <!-- The Basics -->
    <parent>
        <groupId>com.mycompany</groupId>
        <artifactId>my-project</artifactId>
        <version>1.0.0</version>
        <relativePath>../../pom.xml</relativePath>
    </parent>

    <artifactId>my-project-resource</artifactId>
    <version>1.0.0</version>

    <dependencies>
        <!-- Dependências Gerais Quarkus BOM -->
        <dependency>
            <groupId>io.quarkus</groupId>
            <artifactId>quarkus-resteasy-reactive-jackson</artifactId>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>io.quarkus</groupId>
            <artifactId>quarkus-resteasy-jsonb</artifactId>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.jboss.spec.javax.ws.rs</groupId>
            <artifactId>jboss-jaxrs-api_2.1_spec</artifactId>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>io.quarkus</groupId>
            <artifactId>quarkus-smallrye-openapi</artifactId>
            <scope>provided</scope>
        </dependency>
        <!--  <dependency>
              <groupId>io.quarkus</groupId>
              <artifactId>quarkus-smallrye-graphql</artifactId>
          </dependency> -->
          <dependency>
              <groupId>org.eclipse.microprofile.rest.client</groupId>
              <artifactId>microprofile-rest-client-api</artifactId>
          </dependency>
          <!-- Dependências do Projeto -->
        <dependency>
            <groupId>com.mycompany</groupId>
            <artifactId>my-project-service</artifactId>
            <scope>provided</scope>
        </dependency>
        <!-- Dependência JTS -->
        <dependency>
            <groupId>org.locationtech.jts</groupId>
            <artifactId>jts-core</artifactId>
        </dependency>
        <!-- Dependência mycompany -->
        <dependency>
            <groupId>com.google.guava</groupId>
            <artifactId>guava</artifactId>
        </dependency>
    </dependencies>

    <!-- Build Settings -->
    <build>
        <plugins>
            <plugin>
                <groupId>org.jboss.jandex</groupId>
                <artifactId>jandex-maven-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

【问题讨论】:

  • 嗨!!!我使用 ResponseExceptionMapper 修复它以提取内容并将其解析为字符串
  • public class DefaultMicroprofileRestClientExceptionMapper implements ResponseExceptionMapper { Response resp = null; String responseAsString = null;
  • public Throwable toThrowable(Response response) { try { response.getEntity(); ByteArrayInputStream in = (ByteArrayInputStream) response.getEntity(); int n = in.available(); byte[] bytes = new byte[n]; in.read(bytes, 0, n); responseAsString = new String(bytes, StandardCharsets.UTF_8); // Or any encoding. } catch (Exception var3) { System.out.println(var3); } return new RestClientException(responseAsString, response); }
  • public boolean handles(int status, MultivaluedMap headers) { return status &gt;= 400; } public int getPriority() { return 5000; }

标签: rest runtime-error resteasy quarkus rest-client


【解决方案1】:

如果你想阅读正文,你可以捕捉到这样的错误:

try {
  String resourceResponse = errorRestClient.getResource();
} catch (WebApplicationException e) {
  String respStr = e.getResponse().readEntity(String.class);
  LOGGER.error(respStr);
}

这将为http://mockbin.org/bin/3144eda0-9fa7-4893-90ee-5d624d51bcd2 的以下 mockbin 打印此内容

2022-01-30 17:59:45,851 错误 [org.acm.arc.ErrorHTTPrestAPI] (vert.x-eventloop-thread-11){“描述”:“一些 描述....”, "标签": "API_ERROR_OBJECT_NOT_FOUND", “消息”:“未找到请求的对象”}

我的界面是这样的

@ApplicationScoped
@Path("/")
@RegisterRestClient(baseUri="http://mockbin.org/bin/3144eda0-9fa7-4893-90ee-5d624d51bcd2")
public interface ErrorRestClient {

    @GET
    @Path("")
    String getResource();

}

【讨论】:

    【解决方案2】:

    如果我理解正确这个issue, 当 RestClient 收到一个不同于 2xx 的响应时,它会自动抛出这个异常。

    也许您可以禁用 ResponseExceptionMapper 或创建一个拦截器来处理此异常。

    下面我只是引用这个issue的答案

    这个异常是由默认的rest客户端ResponseExceptionMapper抛出的。

    您应该能够通过将以下属性添加到 application.properties 来禁用它:

    microprofile.rest.client.disable.default.mapper=false
    

    请参阅 MicroProfile Rest Client 规范的 Default ResponxeExceptionMapper 和 ResponseExceptionMapper 部分。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-08-29
      • 1970-01-01
      • 2020-09-18
      • 2019-10-12
      • 2021-10-13
      • 2012-10-13
      • 2021-06-19
      • 2018-08-24
      相关资源
      最近更新 更多