You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@cxf.apache.org by oceanz <oc...@yahoo.com> on 2010/10/05 18:52:20 UTC

Help required with apache cxf multiparts

Hi,

I am trying to pass a list of objects using muliparts. Below is the code:

UserService userService = (UserService)
JAXRSClientFactory.create("http://localhost:8080/sitexchange/services",UserService.class);
userService.addDevice3(list);


	@Path("user/site/plant/device/addDevice1")
	@POST
	@Produces("text/xml")
	@Consumes("multipart/form-data")
	Device addDevice1(@Multipart("")List<Device> device);

But I get an exception as below. Can anyone guide me as to what am I
missing.

org.apache.cxf.interceptor.Fault
	at
org.apache.cxf.jaxrs.client.ClientProxyImpl$BodyWriter.handleMessage(ClientProxyImpl.java:523)
	at
org.apache.cxf.phase.PhaseInterceptorChain.doIntercept(PhaseInterceptorChain.java:243)
	at
org.apache.cxf.jaxrs.client.ClientProxyImpl.doChainedInvocation(ClientProxyImpl.java:438)
	at
org.apache.cxf.jaxrs.client.ClientProxyImpl.invoke(ClientProxyImpl.java:177)
	at $Proxy20.addDevice3(Unknown Source)
	at com.esei.services.wsTest.main(wsTest.java:51)
Caused by: javax.ws.rs.WebApplicationException
	at
org.apache.cxf.jaxrs.client.AbstractClient.reportNoMessageHandler(AbstractClient.java:487)
	at
org.apache.cxf.jaxrs.client.AbstractClient.writeBody(AbstractClient.java:401)
	at
org.apache.cxf.jaxrs.client.ClientProxyImpl$BodyWriter.handleMessage(ClientProxyImpl.java:514)
	... 5 more
Exception in thread "main" org.apache.cxf.interceptor.Fault
	at
org.apache.cxf.jaxrs.client.ClientProxyImpl$BodyWriter.handleMessage(ClientProxyImpl.java:523)
	at
org.apache.cxf.phase.PhaseInterceptorChain.doIntercept(PhaseInterceptorChain.java:243)
	at
org.apache.cxf.jaxrs.client.ClientProxyImpl.doChainedInvocation(ClientProxyImpl.java:438)
	at
org.apache.cxf.jaxrs.client.ClientProxyImpl.invoke(ClientProxyImpl.java:177)
	at $Proxy20.addDevice3(Unknown Source)
	at com.esei.services.wsTest.main(wsTest.java:51)
Caused by: javax.ws.rs.WebApplicationException
	at
org.apache.cxf.jaxrs.client.AbstractClient.reportNoMessageHandler(AbstractClient.java:487)
	at
org.apache.cxf.jaxrs.client.AbstractClient.writeBody(AbstractClient.java:401)
	at
org.apache.cxf.jaxrs.client.ClientProxyImpl$BodyWriter.handleMessage(ClientProxyImpl.java:514)
-- 
View this message in context: http://cxf.547215.n5.nabble.com/Help-required-with-apache-cxf-multiparts-tp3199975p3199975.html
Sent from the cxf-user mailing list archive at Nabble.com.

Re: Help required with apache cxf multiparts

Posted by Sergey Beryozkin <sb...@gmail.com>.
Just FYI - explicitly setting a header for proxies in this case is now
optional in 2.2.11/2.3 - it depends on whether you can afford modifying
@Consumes to include a type parameter or not

cheers, Sergey

On Wed, Oct 6, 2010 at 5:29 AM, oceanz <oc...@yahoo.com> wrote:

>
> Thanks a lot Serge.
> The         WebClient.client(client).header("Content-Type",
> "multipart/mixed;type=application/xml");
> did the trick.
> --
> View this message in context:
> http://cxf.547215.n5.nabble.com/Help-required-with-apache-cxf-multiparts-tp3199975p3200774.html
> Sent from the cxf-user mailing list archive at Nabble.com.
>

Re: Help required with apache cxf multiparts

Posted by oceanz <oc...@yahoo.com>.
Thanks a lot Serge.
The         WebClient.client(client).header("Content-Type", 
"multipart/mixed;type=application/xml"); 
did the trick.
-- 
View this message in context: http://cxf.547215.n5.nabble.com/Help-required-with-apache-cxf-multiparts-tp3199975p3200774.html
Sent from the cxf-user mailing list archive at Nabble.com.

Re: Help required with apache cxf multiparts

Posted by Sergey Beryozkin <sb...@gmail.com>.
Hi Josh

thanks for this tip, but my question is to do with you referring to
collections of JAXB beans not being supported.
I'd appreciate if you could send me an example of such a collection.
Explicit collection have been supported for a while and I recall users
confirming that it worked for them, so if there's some issue there lurking
then I'd like to get it fixed :-)

thanks, Sergey

On Tue, Oct 5, 2010 at 7:24 PM, Josh Holtzman <jh...@berkeley.edu>wrote:

> This part of the stack trace:
>
> Caused by: javax.ws.rs.WebApplicationException at
>
> org.apache.cxf.jaxrs.client.AbstractClient.reportNoMessageHandler(AbstractClient.java:487)
>
> looks like there's no message body writer.  I've run into this kind of
> problem when trying to return Collections, even collections of JAXB
> elements, from JAX-RS endpoints.  I usually solve it by wrapping the list
> in
> a root class, and passing the wrapper rather than the Collection, like
> this:
>
> @XmlRootElement(name="deviceList")
> public class DeviceList {
>  @XmlElement
>  protected List<Device> devices;
> }
>
> Perhaps this might help in your case too, switching from
> addDevice(List<Device> device) to addDevice(DeviceList deviceList).
>
> Josh
>
> On Tue, Oct 5, 2010 at 8:00 PM, oceanz <oc...@yahoo.com> wrote:
>
> >
> > I changed it to:
> >         @Path("user/site/plant/device/addDevice1")
> >        @POST
> >        @Produces("text/xml")
> >         @Consumes("multipart/form-data;type=text/xml")
> >        Device addDevice(List<Device> device);
> >
> > But it still doesn't work.
> > Same exception:Caused by: javax.ws.rs.WebApplicationException
> >         at
> >
> >
> org.apache.cxf.jaxrs.client.AbstractClient.reportNoMessageHandler(AbstractClient.java:487)
> >        at
> >
> >
> org.apache.cxf.jaxrs.client.AbstractClient.writeBody(AbstractClient.java:401)
> >        at
> >
> >
> org.apache.cxf.jaxrs.client.ClientProxyImpl$BodyWriter.handleMessage(ClientProxyImpl.java:514)
> >        ... 5 more
> >
> > Device is a JAXB Bean.:
> > @Entity(name="device")
> > @XmlRootElement(name="device")
> > @XmlAccessorType(XmlAccessType.FIELD)
> > public class Device extends DomainEntity {
> >
> >        /**
> >         *
> >         */
> >        private static final long serialVersionUID = 1L;
> >
> >        public Device() {}
> >
> >
> >        @XmlAttribute(name="deviceName")
> >        private String deviceName;
> >
> >        @XmlAttribute(name="deviceType")
> >        private String deviceType;
> >
> >        @XmlAttribute(name="modelNumber")
> >        private String modelNumber;
> >
> >        @XmlAttribute(name="serialNumber")
> >        private String serialNumber;
> >        ..........
> > }
> >
> > Who do I need to do next? Desparate to get this working.
> > Thanks for all your help.
> > --
> > View this message in context:
> >
> http://cxf.547215.n5.nabble.com/Help-required-with-apache-cxf-multiparts-tp3199975p3200103.html
> > Sent from the cxf-user mailing list archive at Nabble.com.
> >
>

Re: Help required with apache cxf multiparts

Posted by Sergey Beryozkin <sb...@gmail.com>.
Hi

Here is the test involving WebClient :

@Test
    public void testAddGetJaxbBooksWebClient() throws Exception {
        String address = "http://localhost:" + PORT +
"/bookstore/books/jaxbonly";
        WebClient client = WebClient.create(address);


client.type("multipart/mixed;type=application/xml").accept("multipart/mixed");

        Book b = new Book("jaxb", 1L);
        Book b2 = new Book("jaxb2", 2L);
        List<Book> books = new ArrayList<Book>();
        books.add(b);
        books.add(b2);
        Collection<? extends Book> coll = client.postAndGetCollection(books,
Book.class);
        List<Book> result = new ArrayList<Book>(coll);
        Book jaxb = result.get(0);
        assertEquals("jaxb", jaxb.getName());
        assertEquals(1L, jaxb.getId());
        Book jaxb2 = result.get(1);
        assertEquals("jaxb2", jaxb2.getName());
        assertEquals(2L, jaxb2.getId());
    }

where the server code is :

@POST
    @Path("/books/jaxbonly")
    @Consumes("multipart/mixed")
    @Produces("multipart/mixed;type=text/xml")
    public List<Book> addBooks(List<Book> books) {
        List<Book> books2 = new ArrayList<Book>();
        books2.addAll(books);
        return books2;
    }

I've just added a proxy test :

@Test
    public void testAddCollectionOfBooksWithProxy() {
        String address = "http://localhost:" + 8080;
        MultipartStore client = JAXRSClientFactory.create(address,
MultipartStore.class);

        WebClient.client(client).header("Content-Type",
"multipart/mixed;type=application/xml");

        List<Book> books = new ArrayList<Book>();
        books.add(new Book("CXF 1", 1L));
        books.add(new Book("CXF 2", 2L));
        List<Book> books2 = client.addBooks(books);
        assertNotSame(books, books2);
        assertEquals(2, books2.size());
        assertEquals(books.get(0).getId(), books2.get(0).getId());
        assertEquals(books.get(1).getId(), books2.get(1).getId());
    }

Note that a proxy "helps" the MultipartProvider to figure out the type of
individual collection parts.
@Consumes("multipart/mixed;type=application/xml") should work too - it works
in my local snapshot - but I'll need to run more tests first before
committing a minor fix to MediaType handler to do with handling complex
types usually contained in multipart requests.

thanks, Sergey

On Tue, Oct 5, 2010 at 7:40 PM, oceanz <oc...@yahoo.com> wrote:

>
> Josh,
>
> I tried to wrap it up.But still the same problem.Any furthur pointers?
>
> Thanks.
> --
> View this message in context:
> http://cxf.547215.n5.nabble.com/Help-required-with-apache-cxf-multiparts-tp3199975p3200207.html
> Sent from the cxf-user mailing list archive at Nabble.com.
>

Re: Help required with apache cxf multiparts

Posted by oceanz <oc...@yahoo.com>.
Josh,

I tried to wrap it up.But still the same problem.Any furthur pointers?

Thanks.
-- 
View this message in context: http://cxf.547215.n5.nabble.com/Help-required-with-apache-cxf-multiparts-tp3199975p3200207.html
Sent from the cxf-user mailing list archive at Nabble.com.

Re: Help required with apache cxf multiparts

Posted by Josh Holtzman <jh...@berkeley.edu>.
This part of the stack trace:

Caused by: javax.ws.rs.WebApplicationException at
org.apache.cxf.jaxrs.client.AbstractClient.reportNoMessageHandler(AbstractClient.java:487)

looks like there's no message body writer.  I've run into this kind of
problem when trying to return Collections, even collections of JAXB
elements, from JAX-RS endpoints.  I usually solve it by wrapping the list in
a root class, and passing the wrapper rather than the Collection, like this:

@XmlRootElement(name="deviceList")
public class DeviceList {
  @XmlElement
  protected List<Device> devices;
}

Perhaps this might help in your case too, switching from
addDevice(List<Device> device) to addDevice(DeviceList deviceList).

Josh

On Tue, Oct 5, 2010 at 8:00 PM, oceanz <oc...@yahoo.com> wrote:

>
> I changed it to:
>         @Path("user/site/plant/device/addDevice1")
>        @POST
>        @Produces("text/xml")
>         @Consumes("multipart/form-data;type=text/xml")
>        Device addDevice(List<Device> device);
>
> But it still doesn't work.
> Same exception:Caused by: javax.ws.rs.WebApplicationException
>         at
>
> org.apache.cxf.jaxrs.client.AbstractClient.reportNoMessageHandler(AbstractClient.java:487)
>        at
>
> org.apache.cxf.jaxrs.client.AbstractClient.writeBody(AbstractClient.java:401)
>        at
>
> org.apache.cxf.jaxrs.client.ClientProxyImpl$BodyWriter.handleMessage(ClientProxyImpl.java:514)
>        ... 5 more
>
> Device is a JAXB Bean.:
> @Entity(name="device")
> @XmlRootElement(name="device")
> @XmlAccessorType(XmlAccessType.FIELD)
> public class Device extends DomainEntity {
>
>        /**
>         *
>         */
>        private static final long serialVersionUID = 1L;
>
>        public Device() {}
>
>
>        @XmlAttribute(name="deviceName")
>        private String deviceName;
>
>        @XmlAttribute(name="deviceType")
>        private String deviceType;
>
>        @XmlAttribute(name="modelNumber")
>        private String modelNumber;
>
>        @XmlAttribute(name="serialNumber")
>        private String serialNumber;
>        ..........
> }
>
> Who do I need to do next? Desparate to get this working.
> Thanks for all your help.
> --
> View this message in context:
> http://cxf.547215.n5.nabble.com/Help-required-with-apache-cxf-multiparts-tp3199975p3200103.html
> Sent from the cxf-user mailing list archive at Nabble.com.
>

Re: Help required with apache cxf multiparts

Posted by oceanz <oc...@yahoo.com>.
I changed it to:
	@Path("user/site/plant/device/addDevice1")
	@POST
	@Produces("text/xml")
	@Consumes("multipart/form-data;type=text/xml") 
	Device addDevice(List<Device> device);

But it still doesn't work.
Same exception:Caused by: javax.ws.rs.WebApplicationException
	at
org.apache.cxf.jaxrs.client.AbstractClient.reportNoMessageHandler(AbstractClient.java:487)
	at
org.apache.cxf.jaxrs.client.AbstractClient.writeBody(AbstractClient.java:401)
	at
org.apache.cxf.jaxrs.client.ClientProxyImpl$BodyWriter.handleMessage(ClientProxyImpl.java:514)
	... 5 more

Device is a JAXB Bean.:
@Entity(name="device")
@XmlRootElement(name="device")
@XmlAccessorType(XmlAccessType.FIELD)
public class Device extends DomainEntity {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	public Device() {}
	
	
	@XmlAttribute(name="deviceName")
	private String deviceName;
	
	@XmlAttribute(name="deviceType")
	private String deviceType;
	
	@XmlAttribute(name="modelNumber")
	private String modelNumber;
	
	@XmlAttribute(name="serialNumber")
	private String serialNumber;
	..........
}

Who do I need to do next? Desparate to get this working.
Thanks for all your help.
-- 
View this message in context: http://cxf.547215.n5.nabble.com/Help-required-with-apache-cxf-multiparts-tp3199975p3200103.html
Sent from the cxf-user mailing list archive at Nabble.com.

Re: Help required with apache cxf multiparts

Posted by Sergey Beryozkin <sb...@gmail.com>.
Hi

Perhaps no provider is found for Device given that you;re passing an
explicit collection ? Is device a JAXB bean ?
Additionally, you may want  to drop an empty @Multipart - it is meant to
identify 'bind' a given method parameter to a multi-part with a matching
Content-Id and optionally a matching content type...

If it is a jaxb bean and still does not work then try

@Consumes("multipart/form-data;type=text/xml")

let me know please if it works

cheers, Sergey

On Tue, Oct 5, 2010 at 5:52 PM, oceanz <oc...@yahoo.com> wrote:

>
> Hi,
>
> I am trying to pass a list of objects using muliparts. Below is the code:
>
> UserService userService = (UserService)
> JAXRSClientFactory.create("http://localhost:8080/sitexchange/services
> ",UserService.class);
> userService.addDevice3(list);
>
>
>        @Path("user/site/plant/device/addDevice1")
>        @POST
>        @Produces("text/xml")
>        @Consumes("multipart/form-data")
>        Device addDevice1(@Multipart("")List<Device> device);
>
> But I get an exception as below. Can anyone guide me as to what am I
> missing.
>
> org.apache.cxf.interceptor.Fault
>        at
>
> org.apache.cxf.jaxrs.client.ClientProxyImpl$BodyWriter.handleMessage(ClientProxyImpl.java:523)
>        at
>
> org.apache.cxf.phase.PhaseInterceptorChain.doIntercept(PhaseInterceptorChain.java:243)
>        at
>
> org.apache.cxf.jaxrs.client.ClientProxyImpl.doChainedInvocation(ClientProxyImpl.java:438)
>        at
>
> org.apache.cxf.jaxrs.client.ClientProxyImpl.invoke(ClientProxyImpl.java:177)
>        at $Proxy20.addDevice3(Unknown Source)
>        at com.esei.services.wsTest.main(wsTest.java:51)
> Caused by: javax.ws.rs.WebApplicationException
>        at
>
> org.apache.cxf.jaxrs.client.AbstractClient.reportNoMessageHandler(AbstractClient.java:487)
>        at
>
> org.apache.cxf.jaxrs.client.AbstractClient.writeBody(AbstractClient.java:401)
>        at
>
> org.apache.cxf.jaxrs.client.ClientProxyImpl$BodyWriter.handleMessage(ClientProxyImpl.java:514)
>        ... 5 more
> Exception in thread "main" org.apache.cxf.interceptor.Fault
>        at
>
> org.apache.cxf.jaxrs.client.ClientProxyImpl$BodyWriter.handleMessage(ClientProxyImpl.java:523)
>        at
>
> org.apache.cxf.phase.PhaseInterceptorChain.doIntercept(PhaseInterceptorChain.java:243)
>        at
>
> org.apache.cxf.jaxrs.client.ClientProxyImpl.doChainedInvocation(ClientProxyImpl.java:438)
>        at
>
> org.apache.cxf.jaxrs.client.ClientProxyImpl.invoke(ClientProxyImpl.java:177)
>        at $Proxy20.addDevice3(Unknown Source)
>        at com.esei.services.wsTest.main(wsTest.java:51)
> Caused by: javax.ws.rs.WebApplicationException
>        at
>
> org.apache.cxf.jaxrs.client.AbstractClient.reportNoMessageHandler(AbstractClient.java:487)
>        at
>
> org.apache.cxf.jaxrs.client.AbstractClient.writeBody(AbstractClient.java:401)
>        at
>
> org.apache.cxf.jaxrs.client.ClientProxyImpl$BodyWriter.handleMessage(ClientProxyImpl.java:514)
> --
> View this message in context:
> http://cxf.547215.n5.nabble.com/Help-required-with-apache-cxf-multiparts-tp3199975p3199975.html
> Sent from the cxf-user mailing list archive at Nabble.com.
>