How to force using particular MediaType in RestEasy client

I have a shared API and services are annotated

@Produces({"application/json","application/x-jackson-smile"})
@Consumes({"application/json","application/x-jackson-smile"})
public class AServiceClass {

So default is JSON - this will be preferred when using browser AJAX calls.

However I have a RestEasy client which I create using

ProxyFactory.create(AServiceClass.class, url)

And I want this client to use SMILE for both inbound and aoutbound communication. Of course it picks first item from @Consumes and tries marshalling to JSON.

I'm using RestEasy 2.3.5

How to force the client to use SMILE marshalling?

How to force the client to accept SMILE instead of JSON?

Actually it turns out that you ( I mean I :-) ) can't do this.

Checking MediaTypeHelper.getConsumes() shows that always first annotation value is picked to determine marshalling media type.

return MediaType.valueOf(consume.value()[0]);

The same happens when it comes to figuring out accept header. The code uses MediaTypeHelper.getProduces()

json, Of course it picks first item from @Consumes and tries marshalling to JSON. I'm using RestEasy 2.3.5. How to force the client to use SMILE marshalling? Packages that use MediaType; javax.ws.rs.core: Low-level interfaces and annotations used to create RESTful service resources. javax.ws.rs.ext: APIs that provide extensions to the types supported by the JAX-RS API.

It can be done by specifying the value for the header Accept

             Response response = client.target(host + "/yourpath").request()
                 .header(HttpHeaders.ACCEPT, "application/x-jackson-smile")
                 .get();

How to force using particular MediaType in RestEasy client, Of course it picks first item from #Consumes and tries marshalling to JSON. I'm using RestEasy 2.3.5 How to force the client to use SMILE marshalling? How to  Creates a new instance of MediaType with the supplied type, subtype and parameters. Parameters: type - the primary type, null is equivalent to MEDIA_TYPE_WILDCARD. subtype - the subtype, null is equivalent to MEDIA_TYPE_WILDCARD. parameters - a map of media type parameters, null is the same as an empty map.

You can also achieve this with a ClientRequestFilter, which is more usful if you are using proxies of your JAX-RS annotated classes. For example

public class AcceptedHeaderFilter implements ClientRequestFilter
{
    private final MediaType acceptedType;

    public AcceptedHeaderFilter(final MediaType acceptedType)
    {
        super();
        this.acceptedType = acceptedType;
    }

    @Override
    public void filter(final ClientRequestContext requestContext) throws IOException
    {
        requestContext.getHeaders().get(HttpHeaders.ACCEPT).clear();
        requestContext.getHeaders().get(HttpHeaders.ACCEPT).add(acceptedType.toString());
    }

}

If you are using Resteasy, you can register on your ResteasyWebTarget

MediaType contentType = MediaType.APPLICATION_XML_TYPE;
final ResteasyWebTarget target = buildTarget();
target.getResteasyClient().register(new AcceptedHeaderFilter(contentType));

CXF or Jersey will let you register the filter, but will require you to do it in a slightly different way.

RESTEasy JAX-RS - JBoss Community Confluence, Apache HTTP Client 4.x and other backends resteasy.media.type.mappings, no default, Replaces the need for an Accept header by Given the previous book store service augmented with comments: Besides mapping XML to JSON namespaces, you can also force @XmlAttribute's to be marshaled as XMLElements. In the previous tutorial, we have developed CRUD restful web services using RESTEasy.In this tutorial, we will learn how to build a RESTful client for consuming GET, POST, PUT and DELETE RESTFul APIs. JAX-RS 2.0 Client API Overview

[PDF] RESTEasy Reference Guide, A reference guide to RESTEasy for use with JBoss Enterprise Application Platform 5 RESTEasy JAX-RS Client Framework, allowing you to map outgoing HTTP requests to remote servers wildcard matching of one particular segment, like so: For example, MediaType has a valueOf() method, so you. Creates a new instance of MediaType with the supplied type, subtype and parameters. Parameters: type - the primary type, null is equivalent to MEDIA_TYPE_WILDCARD. subtype - the subtype, null is equivalent to MEDIA_TYPE_WILDCARD. parameters - a map of media type parameters, null is the same as an empty map.

Jersey 2.30.1 User Guide, Client API. 5.1. Uniform Interface Constraint; 5.2. Ease of use and reusing JAX-​RS artifacts; 5.3. Using File with a specific media type to produce a response; 7.2. If you want to force Jersey to use the algorithm compatible with JAX-RS 1.x​,  RESTeasy Proxy Framework: by using the ResteasyClientBuilder class; We will focus on the RESTEasy Proxy Framework here. Instead of using JAX-RS annotations to map an incoming request to your RESTFul Web Service method, the client framework builds an HTTP request that it uses to invoke on a remote RESTful Web Service. So let's start writing a

arquillian-extension-rest/RestClientTestCase.java at master , See the License for the specific language governing permissions and. * limitations under the javax.ws.rs.core.MediaType; Annotate the TestClass's TestMethods with JAX-RS Client annotations. * <p/> To force proxy to. * use XML we  RESTEasy allows you to map file name suffixes like (.xml, .txt, .en, .fr) to media types and languages. These file name suffixes take the place and override any Accept header sent by the client. You configure this using the resteasy.media.type.mappings and resteasy.language.mappings context-param variables within your web.xml.