2012-07-03 24 views
1

如何在編組字符串時將'null'打印爲字段值?JAXB null在封送處理過程中改爲空字符串

示例:error和error_code是字符串,我想使用'null'作爲值,表示服務器端沒有值/錯誤發生。

{ 
    "error_code": null, 
    "error": null 
} 

今天,我必須使用空值,從而使「ERROR_CODE」或「錯誤」,這些領域一般分爲JSON,如果他們沒有明確初始化爲this.errorCode = StringUtils.EMPTY; 所以今天,我有下一個JSON:

{ 
    "error_code": "", 
    "error": "" 
} 

這是怎麼看起來在代碼:

@XmlRootElement() 
@XmlAccessorType(XmlAccessType.FIELD) 
public class Response 
{ 
     @SuppressWarnings("unused") 
     private static final Logger log = LoggerFactory.getLogger(Response.class); 

     public static final String ERROR_FIELD_NAME = "error"; 
     public static final String ERROR_CODE_FIELD_NAME = "error_code"; 

     // @XmlJavaTypeAdapter(CafsResponse.EmptyStringAdapter.class) 
     @XmlElement(name = Response.ERROR_CODE_FIELD_NAME) 
     private String errorCode; 

     // @XmlJavaTypeAdapter(CafsResponse.EmptyStringAdapter.class) 
     @XmlElement(name = Response.ERROR_FIELD_NAME) 
     private String errorMessage; 

    // Empty Constructor 
    public Response() 
    { 
      this.errorCode = StringUtils.EMPTY; // explicit initialization, otherwise error_code will not appear as part of json, how to fix this this ? 
      this.errorMessage = StringUtils.EMPTY; 
    } 

等等

// Empty Constructor 
public Response() 
{ 
     this.errorCode = null; // this variant dosn't work either, and error_code again didn't get to json 
     this.errorMessage = null; 
} 

見,@XmlJavaTypeAdapter,我想這可能會幫助我 - 但沒有:)

而不是空值,我得到「空」作爲字符串。

if (StringUtils.isEmpty(str)) 
{ 
    return null; 
} 
return str; 

{ 
    "error_code": "null", // this is not whta i wanted to get. 
    "error": "null" 
} 

對此有何幫助? - 問我有沒有不清楚的地方。

名單:

/** 
* Empty string Adapter specifying how we want to represent empty strings 
* (if string is empty - treat it as null during marhsaling) 
* 
*/ 
@SuppressWarnings("unused") 
private static class EmptyStringAdapter extends XmlAdapter<String, String> 
{ 

     @Override 
     public String unmarshal(String str) throws Exception 
     { 
       return str; 
     } 


     @Override 
     public String marshal(String str) throws Exception 
     { 
       if (StringUtils.isEmpty(str)) 
       { 
         return null; 
       } 
       return str; 
     } 

} 
+1

以下應該有所幫助:http://blog.bdoughan.com/2012/04/binding-to-json-xml -handling-null.html –

+0

@BlaiseDoughan 基本上我有一些POJO字段,用@XmlElement元素定義如下: @XmlElement(nillable = true)private String error_code;我得到正確的值: 然而,當我使用的應用程序/ JSON作爲輸出我得到: ' 」狀態「:{ 」@nil「: 」真正的「 } ' 這似乎不正確。根據JSON規範,它更像: 「error_code」:null 如何解決此問題? – IgorA

+0

我已經添加了一個答案,演示如何使用MOXY作爲您的JSON提供程序來實現您的用例。 –

回答

0

注:我是EclipseLink JAXB (MOXy)鉛和JAXB (JSR-222)專家小組的成員。

您可以使用MOXy作爲您的JSON提供程序來支持此用例。下面是一個例子:

響應

莫西將Marshal屬性標記@XmlElement(nillable=true)你正在尋找 (參見:http://blog.bdoughan.com/2012/04/binding-to-json-xml-handling-null.html)表示。

package forum11319741; 

import javax.xml.bind.annotation.*; 

@XmlRootElement 
@XmlAccessorType(XmlAccessType.FIELD) 
public class Response { 

     public static final String ERROR_FIELD_NAME = "error"; 
     public static final String ERROR_CODE_FIELD_NAME = "error_code"; 

     @XmlElement(name = Response.ERROR_CODE_FIELD_NAME, nillable = true) 
     private String errorCode; 

     @XmlElement(name = Response.ERROR_FIELD_NAME, nillable = true) 
     private String errorMessage; 

} 

jaxb。性能

要使用莫西爲您的JAXB提供者,你需要包括在同一個包中的以下項域模型稱爲jaxb.properties文件(參見:http://blog.bdoughan.com/2011/05/specifying-eclipselink-moxy-as-your.html):

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

演示

package forum11319741; 

import javax.xml.bind.*; 

public class Demo { 

    public static void main(String[] args) throws Exception { 
     JAXBContext jc = JAXBContext.newInstance(Response.class); 

     Marshaller marshaller = jc.createMarshaller(); 
     marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); 
     marshaller.setProperty("eclipselink.media-type", "application/json"); 
     marshaller.setProperty("eclipselink.json.include-root", false); 

     Response response = new Response(); 
     marshaller.marshal(response, System.out); 
    } 

} 

輸出

{ 
    "error_code" : null, 
    "error" : null 
} 

莫西和JAX-RS

可以使用MOXyJsonProvider類,使莫西在你的JAX-RS應用程序的JSON提供商(請參閱:http://blog.bdoughan.com/2012/05/moxy-as-your-jax-rs-json-provider.html)。

package org.example; 

import java.util.*; 
import javax.ws.rs.core.Application; 
import org.eclipse.persistence.jaxb.rs.MOXyJsonProvider; 

public class CustomerApplication extends Application { 

    @Override 
    public Set<Class<?>> getClasses() { 
     HashSet<Class<?>> set = new HashSet<Class<?>>(2); 
     set.add(MOXyJsonProvider.class); 
     set.add(CustomerService.class); 
     return set; 
    } 

} 

更多信息