简体   繁体   English

SPRING MVC:使用@RequestBody在post方法中接收具有空属性的对象

[英]SPRING MVC: recieving object with empty attributes in a post Method using @RequestBody

I try to send a json Object to a Rest Service. 我尝试将json对象发送到Rest服务。 The problem is that I recieve the object with empty attributes 问题是我收到具有空属性的对象

The rest Service : 其余服务

 @RequestMapping(value = "/retrieve_combo_values",
        consumes="application/json",
        method = RequestMethod.POST)
public List<ComboValueDTO> retrieveValuesForCombo(@RequestBody ElementDTO element)
        throws Exception {
    return service.retrieveValuesForCombo(element);
}

ElementDTO : ElementDTO

public class ElementDTO implements Serializable {
    private String name = "";
    private String labelId = "";
    private ParameterType parameterType;
    private boolean mandatory = false;
    private boolean visible = true;
    private String defaultValue = "";
    private UIElementTypeDTO elementType;
    private String regExErrMsg = "";
    private String requiredErrMsg = "";

    public ElementDTO() { }

    public ElementDTO(String name, String labelId,
                      ParameterType parameterType,
                      boolean mandatory,
                      boolean visible,
                      String defaultValue,
                      UIElementTypeDTO elementType,
                      String regExErrMsg,
                      String requiredErrMsg)
    {
        this.name = name;
        this.labelId = labelId;
        this.parameterType = parameterType;
        this.mandatory = mandatory;
        this.visible = visible;
        this.defaultValue = defaultValue;
        this.elementType = elementType;
        this.regExErrMsg = regExErrMsg;
        this.requiredErrMsg = requiredErrMsg;
    }

    public ElementDTO(UIQueryParameter el) {
        this(el.getName(), "", el.getParameterType(), 
             el.isMandatory(), el.isVisible(), el.getDefaultValue(),
             getElementTypeFromParameterType(el.getParameterType()),
             "", "");
        ResourceBundle props = 
            ResourceBundle.getBundle(IConstants.BUNDLE_KEY,  
                                     BdociConfig.getLocale());
        this.labelId = Utils.getUTF8ResourceBundleString(props,  
                                                         el.getLabelId());
        this.regExErrMsg = Utils.getUTF8ResourceBundleString(
            props, IConstants.RegExp_Error_Prefix + parameterType.getErrID());
        if (this.mandatory) {
            this.requiredErrMsg = 
                 Utils.getUTF8ResourceBundleString(
                     props, IConstants.Required_Error_Key);
        }
    }

    private static UIElementTypeDTO  
    getElementTypeFromParameterType(ParameterType parameterType)
    {
        String pType = parameterType.getClass().getSimpleName();
        return UIElementTypeDTO.get(pType);
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getLabelId() {
        return labelId;
    }

    public void setLabelId(String labelId) {
        this.labelId = labelId;
    }

    public ParameterType getParameterType() {
        return parameterType;
    }

    public void setParameterType(ParameterType parameterType) {
        this.parameterType = parameterType;
    }

    public boolean isMandatory() {
        return mandatory;
    }

    public void setMandatory(boolean mandatory) {
        this.mandatory = mandatory;
    }

    public boolean isVisible() {
        return visible;
    }

    public void setVisible(boolean visible) {
        this.visible = visible;
    }

    public String getDefaultValue() {
        return defaultValue;
    }

    public void setDefaultValue(String defaultValue) {
        this.defaultValue = defaultValue;
    }

    public UIElementTypeDTO getElementType() {
        return elementType;
    }

    public void setElementType(UIElementTypeDTO elementType) {
        this.elementType = elementType;
    }

    public String getRegExErrMsg() {
        return regExErrMsg;
    }

    public void setRegExErrMsg(String regExErrMsg) {
        this.regExErrMsg = regExErrMsg;
    }

    public String getRequiredErrMsg() {
        return requiredErrMsg;
    }

    public void setRequiredErrMsg(String requiredErrMsg) {
        this.requiredErrMsg = requiredErrMsg;
    }

    public List<ComboValueDTO>
    retrieveValues(IBdocDesktopController service)
        throws BdocWebAccessException,
               TransformerException,
               ParserConfigurationException
    {
        return elementType.retrieveValues(parameterType, service);
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        ElementDTO that = (ElementDTO) o;

        if (mandatory != that.mandatory) return false;
        if (visible != that.visible) return false;
        if (defaultValue != null ? 
                !defaultValue.equals(that.defaultValue) : 
                that.defaultValue != null)
            return false;
        if (elementType != that.elementType) return false;
        if (labelId != null ? !labelId.equals(that.labelId) : 
            that.labelId != null) return false;
        if (name != null ? !name.equals(that.name) : that.name != null) 
             return false;
        if (parameterType != null ? 
                !parameterType.equals(that.parameterType) :  
                that.parameterType != null)
            return false;
        if (regExErrMsg != null ? !regExErrMsg.equals(that.regExErrMsg) : 
                that.regExErrMsg != null)
            return false;
        if (requiredErrMsg != null ? 
                !requiredErrMsg.equals(that.requiredErrMsg) : 
                that.requiredErrMsg != null)
            return false;

        return true;
    }

    @Override
    public int hashCode() {
        int result = name != null ? name.hashCode() : 0;
        result = 31 * result + (labelId != null ? labelId.hashCode() : 0);
        result = 31 * result + (parameterType != null ? 
            parameterType.hashCode() : 0);
        result = 31 * result + (mandatory ? 1 : 0);
        result = 31 * result + (visible ? 1 : 0);
        result = 31 * result + (defaultValue != null ? 
            defaultValue.hashCode() : 0);
        result = 31 * result + (elementType != null ? 
            elementType.hashCode() : 0);
        result = 31 * result + (regExErrMsg != null ? 
            regExErrMsg.hashCode() : 0);
        result = 31 * result + (requiredErrMsg != null ? 
            requiredErrMsg.hashCode() : 0);
        return result;
    }

} }

In the client side: I want to create an Extjs Store using this Service 在客户端:我想使用此服务创建一个Extjs商店

 store.on('beforeload', function (store) {
            store.proxy.extraParams = {
                element: me  //me is the json Object
            }
        });

Request body: 要求正文:

{"query":"","element":{"name":"domaine","labelId":"Domaines ","parameterType":{"regEx":"^.*$","errID":"121","selectedValue":""},"mandatory":false,"visible":true,"defaultValue":"","elementType":"BDOC_DOMAIN_LIST","regExErrMsg":"Valeur invalide.","requiredErrMsg":""}}

When debugging in the service, I found the element object but its attributes are empty. 在服务中调试时,我发现了元素对象,但其属性为空。 Can you help me please? 你能帮我吗?

You need to make your handler method parameter POJO fit your JSON 您需要使处理程序方法参数POJO适合您的JSON

{
    "query": "",
    "element": {
        "name": "domaine",
        "labelId": "Domaines ",
        "parameterType": {
            "regEx": "^.*$",
            "errID": "121",
            "selectedValue": ""
        },
        "mandatory": false,
        "visible": true,
        "defaultValue": "",
        "elementType": "BDOC_DOMAIN_LIST",
        "regExErrMsg": "Valeur invalide.",
        "requiredErrMsg": ""
    }
}

That is 那是

class Wrapper {
    private String query;
    private ElementDTO element;
    // getters and setters
} 

which will change 将会改变

public List<ComboValueDTO> retrieveValuesForCombo(@RequestBody ElementDTO element)

to

public List<ComboValueDTO> retrieveValuesForCombo(@RequestBody Wrapper element)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM