jakartaee / rest

Jakarta RESTful Web Services
Other
361 stars 117 forks source link

Properly write a rest client interface #1160

Closed nicolasard closed 1 year ago

nicolasard commented 1 year ago

Hello, I can't find how I can write a proper interface definition that is able to parse the payload and the headers of the response.

If I write the following interface, with restEasy I can get the response properly and that's deserialized. But I want to read the response headers too.

@RegisterRestClient(configKey = "demo")
public interface DemoInterfaceApi{

    @POST
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
    @Path("/demo")
    CustomJacksonAnotatedResponse doInquiry(@HeaderParam("Authorization") String authorization,
                                               CustomJacksonAnotatedRequest customJacksonAnotatedRequest );
}

Then to read the headers I can do this and use javax.ws.rs.core.Response

@RegisterRestClient(configKey = "demo")
public interface DemoInterfaceApi{

    @POST
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
    @Path("/demo")
    Response doInquiry(@HeaderParam("Authorization") String authorization,
                                               CustomJacksonAnotatedRequest customJacksonAnotatedRequest );

}

I can get the entity now from the Response and the headers. But I is not clear by reading the interface what type of response object we expect. I wonder if there si something like Response that I can use that wraps the custom entity object with the header values.

NicoNes commented 1 year ago

Hi @nicolasard ,

I wonder if there si something like Response that I can use that wraps the custom entity object with the header values.

I'm not sure that such a "custom" Response wrapper with both your custom response entity and the response header exists.

But maybe you can create this Response wrapper and register a custom MessageBodyReader<T> to convert the HTTP stream into this Response wrapper. Something like that :

    public class CustomJacksonAnotatedResponseWrapper {

        private CustomJacksonAnotatedResponse responseEntity;
        private MultivaluedMap<String, String> responseHeaders;

        ....
    }

    public class MessageBodyReaderImpl implements MessageBodyReader<CustomJacksonAnotatedResponseWrapper> {

         ...

        @Override
        public CustomJacksonAnotatedResponseWrapper readFrom(Class<CustomJacksonAnotatedResponseWrapper> type, Type genericType, Annotation[] annotations, MediaType mediaType,
                MultivaluedMap<String, String> httpHeaders, InputStream entityStream) throws IOException, WebApplicationException {
            CustomJacksonAnotatedResponseWrapper customResponseEntityWrapper = new CustomJacksonAnotatedResponseWrapper();
            customResponseEntityWrapper.setResponseHeaders(httpHeaders);
            customResponseEntityWrapper.setResponseEntity(objectMapper.read(entityStream, CustomJacksonAnotatedResponse.class));
            return customResponseEntityWrapper;
        }

    }

-- Nicolas

jamezp commented 1 year ago

This should really be a discussion on https://github.com/resteasy/resteasy/discussions. This is discussion area is for the Jakarta RESTful Web Services specification of which RESTEasy is an implementation of.

spericas commented 1 year ago

Closing given that Rest Client is in MP.