cxf-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jordan <zhang...@cn.ibm.com>
Subject Why CXF Client default accept type is application/xml
Date Mon, 22 Sep 2014 14:23:15 GMT
Hi dear all.

We have using the CXF client. But we found the client default accept type is
application/xml and is not text/plain or */*. Could you please help take a
look? Appreciate it! :)

I think I get the root cause and I'm not sure if this can be treated as a
bug?
 
The reason is that if we don't explicitly set the accept header of the
client request, it will set it as "application/xml" instead of "*/*".
Therefore a request with accept header := application/xml comes to the
server. As the resource class is annotated by @Produce(TEXT_PLAIN), it will
refuse accept header with application/xml with 406 as expected. 

    @GET
    @Produces(MediaType.TEXT_PLAIN)
    public String getAll() {
        return strings.toString();
    }

Client code is:

ClientBuilder cb = ClientBuilder.newBuilder();
				Client c = cb.build();
				WebTarget t1 = c.target(uri + "/string");
				String result = t1.request()..get(String.class);

				htmlPage = "\nClient string test: " + "\nReturn result: "
						+ result;
				System.out.println(htmlPage);
				c.close();


Then I will get the error message:

[ERROR   ] SRVE0777E: Exception thrown by application class
'sun.reflect.NativeConstructorAccessorImpl.newInstance0:-2'
javax.ws.rs.NotAcceptableException: HTTP 406 Not Acceptable
	at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
	at
sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:85)
	at
sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:57)
	at java.lang.reflect.Constructor.newInstance(Constructor.java:541)
	at
org.apache.cxf.jaxrs.client.AbstractClient.convertToWebApplicationException(AbstractClient.java:478)
	at [internal classes]


If I modify client code to:
target.request().accept(*MediaType.WILDCARD*).get(String.class);

The exception is disappear.


When I learn the CXF code, I found CXF really set the default accept type to
application/xml.

I'm not sure is there any reason to set application/xml as default? I always
think */* should be the default at least.

Or can we think this is a bug?


Thanks for your time and help! :)




--
View this message in context: http://cxf.547215.n5.nabble.com/Why-CXF-Client-default-accept-type-is-application-xml-tp5749057.html
Sent from the cxf-dev mailing list archive at Nabble.com.

Mime
View raw message