我正在尝试从 quarkus 控制器连接到第三方 API。我有一个使用服务方法的控制器。 try catch 块不起作用。我拥有所有必需的依赖项,并且遵循 quarkus 文档 这是代码
控制器
package com.ncr.invoice;
// all imports over here
@Path("/api")
@RequestScoped
public class InvoiceController {
// all variable and injection
@POST
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
@Path("/invoice")
@Timeout(250)
@PermitAll
public Response getInvoice(AuthTokenRequestModel atrm){
SoupResponseInvoiceDetail srid = null;
try{
srid = service.getInvoice(
atrm.getMcn(),"transactionId", atrm.getInvoiceNumber()
);
LOG.info("try block end");
}
catch(InterruptedException e)
{
LOG.info("Over here");
return Response.status(401).build();
}
return Response.ok(srid).build();
}
return Response.status(401).build();
}
// all getter setter
}
服务
package com.ncr.invoice;
//all imports
@Path("/")
@RegisterRestClient(configKey="invoice-api")
@ClientHeaderParam(name = "Authorization", value = "{basicAuth}")
public interface InvoiceService {
@GET
@Produces(MediaType.APPLICATION_JSON)
@Path("/")
public SoupResponseInvoiceDetail getInvoice(@QueryParam("id") String id,@QueryParam("txn_id") String txnId, @QueryParam("invoice") String invoice) throws InterruptedException;
default String basicAuth() {
String uName = ConfigProvider.getConfig().getValue("auth.username", String.class);
String pwd = ConfigProvider.getConfig().getValue("auth.pwd", String.class);
String creds = uName + ":" + pwd;
return "Basic " + Base64.getEncoder().encodeToString(creds.getBytes());
}
}
我遇到的错误 2021-01-07 13:07:42,286 INFO [com.ncr.inv.InvoiceController] (executor-thread-189) 尝试块结束 2021-01-07 13:07:42,555 错误 [io.und.req.io] (executor-thread-189) 异常处理请求 58a6d4b3-76c1-4a8b-b4a0-1e241219fb4d-4 到 /api/invoice: org.jboss .resteasy.spi.UnhandledException:org.eclipse.microprofile.faulttolerance.exceptions.TimeoutException:超时[com.ncr.invoice.InvoiceController#getInvoice]超时 在 org.jboss.resteasy.core.ExceptionHandler.handleApplicationException(ExceptionHandler.java:106) 在 org.jboss.resteasy.core.ExceptionHandler.handleException(ExceptionHandler.java:372) 在 org.jboss.resteasy.core.SynchronousDispatcher.writeException(SynchronousDispatcher.java:218) 在 org.jboss.resteasy.core.SynchronousDispatcher.invoke(SynchronousDispatcher.java:519) 在 org.jboss.resteasy.core.SynchronousDispatcher.lambda$invoke$4(SynchronousDispatcher.java:261) 在 org.jboss.resteasy.core.SynchronousDispatcher.lambda$preprocess$0(SynchronousDispatcher.java:161) ................................................
对我来说,你需要做这样的事情:
@Provider
public class TimeoutExceptionMapper implements ExceptionMapper<TimeoutException> {
private static final Logger LOGGER = LoggerFactory.getLogger(java.lang.invoke.MethodHandles.lookup().lookupClass());
@Override
public Response toResponse(TimeoutException exception) {
LOGGER.warn(exception.getMessage());
return getResponse();
}
public static Response getResponse() {
ErrorResponse response = new ErrorResponse();
response.setDescription("Operation timet out, try again later");
return status(Response.Status.GATEWAY_TIMEOUT).entity(response).build();
}
}