users@jersey.java.net

posting a list of objects

From: Suchitha Koneru (sukoneru) <"Suchitha>
Date: Wed, 6 Jan 2010 17:15:42 -0800

Hello Jersey Users ,

                 I am using Jersey version 1.1.1-ea, JDK 1.5 and
application server Jboss 5.1.0GA for developing restful services . I
have used POSTER (plug in in fire fox) for testing out the restful
service apis

 

 

The following Restful service APIS did not work

 

1)

@POST @Path("echoListThree")

      @Consumes ("application/xml")

      @Produces ("application/xml")

      

      public List<UserBean> echoList(List<UserBean> ubeans) {

            return ubeans ;

      }

      

2)

      @POST @Path("echoListFour")

      @Consumes ("application/xml")

      @Produces ("application/xml")

      public List<UserBean> echoJAXBList(JAXBElement<List<UserBean>>
ubeans) {

            return ubeans.getValue();

      }

 

 

The xml which was posted as input for both of these APIS is

<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>

 <userBeans>

 

<userBean>

  <firstName>John</firstName>

  <lastName>Smith</lastName>

  <phoneNumber>9999999</phoneNumber>

  <userId>5</userId>

 </userBean>

 

 <userBean>

  <firstName>Suchitha</firstName>

  <lastName>Koneru</lastName>

  <phoneNumber>888888888</phoneNumber>

  <userId>1</userId>

  </userBean>

  

</userBeans>

 

The API 1) threw a Http 500 error and in the server logs I found the
following exception

 

6:15:31,535 ERROR [[Jersey Web Application]] Servlet.service() for
servlet Jersey Web Application threw exception

java.lang.ClassCastException:
sun.reflect.generics.reflectiveObjects.ParameterizedTypeImpl

                at
com.sun.jersey.core.provider.jaxb.AbstractJAXBElementProvider.readFrom(A
bstractJAXBElementProvider.java:88)

                at
com.sun.jersey.core.provider.jaxb.AbstractJAXBElementProvider.readFrom(A
bstractJAXBElementProvider.java:63)

                at
com.sun.jersey.spi.container.ContainerRequest.getEntity(ContainerRequest
.java:393)

                at
com.sun.jersey.server.impl.model.method.dispatch.EntityParamDispatchProv
ider$EntityInjectable.getValue(EntityParamDispatchProvider.java:139)

                at
com.sun.jersey.server.impl.inject.InjectableValuesProvider.getInjectable
Values(InjectableValuesProvider.java:43)

                at
com.sun.jersey.server.impl.model.method.dispatch.AbstractResourceMethodD
ispatchProvider$EntityParamInInvoker.getParams(AbstractResourceMethodDis
patchProvider.java:126)

                at
com.sun.jersey.server.impl.model.method.dispatch.AbstractResourceMethodD
ispatchProvider$TypeOutInvoker._dispatch(AbstractResourceMethodDispatchP
rovider.java:154)

                at
com.sun.jersey.server.impl.model.method.dispatch.ResourceJavaMethodDispa
tcher.dispatch(ResourceJavaMethodDispatcher.java:67)

                at
com.sun.jersey.server.impl.uri.rules.HttpMethodRule.accept(HttpMethodRul
e.java:166)

                at
com.sun.jersey.server.impl.uri.rules.RightHandPathRule.accept(RightHandP
athRule.java:114)

                at
com.sun.jersey.server.impl.uri.rules.ResourceClassRule.accept(ResourceCl
assRule.java:74)

                at
com.sun.jersey.server.impl.uri.rules.RightHandPathRule.accept(RightHandP
athRule.java:114)

                at
com.sun.jersey.server.impl.uri.rules.RootResourceClassesRule.accept(Root
ResourceClassesRule.java:66)

                at
com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest
(WebApplicationImpl.java:709)

                at
com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(
WebApplicationImpl.java:667)

                at
com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(
WebApplicationImpl.java:658)

 

The API 2) resulted in HTTP 400 error (description of the error was "The
request sent by the client was syntactically incorrect ")

 

How ever when I try to post a single object as shown in the APIs 3) and
4) below , it works. The input xml in this case is

 

<?xml version="1.0" encoding="UTF-8" standalone="yes">

<alertPref><custID>cust1</custID>

                <includeAck>true</includeAck>

                <includeClear>false</includeClear>

                <severityLevel>4</severityLevel>

</alertPref>

 

 

3)

@POST @Path("echoXml")

@Consumes ("application/xml")

@Produces ("application/xml")

 

public AlertPrefInfo echoXML(JAXBElement<AlertPrefInfo> alertPref)
throws Exception {

            System.out.println(alertPref.getValue());

            AlertPrefInfo prefInfo = alertPref.getValue();

            return prefInfo;

      }

4)

      

      @POST @Path("echoXmlTwo")

      @Consumes ("application/xml")

      @Produces ("application/xml")

      

       public AlertPrefInfo echoXMLTwo(AlertPrefInfo alertPref) throws
Exception {

                  System.out.println(alertPref);

                  AlertPrefInfo prefInfo = alertPref;

                  return prefInfo;

      }

      

Do we need any special handling if we want the posted xml to be
converted into a list of objects ? Is this an issue with JAXB or Jersey
? Could you please let me know

 

Thank you,

Suchitha.