example java web-services java-ee jaxb jbossws

example - java.util.List es una interfaz, y JAXB no puede manejar interfaces



ws security java client example (4)

Puede usar "ArrayList" en lugar de "Lista" interna

Me pareció que recibí la siguiente excepción al intentar implementar mi aplicación:

Caused by: com.sun.xml.bind.v2.runtime.IllegalAnnotationsException: 2 counts of IllegalAnnotationExceptions java.util.List is an interface, and JAXB can''t handle interfaces. this problem is related to the following location: at java.util.List at private java.util.List foobar.alkohol.register.webservice.jaxws.GetRelationsFromPersonResponse._return at foobar.alkohol.register.webservice.jaxws.GetRelationsFromPersonResponse java.util.List does not have a no-arg default constructor. this problem is related to the following location: at java.util.List at private java.util.List foobar.alkohol.register.webservice.jaxws.GetRelationsFromPersonResponse._return at foobar.alkohol.register.webservice.jaxws.GetRelationsFromPersonResponse


Mi código funcionó muy bien hasta que cambié el tipo de devolución de List a List <List <RelationCanonical >>

Aquí está el servicio web parcial:

@Name("relationService") @Stateless @WebService(name = "RelationService", serviceName = "RelationService") @SOAPBinding(style = SOAPBinding.Style.DOCUMENT, use = SOAPBinding.Use.LITERAL, parameterStyle = SOAPBinding.ParameterStyle.WRAPPED) public class RelationService implements RelationServiceLocal { private boolean login(String username, String password) { Identity.instance().setUsername(username); Identity.instance().setPassword(password); Identity.instance().login(); return Identity.instance().isLoggedIn(); } private boolean logout() { Identity.instance().logout(); return !Identity.instance().isLoggedIn(); } @WebMethod public List<List<RelationCanonical>> getRelationsFromPerson(@WebParam(name = "username") String username, @WebParam(name = "password") String password, @WebParam(name = "foedselsnummer") String... foedselsnummer) { ...... ...... ...... }


También lo intenté eliminando el @SOAPBinding y el intento por defecto, pero ocurre el mismo resultado. Apreciar cualquier ayuda

ACTUALIZAR

Quiero notar algo. Cambié todas las listas a ArrayList, y luego las compilé. La razón por la que digo compilada y no funciona es porque se comporta de manera extraña. Obtengo un objeto de tipo: RelationServiceStub.ArrayList pero el objeto no tiene métodos get o no se comporta como una lista. También traté de lanzarlo a una lista, pero eso no funcionó.

Tenga en cuenta que esto es después de haber usado Axis 2 y wsdl2java Entonces sí, ahora se compila, pero no sé cómo sacar los datos.


Según entiendo, no podrá procesar una List simple a través de JAXB, ya que JAXB no tiene idea de cómo transformar eso en XML.

En su lugar, deberá definir un tipo JAXB que contenga una List<RelationCanonical> (lo llamaré Type1 ) y otra para mantener una lista de esos tipos, a su vez (ya que se trata de una List<List<...>> ; Llamaré a este tipo Type2 ).

El resultado podría ser una salida XML como esta:

<Type2 ...> <Type1 ...> <RelationCanonical ...> ... </RelationCanonical> <RelationCanonical ...> ... </RelationCanonical> ... </Type1> <Type1> <RelationCanonical ...> ... </RelationCanonical> <RelationCanonical ...> ... </RelationCanonical> ... </Type1> ... </Type2>

Sin los dos tipos adjuntos con anotaciones JAXB, el procesador JAXB no tiene idea de qué marcado generar y, por lo tanto, falla.

--Editar:

Lo que quiero decir debería parecerse a esto:

@XmlType public class Type1{ private List<RelationCanonical> relations; @XmlElement public List<RelationCanonical> getRelations(){ return this.relations; } public void setRelations(List<RelationCanonical> relations){ this.relations = relations; } }

y

@XmlRootElement public class Type2{ private List<Type1> type1s; @XmlElement public List<Type1> getType1s(){ return this.type1s; } public void setType1s(List<Type1> type1s){ this.type1s= type1s; } }

También debe consultar la sección JAXB en el tutorial J5EE y la Guía no oficial JAXB .


Si eso se adapta a su propósito, siempre puede definir una matriz como esta:

YourType[]

JAXB ciertamente puede descubrir qué es eso y usted debería ser capaz de usarlo inmediatamente desde el lado del cliente. También te recomendaría que lo hicieras de esa manera, ya que no deberías poder modificar la matriz recuperada de un servidor a través de una Lista, sino a través de los métodos provistos por el servicio web.


Si quieres hacer esto para cualquier clase.

return items.size() > 0 ? items.toArray((Object[]) Array.newInstance( items.get(0).getClass(), 0)) : new Object[0];