2010-10-19 1443 views
9

我有一个POJO我需要格式化为MultiValueMap。这个MultiValueMap将在POST方法中作为请求使用restTemplate类,并将作为contentType application/x-www-form-urlencoded传递给我的web服务。POJO到MultiValueMap映射/绑定/转换在春天3

是否有任何工具或实用程序会为我执行POJO - > MultiValueMap转换?

样品POJO:

public class SampleDto implements Serializable, Idable, Comparable<SampleDto> { 

    private static final long serialVersionUID = 1L; 

    private Integer id; 

    private Boolean active; 

    private String lastName; 

    private List<SurgeonClinicDto> surgeonClinics = new ArrayList<SurgeonClinicDto>(); 
    private List<PromptValueDto> promptValues = new ArrayList<PromptValueDto>(); 

    private Date lastUpdated = new Date(); 

    public SampleDto() { 

    } 

    public Integer getId() { 
     return id; 
    } 

    public void setId(Integer id) { 
     this.id = id; 
    } 

    public Boolean getActive() { 
     return active; 
    } 

    public void setActive(Boolean active) { 
     this.active = active; 
    } 

    public String getLastName() { 
     return lastName; 
    } 

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

    public Date getLastUpdated() { 
     return lastUpdated; 
    } 

    public void setLastUpdated(Date lastUpdated) { 
     this.lastUpdated = lastUpdated; 
    } 

    public List<SurgeonClinicDto> getSurgeonClinics() { 
     return surgeonClinics; 
    } 

    public void setSurgeonClinics(List<SurgeonClinicDto> surgeonClinics) { 
     this.surgeonClinics = surgeonClinics; 
    } 

    public List<PromptValueDto> getPromptValues() { 
     return promptValues; 
    } 

    public void setPromptValues(List<PromptValueDto> promptValues) { 
     this.promptValues = promptValues; 
    } 

    public int compareTo(SampleDto o) { 

     if (getLastName() != null && o.getLastName() != null 
       && getLastName().compareTo(o.getLastName()) != 0) { 
      return getLastName().compareTo(o.getLastName()); 
     } 
     if (getActive() != null && o.getActive() != null 
       && getActive().compareTo(o.getActive()) != 0) { 
      return getActive().compareTo(o.getActive()); 
     } 
     if (getLastUpdated() != null && o.getLastUpdated() != null 
       && getLastUpdated().compareTo(o.getLastUpdated()) != 0) { 
      return getLastUpdated().compareTo(o.getLastUpdated()); 
     } 
     if (getId() != null && o.getId() != null 
       && getId().compareTo(o.getId()) != 0) { 
      return getId().compareTo(o.getId()); 
     } 

     return 0; 
    } 
} 

后MultiValueMap被转换成的contentType:应用/通过调用POST restTemplate对象上的X WWW窗体-urlencoded:

id=11752&active=true&lastName=Brownie& 
promptValues[0].id=12&promptValues[0].miscPromptId=882&promptValues[0].value=meFirst& 
promptValues[1].id=13&promptValues[1].miscPromptId=881&promptValues[1].value=youToo& 
surgeonClinics[0].address1=newAddress&surgeonClinics[0].address2=newAddress2&surgeonClinics[0].city=clinic City& 
surgeonClinics[0][email protected]&surgeonClinics[0].fax=123.456.7890&surgeonClinics[0].id=33273& 
surgeonClinics[0].name=clinic name&surgeonClinics[0].phone=890-098-4567& 
surgeonClinics[0].zip=34567&surgeonClinics[0].surgeryCenter1=MySurgeryCenter1& 
surgeonClinics[0].surgeryCenter2=MySurgeryCenter2& 
surgeonClinics[1].address1=newAddress11&surgeonClinics[1].address2=newAddress22&surgeonClinics[1].city=clinic2 City& 
surgeonClinics[1][email protected]&surgeonClinics[1].fax=123.456.7890&surgeonClinics[1].id=33274& 
surgeonClinics[1].name=clinic2 name&surgeonClinics[1].phone=890-098-4567& 
surgeonClinics[1].zip=34567& 
surgeonClinics[1].surgeryCenter1=MySurgeryCenter21&surgeonClinics[1].surgeryCenter2=MySurgeryCenter22 
+1

显示您期望的pojo和地图。 – Bozho 2010-10-19 06:16:28

+1

添加了一些代码示例来演示该问题。 – tia 2010-10-19 17:07:37

+0

你已经问过这个问题:http://stackoverflow.com/questions/3928163/spring-resttemplate-post-parameters-from-complex-object – skaffman 2010-10-19 17:59:17

回答

0

可以与反射做到这一点和/或内省(我从来不记得正确的名字)。这是序列化的变体,所以你可能想看看序列化的实现。

另一种选择是创建一个类似于此

 

putlic interface ToMap 
{ 
    Map<String, String> toMap(); 
} 

的接口,并实现它有问题的类。

为了您的POJO它可能是这样的:

 

Map<String, String> toMap() 
{ 
    int index; 
    StringBuilder key = new StringBuidler(); 
    Map<String, String> returnValue = new HashMap<String, String>(); 

    returnValue.put("id", id); 
    returnValue.put("active", active); 
    returnValue.put("lastName", lastName); 

    index = 0; 
    for (SurgeonClinicDto surgeonClinic : surgeonClinics) 
    { 
     key.setLength(0); 
     key.append("surgeonClinic["); 
     key.append(index); 
     key.append("].field1"); 

     returnValue.put(key.toString(), surgeonClinic[index].field1); 

     key.setLength(0); 
     key.append("surgeonClinic["); 
     key.append(index); 
     key.append("].field2"); 

     returnValue.put(key.toString(), surgeonClinic[index].field2); 

     ... more stuff here... 

    } 
    return returnValue; 
} 
 
0

如果你正在寻找的,你在你的问题中指定的格式发送,那么你可能要像做什么DwB建议,但是我认为如果你使用更多的面向对象的方法,例如JSON,你可能会发现转换更容易,还有很多库需要在POJO-> JSON之间进行转换。另一个优点是JSON分别处理数字/字符串/布尔值,因此将其转换回POJO更容易,而如果您发送数据,则需要将所有String对象转换回它们所属的类型。 id需要被转换为String-> int并且被转换为String-> boolean。

所以在JSON它可能是这个样子:

dto = { 
    id : 11752, 
    active : true, 
    lastName : "Brownie", 
    promptValues : [ 
     {id : 12, miscPromptId : 882, value : "meFirst"}, 
     {id : 13, miscPromptId : 881, value : "youToo"} 
    ], 
    surgeonClinics = [{..etc..}] 
} 

很明显,你可以做XML类似的东西,但是当我想在一行简单,所有的发送数据我喜欢这种简单的解决方案。 JSON库越来越好,有些可以通过反射从你的对象中产生。

无论如何只是一个建议。