2012-01-05 7 views
6

जेएसओबी स्टिंग प्राप्त करते समय जेएक्सबी कैसे नल को संरक्षित करता है जिसमें शून्य या "" मान होता है।जेएक्स-आरएस जर्सी JSON एनोटेशन का उपयोग करके शून्य को सुरक्षित रखता है

स्ट्रिंग: {"id":null,"fname":"John","region":""}

रिटर्न वस्तु:

Person { 
    Integer id = 0 
    String fname = "John" 
    Region regions = 0 } 

मैं इसे 0

यहाँ की अशक्त बजाय वापस जाने के लिए चाहते हैं कि मैं क्या अब तक है:

@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 को @XmlRootElement के साथ एनोटेट किया गया है मैंने कोशिश करने की कोशिश की है जैक्सन एनोटेशन में लेकिन कोई सफलता नहीं मिली है।

+0

यहां वही समस्या है। किसी को इस समस्या का समाधान मिला। ऑब्जेक्टमैपर को जर्सी – keatch

+0

द्वारा भी वही समस्या से अनदेखा किया जा रहा है। मैंने ऑब्जेक्टमैपर और जर्सी का उपयोग करने का भी प्रयास किया और इसे भी चुनने में असफल रहा।मुझे यकीन नहीं है कि ऑब्जेक्टमैपर का उपयोग करना सबसे आसान समाधान है ... – Oleksi

+1

मेरा इसी तरह का मुद्दा हल हो गया है। आप देख सकते हैं कि यह आपकी मदद करता है ... http: //stackoverflow.com/questions/10420641/including-null-elements-in-json-output-of-jersey-restful-api-with-jaxb – Oleksi

उत्तर

3

नोट: मैं EclipseLink JAXB (MOXy) लीड और JAXB (JSR-222) विशेषज्ञ समूह का सदस्य हूं।

नीचे एक उदाहरण है कि यह MOXy के साथ कैसे किया जा सकता है।

व्यक्ति

MOXY Person वस्तु unmarshal जाएगा के रूप में आप किसी भी मेटाडाटा निर्दिष्ट किए बिना चाहते हैं। आउटपुट जेएसओएन में शून्य मान होने के लिए, आप @XmlElement(nillable=true) एनोटेशन का उपयोग कर सकते हैं (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

(JSR-222) प्रदाता आप निम्न लिखित प्रविष्टि के साथ अपने डोमेन कक्षाओं के रूप में ही पैकेज में एक फ़ाइल jaxb.properties बुलाया जोड़ने की जरूरत है अपने JAXB के रूप में MOXY निर्दिष्ट करने के लिए (देखें Specifying EclipseLink MOXy as Your JAXB Provider)।

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

डेमो

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); 
    } 

} 

आउटपुट

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

एक JAX-आरएस आवेदन

में MOXY का उपयोग करते हुए एक के लिए n एक JAX-आरएस आवेदन में MOXY का उपयोग करने का उदाहरण देखें:

0

JSON-बी JSR-367 (JAX-आरएस 2.1 JSR-370) के रूप में, निम्नलिखित @XmlElement के बजाय एनोटेशन काम करता है।

@JsonbProperty(nillable = true) 
संबंधित मुद्दे