java.util.List es una interfaz y JAXB no puede manejar interfaces
-
08-07-2019 - |
Pregunta
Parece que recibo la siguiente excepción cuando intento 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ó bien hasta que cambié el tipo de retorno 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 he intentado eliminar @SOAPBinding y probar el valor predeterminado, pero se produce el mismo resultado.
Agradezco cualquier ayuda
UPDATE
Quiero anotar algo. Cambié toda la lista a ArrayList, y luego se compiló. La razón por la que digo compilado y no trabajado es porque se comporta de manera extraña. Me sale un objeto de tipo: RelationServiceStub.ArrayList pero el objeto no tiene métodos get o tampoco se comporta como una Lista. También traté de convertirlo en 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.
Solución
Según tengo entendido, no podrá procesar un List
simple a través de JAXB, ya que JAXB no tiene idea de cómo transformar eso en XML.
En su lugar, necesitará definir un tipo JAXB que contenga un List<RelationCanonical>
(lo llamaré Type1
), y otro para mantener una lista de esos tipos, a su vez (ya que está tratando con a 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 anotados JAXB, el procesador JAXB no tiene idea de qué marcado generar y, por lo tanto, falla.
--Editar:
Lo que quiero decir debería ser algo así:
@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 de JAXB .
Otros consejos
Si eso se adapta a su propósito, siempre puede definir una matriz como esta:
YourType[]
JAXB ciertamente puede descubrir qué es eso y debería poder usarlo inmediatamente del lado del cliente. También le recomendaría que lo hiciera de esa manera, ya que no debería poder modificar la matriz recuperada de un servidor a través de una Lista sino a través de los métodos proporcionados 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];
Puede usar " ArrayList " en lugar de " List "