Hi
On 22/09/14 15:23, jordan wrote:
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.
JAX-RS 2.0 Client implementation delegates to the CXF specific WebClient
implementation - the latter defaults to XML if the response entity is
expected and no Accept is set.
I'm not sure is there any reason to set application/xml as default? I always
think */* should be the default at least.
I think in the HTTP centric API one needs to be explicit about the
actual expected response format. Specifying a wildcard by default is as
likely to make things worse as it is likely to fix simple issues where
String or primitive types are expected.
Or can we think this is a bug?
Yes, if we talk about a JAX-RS 2.0 client invocation path, given that
the documentation for Client API does not require defaulting to some
specific media type if Accept is missing. I'll get it fixed
Thanks, Sergey
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.
--
Sergey Beryozkin
Talend Community Coders
http://coders.talend.com/
Blog: http://sberyozkin.blogspot.com