You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@cxf.apache.org by "Bin Zhu (JIRA)" <ji...@apache.org> on 2014/08/28 13:06:57 UTC

[jira] [Created] (CXF-5976) incompatible with javax.xml.bind.JAXBElement error when using List> as resource method param

Bin Zhu created CXF-5976:
----------------------------

             Summary: incompatible with javax.xml.bind.JAXBElement error when using List<JAXBElement<Book>>  as resource method param
                 Key: CXF-5976
                 URL: https://issues.apache.org/jira/browse/CXF-5976
             Project: CXF
          Issue Type: Bug
          Components: JAX-RS, JAXB Databinding
    Affects Versions: 3.0.1
            Reporter: Bin Zhu


I'm migrating App to CXF3.0.1 from wink. There will be ClassCastException if I using JAXBElement List  (e.g. List<JAXBElement<Book>>) in the resource method.
e.g. 
    @Path("booklistjaxbelement")
    @Produces(MediaType.APPLICATION_XML)
    @Consumes(MediaType.APPLICATION_XML)
    @POST
    public List<JAXBElement<Book>> echoJAXBElementBookList(List<JAXBElement<Book>> bookElements) {
        List<JAXBElement<Book>> ret = new ArrayList<JAXBElement<Book>>();
        Author author = null;
        Author retAuthor = null;
        Book retBook = null;
        for (JAXBElement<Book> bookElement : bookElements) {
            author = bookElement.getValue().getAuthor();
            retAuthor = new Author();
            retAuthor.setFirstName("echo " + author.getFirstName());
            retAuthor.setLastName("echo " + author.getLastName());
            retBook = new Book();
            retBook.setAuthor(retAuthor);
            retBook.setTitle("echo " + bookElement.getValue().getTitle());
            JAXBElement<Book> element = new JAXBElement<Book>(new QName("book"), Book.class, retBook);
            ret.add(element);
        }
        return ret;
    }

Book is an JAXB bean like this:
@XmlRootElement
public class Book {

    private Author author;
    private String title;

    public Author getAuthor() {
        return author;
    }

    public void setAuthor(Author author) {
        this.author = author;
    }

    public String getTitle() {
        return title;
    }
....
Test client is like this:

   List<Book> source = getBookSource();
        Resource resource = client.resource(JAXB_BASE_URI + "/booklistjaxbelement");
        ClientResponse response =
                        resource.accept(MediaType.APPLICATION_XML).contentType(MediaType.APPLICATION_XML)
                                        .post(new GenericEntity<List<Book>>(source) {
                                              });
        List<Book> responseEntity = response.getEntity(new EntityType<List<Book>>() {
                        });
-------------------
Debugged CXF code and find in AbstractInvoker. performInvocation, it will invoke the method.invoke. The value of paramArray here is Book ArrayList and caused this issue.
This scenario is working in wink, but doesn't work here...
  

    protected Object performInvocation(Exchange exchange, final Object serviceObject, Method m,
                                       Object[] paramArray) throws Exception {
        paramArray = insertExchange(m, paramArray, exchange);
        if (LOG.isLoggable(Level.FINER)) {
            LOG.log(Level.FINER, "INVOKING_METHOD", new Object[] {serviceObject, 
                                                                  m,
                                                                  Arrays.asList(paramArray)});
        }
        return m.invoke(serviceObject, paramArray);
    }





--
This message was sent by Atlassian JIRA
(v6.2#6252)