2016-03-30 15 views
1

Akış oluşturma NesneMapper döküm işleri düzgün çalışıyorsa ("first_name" ve "last_name" geçtiyse). Bir güncelleme akışı üzerinde çalışıyorum. Varolan DB Verilerine yalnızca veri yükü vermeliyim.@JsonProperty BeanWrapper için başvuruyor

Benim POJO:

public class Contact 
{ 
    @JsonProperty("first_name") 
    @JsonView(ContactViews.CommonFields.class) 
    private String firstName; 

    @JsonProperty("last_name") 
    @JsonView(ContactViews.CommonFields.class) 
    private String lastName; 

    public String getFirstName() 
     { 
      return firstName; 
     } 

    public void setFirstName(String firstName) 
     {  
      this.firstName = firstName; 
     } 

    public String getLastName() 
     { 
      return lastName; 
     } 

    public void setLastName(String lastName) 
     { 
      this.lastName = lastName; 
     } 
} 

Ben sadece "first_name" sahip mevcut bir kişiyi olduğunu düşünelim. "Last_name" ile güncellemeliyim. Harita biçimindeki yükü ({"last_name": "XYZ"}) alıyorum. Var olan Map Map ile mevcut iletişimi nasıl güncellerim. oluşturmak için

Mevcut kodu:

Contact contact = mapper.readerWithView(ContactViews.CommonFields.class).forType(Contact.class).readValue(mapper.writeValueAsString(payloadMap)); 

Ben ek alıcıları ve ayarlayıcıları ekleyerek çalıştı. İyi çalışıyordu. Ama bunun üstesinden gelmek istiyorum çünkü çok fazla alan var. Herhangi bir yardım takdir edilecektir!

@JsonProperty("first_name") 
    public String getFirst_name() 
     { 
      return firstName; 
     } 

    @JsonProperty("first_name") 
    public void setFirst_name(String firstName) 
     {  
      this.firstName = firstName; 
     } 

    @JsonProperty("last_name") 
    public String getLast_name() 
     { 
      return lastName; 
     } 

    @JsonProperty("last_name") 
    public void setLast_name(String lastName) 
     { 
      this.lastName = lastName; 
     } 
diğer fonksiyonlar için bu kullanıyorum

(Ama ek Alıcı ve ayarlayıcıların olmadan bu doesnt iş):

-

Ek Alıcılar & Setters (ama bunu önlemek için gereken çalışması için)

public static void applyMapOntoInstance(Object instance , Map <String , ?> properties) 
     { 
      if (Utilities.isEmpty(properties)) 
       return; 

      String propertyName; 

      BeanWrapper beanWrapper = PropertyAccessorFactory.forBeanPropertyAccess(instance); 
      for (Object name : properties.entrySet()) 
       { 
        Map.Entry <String , ?> entry = (Map.Entry <String , ?>) name; 
        propertyName = entry.getKey(); 
        if (beanWrapper.isWritableProperty(propertyName)) 
         beanWrapper.setPropertyValue(propertyName , entry.getValue()); 
       } 
     } 

    public static void copyValues(Object source , Object target , Iterable <String> properties) 
     { 
      BeanWrapper src = new BeanWrapperImpl(source); 
      BeanWrapper trg = new BeanWrapperImpl(target); 

      for (String propertyName : properties) 
       trg.setPropertyValue(propertyName , src.getPropertyValue(propertyName)); 
     } 

cevap

0

Kullanım com.fasterxml.jackson.annotation.JsonProperty

+0

Bunu kullanıyorum sadece –

İlgili konular