2012-01-05 10 views
6

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.

+0

Ten sam problem tutaj. Ktoś znalazł rozwiązanie tego problemu. ObjectMapper wydaje się być ignorowany przez Jersey – keatch

+0

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

+1

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

Odpowiedz

3

Uwaga: Jestem EclipseLink JAXB (MOXy) ołowiu i członkiem grupy JAXB (JSR-222) ekspertów.

Poniżej znajduje się przykład tego, jak można to zrobić z MOXy.

Osoba

Moxy będzie unmarshal obiekt Person jak chcesz bez podania żadnych metadanych. Aby wyjściowy plik JSON zawierał wartości puste, można użyć adnotacji @XmlElement(nillable=true) (patrz Binding to JSON & XML - Handling Null).

package forum8748537; 

import javax.xml.bind.annotation.*; 

@XmlAccessorType(XmlAccessType.FIELD) 
public class Person { 

    @XmlElement(nillable=true) 
    Integer id; 

    @XmlElement(nillable=true) 
    String fname; 

    @XmlElement(nillable=true) 
    Region regions; 

} 

jaxb.properties

Aby określić Moxy jako JAXB (JSR-222) z operatorem trzeba dodać plik o nazwie jaxb.properties w tym samym opakowaniu w klasach domeny z następującego wpisu (patrz Specifying EclipseLink MOXy as Your JAXB Provider).

javax.xml.bind.context.factory=org.eclipse.persistence.jaxb.JAXBContextFactory 

Demo

package forum8748537; 

import java.io.StringReader; 
import java.util.*; 

import javax.xml.bind.*; 
import javax.xml.transform.stream.StreamSource; 

public class Demo { 

    public static void main(String[] args) throws Exception { 
     Map<String, Object> properties = new HashMap<String, Object>(2); 
     properties.put("eclipselink.media-type", "application/json"); 
     properties.put("eclipselink.json.include-root", false); 
     JAXBContext jc = JAXBContext.newInstance(new Class[] {Person.class}, properties); 

     StringReader json = new StringReader("{\"id\":null,\"fname\":\"John\",\"region\":\"\"}"); 
     Unmarshaller unmarshaller = jc.createUnmarshaller(); 
     Person person = unmarshaller.unmarshal(new StreamSource(json), Person.class).getValue(); 

     Marshaller marshaller = jc.createMarshaller(); 
     marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); 
     marshaller.marshal(person, System.out); 
    } 

} 

Wyjście

{ 
    "id" : null, 
    "fname" : "John", 
    "regions" : null 
} 

Korzystanie Moxy w JAX-RS Aplikacja

Dla n przykład za pomocą moxy w aplikacji JAX-RS patrz

0

jako JSON B JSR-367 (JAX-RS 2,1 JSR-370), następujące adnotacja działa zamiast @XmlElement.

@JsonbProperty(nillable = true) 
Powiązane problemy