Ich habe einen wirklich einfachen Rest Web Service, der eine Liste von Fragen zurückgibt. Dieser Code funktioniert wie erwartet, wenn die Anzahl der zurückgegebenen Fragen größer als Null ist. Wenn der Server jedoch ein leeres json-Array wie [] zurückgibt, erstellt JAXB eine Liste mit einer Frageinstanz, in der alle Felder auf null gesetzt sind!Jersey/JAXB: Unmarshaling von leeren JSON-Array führt zu einer Liste mit einem Element, bei dem alle Felder auf Null gesetzt sind
Ich bin neu sowohl Jersey und JAXB, also weiß ich nicht, ob ich es nicht richtig konfiguriert habe oder ob dies ein bekanntes Problem ist. Irgendwelche Tipps?
Client-Konfiguration:
DefaultApacheHttpClientConfig config = new DefaultApacheHttpClientConfig();
config.getProperties().put(DefaultApacheHttpClientConfig.PROPERTY_HANDLE_COOKIES, true);
config.getClasses().add(JAXBContextResolver.class);
//config.getClasses().add(JacksonJsonProvider.class); // <- Jackson causes other problems
client = ApacheHttpClient.create(config);
JAXBContextResolver:
@Provider
public final class JAXBContextResolver implements ContextResolver<JAXBContext> {
private final JAXBContext context;
private final Set<Class> types;
private final Class[] cTypes = { Question.class };
public JAXBContextResolver() throws Exception {
this.types = new HashSet(Arrays.asList(cTypes));
this.context = new JSONJAXBContext(JSONConfiguration.natural().build(), cTypes);
}
@Override
public JAXBContext getContext(Class<?> objectType) {
return (types.contains(objectType)) ? context : null;
}
}
Client-Code:
public List<Question> getQuestionsByGroupId(int id) {
return digiRest.path("https://stackoverflow.com/questions/byGroupId/" + id).get(new GenericType<List<Question>>() {});
}
Die Frage Klasse ist nur eine einfache pojo.
ähnliches Problem hier. Gelöst (aber hacky): http://stackoverflow.com/questions/4197817 –