【发布时间】: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 >= 400; } public int getPriority() { return 5000; }
标签: rest runtime-error resteasy quarkus rest-client