Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.9k views
in Technique[技术] by (71.8m points)

api - How to pass HTTP status code from service to controller in quarkus?

I am trying to connect to a third-party API from quarkus controller . I have a controller using the method of service.

Controller

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 
}

service

@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;

   
}

How to return the HTTP status code from service to controller? I need to look into the response body when http status is 500


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can return a javax.ws.rs.core.Response inside your InvoiceService Rest Client instead of the SoupResponseInvoiceDetail POJO.

It will allow you to access the raw response with the HTTP response status.

You will still be able to access the response body via response.readEntity(SoupResponseInvoiceDetail.class) from inside your InvoiceController.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...