tags:

views:

72

answers:

0

I have a really simple rest web service returning a list of questions. This code works as expected when the number of questions returned are greater than zero. But if the server returns an empty json array like [], JAXB creates a list with one question instance where all fields are set to null!

I'm new to both Jersey and JAXB so I don't know whether I haven't configured it correctly or whether this is a known problem. Any tips?

Client configuration:

 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("/questions/byGroupId/" + id).get(new GenericType<List<Question>>() {});
}

The Question class is just a simple pojo.