POJO到SpringV中的MultiValueMap映射/绑定/转换

tia*_*tia 10 java spring spring-mvc

我有一个POJO我需要格式化为MultiValueMap.此MultiValueMap将在POST方法中用作使用restTemplate类的请求,并将作为contentType application/x-www-form-urlencoded传递给我的Web服务.

是否有任何工具或实用程序可以为我执行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;
    }
}
Run Code Online (Sandbox Code Playgroud)

在通过在restTemplate对象上调用POST将MultiValueMap转换为contentType:application/x-www-form-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=email@clinic1.com&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=email@clinic2.com&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
Run Code Online (Sandbox Code Playgroud)

小智 11

您可以使用 Jackson objectMapper:

MultiValueMap valueMap = new LinkedMultiValueMap<String, Object>();
Map<String, Object> fieldMap = objectMapper.convertValue(requestObject, new TypeReference<Map<String, Object>>() {});
valueMap.setAll(fieldMap);
Run Code Online (Sandbox Code Playgroud)


DwB*_*DwB 0

你可以通过反思和/或内省来做到这一点(我从来不记得正确的名字)。这是序列化的变体,因此您可能需要查看序列化实现。

另一种选择是创建一个与此类似的界面

公共接口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;
}