users@jersey.java.net

Re: [Jersey] UniformInterfaceException

From: Paul Sandoz <Paul.Sandoz_at_Sun.COM>
Date: Wed, 17 Mar 2010 08:51:08 +0100

On Mar 17, 2010, at 4:37 AM, Rameswara Sashi Kiran Challa wrote:

> Hello Paul,
>
> I get the following error when I post something using client to the
> rest webservice that I built.
>
> com.sun.jersey.api.client.UniformInterfaceException: POST http://localhost:8080/Atom2RDF/atomfeed/rdf
> returned a response status of 404
>
> What is this error caused by ?

It means the resource on the server side identified by the URI http://localhost:8080/Atom2RDF/atomfeed/rdf
  does not exist.


>
> What exactly is UniformInterfaceException. Is it something to do
> with the MediaTypes ?
>

See the JavaDoc:

https://jersey.dev.java.net/nonav/apidocs/latest/jersey/com/sun/jersey/api/client/UniformInterfaceException.html


> Could you please forward me to some examples of using
> MessageBodyWriter to return an Array or ArrayList. I am actually
> using JAXP to parse my XML, am not unmarshalling using JAXB. Can I
> still use MessageBodyWriter for the same ??
>

Yes.

At the end of the email is code from the JAXB writer that supports
lists. You can copy and adapt as appropriate. The tricky bit is using
the reflection appropriately for generics.

See also the entity provider sample (although this does use lists of
stuff):

http://download.java.net/maven/2/com/sun/jersey/samples/entity-provider/1.1.5.1/entity-provider-1.1.5.1-project.zip

Paul.

     public boolean isWriteable(Class<?> type, Type genericType,
Annotation annotations[], MediaType mediaType) {
         if (List.class.isAssignableFrom(type)) {
             return verifyGenericType(genericType) &&
isSupported(mediaType);
         } else if (type.isArray()) {
             return verifyArrayType(type) && isSupported(mediaType);
         } else
             return false;
     }

     private boolean verifyArrayType(Class type) {
         type = type.getComponentType();

         return type.isAnnotationPresent(XmlRootElement.class) ||
                 type.isAnnotationPresent(XmlType.class);
     }

     private boolean verifyGenericType(Type genericType) {
         if (!(genericType instanceof ParameterizedType)) return false;

         final ParameterizedType pt = (ParameterizedType)genericType;

         if (pt.getActualTypeArguments().length > 1) return false;

         if (!(pt.getActualTypeArguments()[0] instanceof Class))
return false;

         final Class listClass = (Class)pt.getActualTypeArguments()[0];

         return listClass.isAnnotationPresent(XmlRootElement.class) ||
                 listClass.isAnnotationPresent(XmlType.class);
     }