Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jersey client API WebResource accept() not setting MIME header correctly?

Tags:

java

jersey

public static WebResource createWebResource()
{
    final ClientConfig  cc = new DefaultClientConfig();
    final Client        c = Client.create(cc);
    final WebResource   wr = c.resource("http://localhost:19801/wtg_inventory_war/wtg/rest")
                                  .path(inv);
    return wr;
}

public void tester()
{
final WebResource  wr = JaxrsClientUtil.createWebResource()
                                 .path("wtg-service");

    wr.accept(MediaType.APPLICATION_XML);

String   response = wr.path("get-services")
                          .type(MediaType.APPLICATION_XML)
                          .get(String.class);
    System.out.println(response);
}

Server side:

@Path("get-services")
@GET
@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
public Response handleFindInventoryServices(
@Context WtgSpringContainer     ioc  // Spring config for service operations
)
{
    System.out.println("Got a service listing request...");
    LOGGER.info("Got a service listing request");

    Get the app specific data formatted in JAXB XML or JSON...

    .
    .
    .


    return Response.ok(msg).build();
}

Regardless of what the client side sets for acceptable media type, JSON comes back? Using curl with -HAccept:application/json or application/xml works fine. I'd like to test my server with both without changing the server side.

Any pointers as to why I cannot force the server to XML as my preferred MIME type?

like image 255
David Webster Avatar asked Feb 26 '26 04:02

David Webster


1 Answers

David, I figured it out. You did the same thing I did...

WebResource.accept(..) is a static method and is actually returning a WebResource.Builder instance to you that we were both ignoring, with the correct accept param set on.

Once I changed my code from:

WebResource res = c.resource("http://localhost:5984/");
res.accept(MediaType.APPLICATION_JSON_TYPE);
System.out.println(res.get(String.class));

to:

WebResource res = c.resource("http://localhost:5984/");
Builder builder = res.accept(MediaType.APPLICATION_JSON_TYPE);
System.out.println(builder.get(String.class));

Everything started working, the correct 'Accept' headers got sent to the server.

Hope that helps.

like image 72
Riyad Kalla Avatar answered Feb 27 '26 17:02

Riyad Kalla