W jaki sposób JAXB zachować wartości zerowe podczas odbierania żądań JSON, która zawiera wartość "" lub "".JAX-RS Jersey JSON zachowaj wartość null, stosując adnotacje.
String: {"id":null,"fname":"John","region":""}
powraca obiektu:
Person {
Integer id = 0
String fname = "John"
Region regions = 0 }
chciałbym go zwróci null zamiast 0
Oto co mam do tej pory:
@Provider
public class JAXBContextResolver implements ContextResolver<JAXBContext> {
private JAXBContext context;
private Class<?>[] types = {Person.class};
public JAXBContextResolver() throws Exception {
this.context = new JSONJAXBContext(JSONConfiguration.natural().build(), types);
}
public JAXBContext getContext(Class<?> objectType) {
for (Class<?> c : types) {
if (c.equals(objectType)) {
return context;
}
}
return null;
}
}
Person.class jest opatrzony komentarzem @XmlRootElement
Próbowałem już szukać w adnotacje Jacksona, ale nie odniosły sukcesu.
Ten sam problem tutaj. Ktoś znalazł rozwiązanie tego problemu. ObjectMapper wydaje się być ignorowany przez Jersey – keatch
Ten sam problem, jak również. Próbowałem także używać ObjectMapper i Jersey również go nie podniósł.Nie jestem pewien, czy użycie ObjectMappera jest najprostszym rozwiązaniem ... – Oleksi
Mój podobny problem został rozwiązany. Możesz sprawdzić, czy to pomaga ... http: //stackoverflow.com/questions/10420641/include-null-elements-in-json-output-of-jersey-restful-api-with-jaxb – Oleksi