You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@cxf.apache.org by anthony1975 <an...@gmail.com> on 2014/05/27 16:15:03 UTC
WADL Json Error
Hello
When i try to dislay wadl with json format (Xml is
correct)...http://localhost:8080/wsSCC/api/users/?_wadl&_type=json
The result looks like this ....
Is there a way to resolve this ?
>From my spring-servlet.xml :
Thanks :)
System:
- springframework.version : 3.2.3.RELEASE
- cxf.version : 2.7.8
- jackson.version : 2.0.2
--
View this message in context: http://cxf.547215.n5.nabble.com/WADL-Json-Error-tp5744477.html
Sent from the cxf-user mailing list archive at Nabble.com.
Re: WADL Json Error
Posted by Sergey Beryozkin <sb...@gmail.com>.
Hi
On 28/08/14 14:39, anthony1975 wrote:
> Yes Sergey, *it works !*
>
Great, thanks for making it work. It's a bit of a hack, having to
compose multiple providers, but I have to say the fact Jackson does
accept all types by default often does not help.
By the way, Romain opened the enhancement request earlier on for
WADLGenerator use a typed WADL model for Jackson be able to serialize it
too in JSON format if it is registered.
> For your first question, it's probably
> org.apache.cxf.jaxrs.model.wadl.WadlGenerator who produces XML ?
>
WADLGenerator does produce XML by default but in your case you requested
a JSON format. Can you please give me a favor and try your original
configuration (the one you used when you started this thread) and try
the same request URI with _type=json ?
I'm really curious, given that you've had Jackson registered, where XML
is coming from :-). If you have XML reported with Jackson registered,
then try to remove it and see what happens, all with a _type=json
Give it a try please
Thanks, Sergey
> Thank you so much
>
> Anthony
>
>
>
>
>
> --
> View this message in context: http://cxf.547215.n5.nabble.com/WADL-Json-Error-tp5744477p5748261.html
> Sent from the cxf-user mailing list archive at Nabble.com.
>
Re: WADL Json Error
Posted by anthony1975 <an...@gmail.com>.
Yes Sergey, *it works !*
For your first question, it's probably
org.apache.cxf.jaxrs.model.wadl.WadlGenerator who produces XML ?
Thank you so much
Anthony
--
View this message in context: http://cxf.547215.n5.nabble.com/WADL-Json-Error-tp5744477p5748261.html
Sent from the cxf-user mailing list archive at Nabble.com.
Re: WADL Json Error
Posted by Sergey Beryozkin <sb...@gmail.com>.
So how, given the query, were you getting WADL in XML before ? Clearly
it was not Jackson which was producing XML ? Or was it ? I'm curious how
it worked.
Either way we are nearly there I guess.
In CXF, JSONProvider can support Document. You want to use Jackson for
producing JSON in general but Jackson (apparently) does not support
Document. So if we register JSONProvider alongside this new custom
provider we'd block Jackson. So I guess my suggestion was not correct,
sorry.
You are likely need to:
private JacksonJsonProvoder jackson;
private JSONProvider cxfJsonProvider;
public boolean isWriteable(Class<?> type, Type genericType,
Annotation[] annotations, MediaType mediaType) {
if (Document.class.isAssignableFrom(type)) {
return true;
} else {
return jsonProvider.isWriteable(type, genericType, annotations,
mediaType);
}
}
and then in writeTo, if it is Document, delegate to cxfJsonProvider,
otherwise to jackson.
Cheers, Sergey
On 28/08/14 12:57, anthony1975 wrote:
> Yes of course ... :)
>
> New message from url http://localhost:8080/wsSCC/api/dogs/?_wadl&_type=json
>
> *No message body writer has been found for response class DocumentImpl.*
>
> @Override
> public boolean isWriteable(Class<?> type, Type genericType,
> Annotation[] annotations, MediaType mediaType) {
> if (Document.class.isAssignableFrom(type)) {
> return false; <-- Here
> } else {
> return jsonProvider.isWriteable(type, genericType, annotations,
> mediaType);
> }
> }
>
> Anthony
>
>
>
>
> --
> View this message in context: http://cxf.547215.n5.nabble.com/WADL-Json-Error-tp5744477p5748257.html
> Sent from the cxf-user mailing list archive at Nabble.com.
>
Re: WADL Json Error
Posted by anthony1975 <an...@gmail.com>.
Yes of course ... :)
New message from url http://localhost:8080/wsSCC/api/dogs/?_wadl&_type=json
*No message body writer has been found for response class DocumentImpl.*
@Override
public boolean isWriteable(Class<?> type, Type genericType,
Annotation[] annotations, MediaType mediaType) {
if (Document.class.isAssignableFrom(type)) {
return false; <-- Here
} else {
return jsonProvider.isWriteable(type, genericType, annotations,
mediaType);
}
}
Anthony
--
View this message in context: http://cxf.547215.n5.nabble.com/WADL-Json-Error-tp5744477p5748257.html
Sent from the cxf-user mailing list archive at Nabble.com.
Re: WADL Json Error
Posted by Sergey Beryozkin <sb...@gmail.com>.
Well, I guess you need to address this error then :-)
Use a simple constructor injection, Spring supports it well...
Cheers, Sergey
On 28/08/14 11:23, anthony1975 wrote:
> When I call http://localhost:8080/wsSCC/api/dogs/?_wadl&_type=json
> class:com.sun.org.apache.xerces.internal.dom.DocumentImpl
> mediaType: application/json
>
> *Make sure no Jackson is also registered*
> I comment my jsonProvider bean which register another
> JacksonJaxbJsonProvider...
>
> But my stack trace say...
> org.springframework.beans.factory.UnsatisfiedDependencyException: Error
> creating bean with name 'wadlJacksonJsonProvider
>
> <beans>
> (...)
> <bean id="wadlGenerator"
> class="org.apache.cxf.jaxrs.model.wadl.WadlGenerator">
> <property name="linkJsonToXmlSchema" value="true" />
> <property name="schemaLocations">
> <list>
> <value>classpath:/json.schema</value>
> </list>
> </property>
> </bean>
>
> *
>
> *
> <bean id="wadlJacksonJsonProvider"
> class="fr.scc.utils.WadlJacksonJsonProvider"/>
>
> <jaxrs:server id="restContainer" address="/">
> <jaxrs:serviceBeans>
>
> (...)
> </jaxrs:serviceBeans>
> <jaxrs:providers>
> * *
> <ref bean="wadlGenerator" />
> <ref bean="wadlJacksonJsonProvider" />
> </jaxrs:providers>
> </jaxrs:server>
> </beans>
>
> Anthony
>
>
>
> --
> View this message in context: http://cxf.547215.n5.nabble.com/WADL-Json-Error-tp5744477p5748250.html
> Sent from the cxf-user mailing list archive at Nabble.com.
>
Re: WADL Json Error
Posted by anthony1975 <an...@gmail.com>.
When I call http://localhost:8080/wsSCC/api/dogs/?_wadl&_type=json
class:com.sun.org.apache.xerces.internal.dom.DocumentImpl
mediaType: application/json
*Make sure no Jackson is also registered*
I comment my jsonProvider bean which register another
JacksonJaxbJsonProvider...
But my stack trace say...
org.springframework.beans.factory.UnsatisfiedDependencyException: Error
creating bean with name 'wadlJacksonJsonProvider
<beans>
(...)
<bean id="wadlGenerator"
class="org.apache.cxf.jaxrs.model.wadl.WadlGenerator">
<property name="linkJsonToXmlSchema" value="true" />
<property name="schemaLocations">
<list>
<value>classpath:/json.schema</value>
</list>
</property>
</bean>
*
*
<bean id="wadlJacksonJsonProvider"
class="fr.scc.utils.WadlJacksonJsonProvider"/>
<jaxrs:server id="restContainer" address="/">
<jaxrs:serviceBeans>
(...)
</jaxrs:serviceBeans>
<jaxrs:providers>
* *
<ref bean="wadlGenerator" />
<ref bean="wadlJacksonJsonProvider" />
</jaxrs:providers>
</jaxrs:server>
</beans>
Anthony
--
View this message in context: http://cxf.547215.n5.nabble.com/WADL-Json-Error-tp5744477p5748250.html
Sent from the cxf-user mailing list archive at Nabble.com.
Re: WADL Json Error
Posted by Sergey Beryozkin <sb...@gmail.com>.
Do you now register now WadlJacksonJsonProvider in jaxrs:providers ?
Make sure no Jackson is also registered. Other than that I'm not sure
why it is not invoked, is WadlJacksonJsonProvider.isWriteable called at
all ? What Class and MediaType parameters are passed ?
Sergey
On 28/08/14 10:41, anthony1975 wrote:
> So, i follow your instructions..If you can tell me where I'm wrong :)
> I put your comments (bold) in the code.
>
> import java.io.IOException;
> import java.io.OutputStream;
> import java.lang.annotation.Annotation;
> import java.lang.reflect.Type;
>
> import javax.inject.Inject;
> import javax.ws.rs.Produces;
> import javax.ws.rs.WebApplicationException;
> import javax.ws.rs.core.MediaType;
> import javax.ws.rs.core.MultivaluedMap;
> import javax.ws.rs.ext.MessageBodyWriter;
> import javax.ws.rs.ext.Provider;
> import org.w3c.dom.Document;
>
> import com.fasterxml.jackson.jaxrs.json.JacksonJaxbJsonProvider;
>
> @Provider
> @Produces({ MediaType.APPLICATION_JSON, "text/json" })
> public class WadlJacksonJsonProvider
> implements MessageBodyWriter {
>
> private final JacksonJaxbJsonProvider jsonProvider;
>
> * // init JacksonJaxbProvider*
> @Inject
> public WadlJacksonJsonProvider(JacksonJaxbJsonProvider jsonProvider) {
> this.jsonProvider = jsonProvider;
> }
>
> * //make sure getSize() returns -1*
> @Override
> public long getSize(Object arg0, Class<?> arg1, Type arg2,
> Annotation[] arg3, MediaType arg4) {
> // Return -1 to indicate that no explicit
> // content lenght can be assigned
> return -1;
> }
>
> @Override
> public boolean isWriteable(Class<?> type, Type genericType,
> Annotation[] annotations, MediaType mediaType) {
> * // check if it is DOM Document, if yes, return false*
> if (Document.class.isAssignableFrom(type)) {
> return false;
> } else {
> * // otherwise delegate to
> JacksonJaxbProvider.isWriteable *
> return jsonProvider.isWriteable(type, genericType, annotations,
> mediaType);
> }
> }
>
> @Override
> public void writeTo(Object arg0, Class<?> arg1, Type arg2,
> Annotation[] arg3, MediaType arg4,
> MultivaluedMap<String, Object> arg5, OutputStream arg6)
> throws IOException, WebApplicationException {
> // TODO Auto-generated method stub
>
> }
> }
>
> Perhaps, it's my injection in config file...I call
> <bean id="jsonProvider"
> class="com.fasterxml.jackson.jaxrs.json.JacksonJaxbJsonProvider"/>
> <bean id="wadlJacksonJsonProvider"
> class="fr.scc.utils.WadlJacksonJsonProvider"/>
>
> Thanks
>
> Anthony
>
>
>
> --
> View this message in context: http://cxf.547215.n5.nabble.com/WADL-Json-Error-tp5744477p5748246.html
> Sent from the cxf-user mailing list archive at Nabble.com.
>
Re: WADL Json Error
Posted by anthony1975 <an...@gmail.com>.
So, i follow your instructions..If you can tell me where I'm wrong :)
I put your comments (bold) in the code.
import java.io.IOException;
import java.io.OutputStream;
import java.lang.annotation.Annotation;
import java.lang.reflect.Type;
import javax.inject.Inject;
import javax.ws.rs.Produces;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.ext.MessageBodyWriter;
import javax.ws.rs.ext.Provider;
import org.w3c.dom.Document;
import com.fasterxml.jackson.jaxrs.json.JacksonJaxbJsonProvider;
@Provider
@Produces({ MediaType.APPLICATION_JSON, "text/json" })
public class WadlJacksonJsonProvider
implements MessageBodyWriter {
private final JacksonJaxbJsonProvider jsonProvider;
* // init JacksonJaxbProvider*
@Inject
public WadlJacksonJsonProvider(JacksonJaxbJsonProvider jsonProvider) {
this.jsonProvider = jsonProvider;
}
* //make sure getSize() returns -1*
@Override
public long getSize(Object arg0, Class<?> arg1, Type arg2,
Annotation[] arg3, MediaType arg4) {
// Return -1 to indicate that no explicit
// content lenght can be assigned
return -1;
}
@Override
public boolean isWriteable(Class<?> type, Type genericType,
Annotation[] annotations, MediaType mediaType) {
* // check if it is DOM Document, if yes, return false*
if (Document.class.isAssignableFrom(type)) {
return false;
} else {
* // otherwise delegate to
JacksonJaxbProvider.isWriteable *
return jsonProvider.isWriteable(type, genericType, annotations,
mediaType);
}
}
@Override
public void writeTo(Object arg0, Class<?> arg1, Type arg2,
Annotation[] arg3, MediaType arg4,
MultivaluedMap<String, Object> arg5, OutputStream arg6)
throws IOException, WebApplicationException {
// TODO Auto-generated method stub
}
}
Perhaps, it's my injection in config file...I call
<bean id="jsonProvider"
class="com.fasterxml.jackson.jaxrs.json.JacksonJaxbJsonProvider"/>
<bean id="wadlJacksonJsonProvider"
class="fr.scc.utils.WadlJacksonJsonProvider"/>
Thanks
Anthony
--
View this message in context: http://cxf.547215.n5.nabble.com/WADL-Json-Error-tp5744477p5748246.html
Sent from the cxf-user mailing list archive at Nabble.com.
Re: WADL Json Error
Posted by Sergey Beryozkin <sb...@gmail.com>.
I checked the thread, I suggested:
"Write a custom JAX-RS message body writer and init JacksonJaxbProvider.
In its isWriteable method check if it is DOM Document, if yes, return
false, otherwise delegate to JacksonJaxbProvider.isWriteable".
Another point - make sure getSize() returns -1. It's supposed to be
ignored but just in case...
Sergey
On 27/08/14 16:03, anthony1975 wrote:
> Ok
>
> Simplify my class, ...
> still not working with
>
> @Provider
> @Produces({ MediaType.APPLICATION_JSON, "text/json" })
> public class WadlJacksonJsonProvider
> implements MessageBodyWriter {
>
> private final JacksonJsonProvider delegate;
>
> @Inject
> public WadlJacksonJsonProvider(JacksonJsonProvider delegate) {
> this.delegate = delegate;
> }
>
> @Override
> public long getSize(Object arg0, Class<?> arg1, Type arg2,
> Annotation[] arg3, MediaType arg4) {
> // TODO Auto-generated method stub
> return 0;
> }
>
> @Override
> public boolean isWriteable(Class<?> type, Type genericType,
> Annotation[] annotations, MediaType mediaType) {
> //return Object.class.isAssignableFrom(type);
> return delegate.isWriteable(type, genericType, annotations, mediaType);
> }
>
> @Override
> public void writeTo(Object arg0, Class<?> arg1, Type arg2,
> Annotation[] arg3, MediaType arg4,
> MultivaluedMap<String, Object> arg5, OutputStream arg6)
> throws IOException, WebApplicationException {
> // TODO Auto-generated method stub
>
> }
> }
>
> Return xml with json format...
> "<?xml version=\"1.0\" encoding=\"UTF-16\"?>\n (...)"
>
> Annthony
>
>
>
>
> --
> View this message in context: http://cxf.547215.n5.nabble.com/WADL-Json-Error-tp5744477p5748213.html
> Sent from the cxf-user mailing list archive at Nabble.com.
>
--
Sergey Beryozkin
Talend Community Coders
http://coders.talend.com/
Blog: http://sberyozkin.blogspot.com
Re: WADL Json Error
Posted by anthony1975 <an...@gmail.com>.
Ok
Simplify my class, ...
still not working with
@Provider
@Produces({ MediaType.APPLICATION_JSON, "text/json" })
public class WadlJacksonJsonProvider
implements MessageBodyWriter {
private final JacksonJsonProvider delegate;
@Inject
public WadlJacksonJsonProvider(JacksonJsonProvider delegate) {
this.delegate = delegate;
}
@Override
public long getSize(Object arg0, Class<?> arg1, Type arg2,
Annotation[] arg3, MediaType arg4) {
// TODO Auto-generated method stub
return 0;
}
@Override
public boolean isWriteable(Class<?> type, Type genericType,
Annotation[] annotations, MediaType mediaType) {
//return Object.class.isAssignableFrom(type);
return delegate.isWriteable(type, genericType, annotations, mediaType);
}
@Override
public void writeTo(Object arg0, Class<?> arg1, Type arg2,
Annotation[] arg3, MediaType arg4,
MultivaluedMap<String, Object> arg5, OutputStream arg6)
throws IOException, WebApplicationException {
// TODO Auto-generated method stub
}
}
Return xml with json format...
"<?xml version=\"1.0\" encoding=\"UTF-16\"?>\n (...)"
Annthony
--
View this message in context: http://cxf.547215.n5.nabble.com/WADL-Json-Error-tp5744477p5748213.html
Sent from the cxf-user mailing list archive at Nabble.com.
Re: WADL Json Error
Posted by Sergey Beryozkin <sb...@gmail.com>.
Hi, add it to jaxrs:providers,
or it can be auto-discovered if preferred, only with Spring for now
Sergey
On 27/08/14 15:31, anthony1975 wrote:
> Hello,
>
> Come back perhaps too late...
> I try to complete your idea as doing this:
> Create a class WadlJacksonJsonProvider
>
> <pre>
> @Provider
> @Consumes({ MediaType.APPLICATION_JSON, "text/json" })
> @Produces({ MediaType.APPLICATION_JSON, "text/json" })
> public class WadlJacksonJsonProvider
> implements MessageBodyReader, MessageBodyWriter {
>
> private final JacksonJsonProvider delegate;
> private final Validator validator;
>
> @Inject
> public WadlJacksonJsonProvider(JacksonJsonProvider delegate, Validator
> validator) {
> this.delegate = delegate;
> this.validator = validator;
> }
>
> @Override
> public long getSize(Object arg0, Class<?> arg1, Type arg2,
> Annotation[] arg3, MediaType arg4) {
> // TODO Auto-generated method stub
> return 0;
> }
>
> @Override
> public boolean isWriteable(Class<?> type, Type genericType,
> Annotation[] annotations, MediaType mediaType) {
> return delegate.isWriteable(type, genericType, annotations, mediaType);
> }
>
> @Override
> public void writeTo(Object arg0, Class<?> arg1, Type arg2,
> Annotation[] arg3, MediaType arg4,
> MultivaluedMap<String, Object> arg5, OutputStream arg6)
> throws IOException, WebApplicationException {
> // TODO Auto-generated method stub
>
> }
>
> @Override
> public boolean isReadable(Class<?> arg0, Type arg1, Annotation[] arg2,
> MediaType arg3) {
> // TODO Auto-generated method stub
> return false;
> }
>
> @Override
> public Object readFrom(Class arg0, Type arg1, Annotation[] arg2,
> MediaType arg3, MultivaluedMap<String, String> arg4,
> InputStream arg5) throws IOException, WebApplicationException {
> // TODO Auto-generated method stub
> return null;
> }
>
> }
> </pre>
>
> How to register this class, pleaze ?
>
> Cheers
>
> Anthony
>
>
>
>
> --
> View this message in context: http://cxf.547215.n5.nabble.com/WADL-Json-Error-tp5744477p5748210.html
> Sent from the cxf-user mailing list archive at Nabble.com.
>
--
Sergey Beryozkin
Talend Community Coders
http://coders.talend.com/
Blog: http://sberyozkin.blogspot.com
Re: WADL Json Error
Posted by anthony1975 <an...@gmail.com>.
Hello,
Come back perhaps too late...
I try to complete your idea as doing this:
Create a class WadlJacksonJsonProvider
<pre>
@Provider
@Consumes({ MediaType.APPLICATION_JSON, "text/json" })
@Produces({ MediaType.APPLICATION_JSON, "text/json" })
public class WadlJacksonJsonProvider
implements MessageBodyReader, MessageBodyWriter {
private final JacksonJsonProvider delegate;
private final Validator validator;
@Inject
public WadlJacksonJsonProvider(JacksonJsonProvider delegate, Validator
validator) {
this.delegate = delegate;
this.validator = validator;
}
@Override
public long getSize(Object arg0, Class<?> arg1, Type arg2,
Annotation[] arg3, MediaType arg4) {
// TODO Auto-generated method stub
return 0;
}
@Override
public boolean isWriteable(Class<?> type, Type genericType,
Annotation[] annotations, MediaType mediaType) {
return delegate.isWriteable(type, genericType, annotations, mediaType);
}
@Override
public void writeTo(Object arg0, Class<?> arg1, Type arg2,
Annotation[] arg3, MediaType arg4,
MultivaluedMap<String, Object> arg5, OutputStream arg6)
throws IOException, WebApplicationException {
// TODO Auto-generated method stub
}
@Override
public boolean isReadable(Class<?> arg0, Type arg1, Annotation[] arg2,
MediaType arg3) {
// TODO Auto-generated method stub
return false;
}
@Override
public Object readFrom(Class arg0, Type arg1, Annotation[] arg2,
MediaType arg3, MultivaluedMap<String, String> arg4,
InputStream arg5) throws IOException, WebApplicationException {
// TODO Auto-generated method stub
return null;
}
}
</pre>
How to register this class, pleaze ?
Cheers
Anthony
--
View this message in context: http://cxf.547215.n5.nabble.com/WADL-Json-Error-tp5744477p5748210.html
Sent from the cxf-user mailing list archive at Nabble.com.
Re: WADL Json Error
Posted by Sergey Beryozkin <sb...@gmail.com>.
Hi
On 27/05/14 15:15, anthony1975 wrote:
> Hello
>
> When i try to dislay wadl with json format (Xml is
> correct)...http://localhost:8080/wsSCC/api/users/?_wadl&_type=json
>
> The result looks like this ....
>
>
>
> Is there a way to resolve this ?
>
> From my spring-servlet.xml :
>
>
>
> Thanks :)
>
> System:
> - springframework.version : 3.2.3.RELEASE
> - cxf.version : 2.7.8
> - jackson.version : 2.0.2
>
right now it depends on a JSON provider be capable of transforming a DOM
Document into JSON. CXF JSONProvider does it by relying on CXF DOM-aware
XMLStreamWriter.
Can you configure JacksonJaxbProvider to support DOM ?
If not then you can do the following workaround:
Write a custom JAX-RS message body writer and init JacksonJaxbProvider.
In its isWriteable method check if it is DOM Document, if yes, return
false, otherwise delegate to JacksonJaxbProvider.isWriteable
Please temp disable Jackson to confirm you can have WADL in JSON format
returned before doing a custom writer
Cheers, Sergey
>
>
>
> --
> View this message in context: http://cxf.547215.n5.nabble.com/WADL-Json-Error-tp5744477.html
> Sent from the cxf-user mailing list archive at Nabble.com.
>
--
Sergey Beryozkin
Talend Community Coders
http://coders.talend.com/
Blog: http://sberyozkin.blogspot.com