如何将 HTTP 状态代码从服务传递到 quarkus 中的控制器?

问题描述

我正在尝试从 quarkus 控制器连接到第三方 API。我有一个使用服务方法的控制器。

控制器

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 
}

服务

@Path("/")
@RegisterRestClient(configKey="invoice-api")
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;

   
}

如何将 HTTP 状态码从服务返回给控制器? 当 http 状态为 500 时,我需要查看响应正文

解决方法

您可以在 javax.ws.rs.core.Response Rest Client 中返回 InvoiceService 而不是 SoupResponseInvoiceDetail POJO。

它将允许您访问带有 HTTP 响应状态的原始响应。

您仍然可以从 response.readEntity(SoupResponseInvoiceDetail.class) 内部通过 InvoiceController 访问响应正文。