Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[question] How to use Feign with a JAX-RS interface defining jaxrs.Response as return types #188

Closed
bquenin opened this issue Feb 18, 2015 · 7 comments · May be fixed by #2361
Closed

[question] How to use Feign with a JAX-RS interface defining jaxrs.Response as return types #188

bquenin opened this issue Feb 18, 2015 · 7 comments · May be fixed by #2361

Comments

@bquenin
Copy link

bquenin commented Feb 18, 2015

Hi,

I'm using Feign with a JAX-RS interface using JAX-RS Response return types (not feign.Response).
For instance:

@Path("/users")
@Produces(MediaType.APPLICATION_JSON)
public interface UserContract {
    @POST
    @Path("/")
    @Consumes(MediaType.APPLICATION_JSON)
    public Response create(User user);

    ...

When using feign and calling the "create" method, it seems that feigns tries to deserialize the jaxrs.Response object from the HTTP reponse.

Well it makes sense since it's the return type but it's obviously not what I expect. What would be the best way to handle jaxrs.Response type with feign ?

Thanks

@codefromthecrypt
Copy link
Contributor

codefromthecrypt commented Feb 18, 2015 via email

@codefromthecrypt
Copy link
Contributor

Here's a workaround.. wrap your real decoder with this.

final class ResponseDecoder implements Decoder {
  private final Decoder delegate;

  ResponseDecoder(Decoder delegate) {
    this.delegate = delegate;
  }

  @Override
  public Object decode(Response response, Type type) throws IOException {
    if (jaxrs.Response.class.equals(type)) {
      // construct and return.
    }
    return delegate.decode(response, type);
  }
}

@bquenin
Copy link
Author

bquenin commented Feb 19, 2015

Thanks, that's basically the solution I had, just wanted a confirmation.

@bquenin bquenin closed this as completed Feb 19, 2015
@AjitDas
Copy link

AjitDas commented Nov 2, 2017

@bquenin @adriancole do you have sample to share how you converted feign Response to jaxrs Response with the entity object set into it.

Thanks a lot in advance

import java.io.IOException;
import java.lang.reflect.Type;
import java.util.Collection;
import java.util.Map;

import javax.ws.rs.core.Response.ResponseBuilder;

import feign.Response;
import feign.codec.Decoder;

public class ResponseDecoder implements Decoder {

	private final Decoder delegate;

	public ResponseDecoder(Decoder delegate) {
		this.delegate = delegate;
	}

	@Override
	public Object decode(Response response, Type type) throws IOException {
		
		if (javax.ws.rs.core.Response.class.equals(type)) {
			// construct and return.
			ResponseBuilder responseBuilder = javax.ws.rs.core.Response
					.status(response.status())
					.entity(...................); // need to get the mapped entity object here , calling delegate.decode(response, type) won't help since the type is javax.ws.rs.core.Response here
			for(Map.Entry<String,Collection<String>> entry:response.headers().entrySet()) {
				responseBuilder.header(entry.getKey(), entry.getValue());
			}
			return responseBuilder.build();
		}
		return delegate.decode(response, type);
	}
}

@mattmadhavan
Copy link

Helo,
I have been stuck on this same issue for two days. Can any of you share the source code please?

@mattmadhavan
Copy link

mattmadhavan commented Jun 7, 2020

Sorry figure it out! Got my entity like below!

	@Override
	public Object decode(Response response, Type type) throws IOException {
		
		if (javax.ws.rs.core.Response.class.equals(type)) {
			
			String bodyStr = IOUtils.toString(response.body().asInputStream(), Charsets.UTF_8);
			// construct and return.
			ResponseBuilder responseBuilder = javax.ws.rs.core.Response
					.status(response.status())
					.entity(bodyStr); // need to get the mapped entity object here , calling delegate.decode(response, type) won't help since the type is javax.ws.rs.core.Response here
			for(Map.Entry<String,Collection<String>> entry:response.headers().entrySet()) {
				responseBuilder.header(entry.getKey(), entry.getValue());
			}
			return responseBuilder.build();
		}
		return delegate.decode(response, type);
	}

@velo
Copy link
Member

velo commented Jun 8, 2020

That could be included on the jaxrs module... if you are interest in making a PR, I can review it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants