2015-09-09 33 views
0

我正在使用spring和hibernate。我有一個類(DTO)有很多字符串成員變量。我試圖爲這個類實現搜索。用戶應該能夠通過每個字段進行搜索。我使用jackson json mapper來序列化和反序列化對象。無論如何通過使用JsonProperty值來確定fieldName?通過將@JsonProperty值傳遞給對象映射器來識別屬性

讓這成爲一個例子:我的DTO

public class SampleDTO{ 
    private String field1; 
    private String field2; 
    private String field3; 
    private String field4; 
    @JsonProperty("FIELD_1") 
    public String getField1(){ 
    return field1; 
    } 
    @JsonProperty("FIELD_2") 
    public String getField2(){ 
    return field2; 
    } 
    @JsonProperty("FIELD_3") 
    public String getField3(){ 
    return field3; 
    } 
    @JsonProperty("FIELD_4") 
    public String getField4(){ 
    return field4; 
    } 
} 

讓這成爲我的搜索功能

public Set<T> search(String fieldName, String searchKeyword) { 
    String originalFieldName = someMagicFunction(fieldName); 
    //if fieldName= "FIELD_1", someMagicFunction should return "field1" 
    Criteria criteria = session.createCriteria(T.class); 
    criteria.add(Restrictions.eq(originalFieldName, searchKeyword)); 
    return new HashSet<T>(criteria.list()); 
} 

的任何實施罰款。我正在尋找一種處理這種情況的好方法。感覺像手動查找字段涉及「太多打字」。

回答

2

你基本上想使用反射。這裏有兩種可能性,當談到現場查詢:

  • @JsonProperty註釋
  • 領域的
  • 真實姓名

在第一種情況下的價值,你可能需要使用一些額外的庫使用反射+註記時緩解疼痛,但原油代碼看起來更不像是:

SampleDTO dto = new SampleDTO(); 
    // setup some values here 

    Field[] fields = r.getClass().getFields(); 

    for(Field f : fields) { 
     JsonProperty jsonProperty = f.getDeclaredAnnotation(JsonProperty.class); 

     if (jsonProperty != null && jsonProperty.value().equals("FIELD_1")) { 
      return (String) f.get(dto); 
     } 

     // throw exception since passed field name is illegal 
    }   

在第二個它會容易得多:

SampleDTO dto = new SampleDTO(); 
    // setup some values here 

    String field1Value = (String) r.getClass().getField("field1").get(dto); 
+0

感謝您的意見。跟着回答http://stackoverflow.com/a/13514566/796400。 –

0

如果有人有興趣,這就是我解決問題的方法。我將這段代碼添加到DAO的構造函數中。

try { 

    BeanInfo beanInfo = Introspector.getBeanInfo(T.class); 
    Method[] methods = T.class.getMethods(); 
    PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors(); 
    for(PropertyDescriptor propertyDescriptor: propertyDescriptors) { 
    //I'm looking for string fields only 
    if (propertyDescriptor.getPropertyType().equals(String.class)) { 
    //My annotations are on methods 
    for(Method method: methods) { 
    if(propertyDescriptor.getReadMethod().equals(method)) { 
     JsonProperty jsonProperty = method.getAnnotation(JsonProperty.class); 
     if (jsonProperty != null) { 
     //jsonFieldMapping is a Map<String,String> 
     //will be saving the mapping in the format {"FIELD_1":"field1", "FIELD_2":"field2"} 
     jsonFieldMapping.put(jsonProperty.value(), propertyDescriptor.getDisplayName()); 
     } else { 
     logger.debug("jsonProperty is null"); 
     } 
    } 
    } 
} 
    } 
    // just printing out the values identified from class 
    for(String key: jsonFieldMapping.keySet()) { 
logger.debug("key: " + key + "value: " + jsonFieldMapping.get(key)); 

    } 
} catch (IntrospectionException e) { 
    // TODO Auto-generated catch block 
    e.printStackTrace(); 
} 

所以,我的魔術方法將

public String getField(String jsonFieldName){ 
    if (jsonFieldMapping.containsKey(jsonFieldName)) { 
     return jsonFieldMapping.get(jsonFieldName); 
    } else { 
     throw new IllegalArgumentException("searching field not found"); 
    } 
} 

我還沒有完全測試此代碼。看起來像日誌中的值是正確的。

相關問題