gpt4 book ai didi

java - Spring MVC : recieving object with empty attributes in a post Method using @RequestBody

转载 作者:行者123 更新时间:2023-12-01 22:24:53 27 4
gpt4 key购买 nike

我尝试将 json 对象发送到 Rest 服务。问题是我收到的对象具有空属性

休息服务:

 @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:

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;
}

}

在客户端:我想使用此服务创建一个 Extjs Store

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

请求正文:

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

在服务中调试时,我发现 element 对象,但其属性为空。你能帮我一下吗?

最佳答案

您需要使处理程序方法参数 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": ""
}
}

那就是

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

将会改变

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

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

关于java - Spring MVC : recieving object with empty attributes in a post Method using @RequestBody,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28990544/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com