2014-10-31 76 views
1

使用Oracle RightNow公司,新澤西州1.18,JAX-WS,JDeveloper的11.1.1.7.1澤西空值呈現JAXB對象(從Web服務),

進出口創造一個REST接口,我們RightNow的實例和Im撞上問題與空值。

我通過JAX-WS訪問RightNow,並用JAXB對象生成了一個靜態代理。在過去,我已經能夠簡單地將JAXB對象返回給Jersey,並且它變得可愛..但是,Rightnow Im獲得大量額外(null)字段呈現。在不同的系統上(Oracle SalesCloud),這些字段不會被渲染默認..

另外值得注意的是我不會返回對象的原始列表(事件是我的例子),但我用我自己的類包裝列表,所以我可以把@XMLRootElement註釋。

我的包裝看起來像這樣

@XmlRootElement 
@XmlAccessorType(XmlAccessType.FIELD) 
@JsonSerialize(include=JsonSerialize.Inclusion.NON_NULL) // tried this doesnt work 
public class RESTIncidents { 
    private List<Incident> incidents; 
    public void setIncidents(List<Incident> incidents) { 
    this.incidents = incidents; 
    } 
} 

,結果我得到的回覆是

{ 
    "incidents" : [ 
    { 
     "Asset" : null, "AssignedTo" : { 
      "Account" : null, "StaffGroup" : { 
       "ID" : { 
        "id" : 100885 
       },"Name" : "B2CHousewares" 
      },"ValidNullFields" : null 
     },"BilledMinutes" : null, "Category" : { 
      "ID" : { 
       "id" : 124 
      },"Parents" : [ 
      { 
      ..... 

我看着使用@JsonSerialize(包括= JsonSerialize.Inclusion.NON_NULL)註釋但didnt似乎工作,我猜猜它,因爲它隻影響這個類。我可以將它添加到所有JAXB生成的類,但我不想(如果我需要重新生成類)..

我的另一種方法是創建一個自定義集合類的數據傳輸,但我認爲這方法會節省我的時間+代碼..

有什麼想法?

感謝所有提前。

回答

0

我設法蘇斯出來..

我需要創建一個自定義對象映射,並告訴它不渲染空,爲此工作了這對我來說生成的JAXB對象..

簡單創建一個映射器類,並確保@Provider標籤存在

還要注意我的代碼是新澤西1.x中,特別是1.8

包mypackage的;

import javax.ws.rs.ext.ContextResolver; 
import javax.ws.rs.ext.Provider; 

import org.codehaus.jackson.map.DeserializationConfig; 
import org.codehaus.jackson.map.ObjectMapper; 
import org.codehaus.jackson.map.SerializationConfig; 
import org.codehaus.jackson.map.annotate.JsonSerialize; 

@Provider 
public class CustomJSONObjectMapper implements ContextResolver<ObjectMapper> { 

    private ObjectMapper objectMapper; 


    public CustomJSONObjectMapper() throws Exception { 
     System.out.println("My object mapper init"); 
     objectMapper= new ObjectMapper(); 
     // Force all conversions to be NON_NULL for JSON 
     objectMapper.setSerializationInclusion(JsonSerialize.Inclusion.NON_NULL); 
    } 

    public ObjectMapper getContext(Class<?> objectType) { 
     System.out.println("My Object Mapper called"); 
     return objectMapper; 
    } 
} 

,其結果是

{ 
    "organization" : [ 
    { 
     "id" : { 
      "id" : 68 
     },"lookupName" : "AngieSoft", "createdTime" : 1412166303000, "updatedTime" : 1412166303000, "name" : "AngieSoft", "salesSettings" : { 
      "salesAccount" : { 
       "id" : { 
        "id" : 2 
       } 
      },"totalRevenue" : { 
       "currency" : { 
        "id" : { 
         "id" : 1 
        } 
       } 
      } 
     },"source" : { 
      "id" : { 
       "id" : 1002 
      },"parents" : [ 
      { 
       "id" : { 
        "id" : 32002 
       } 
      } 
] 
     },"crmmodules" : { 
      "marketing" : true, "sales" : true, "service" : true 
     } 
    } 
] 
}