SPRING MVC:使用@RequestBody 在 post 方法中接收具有空属性的对象
SPRING MVC: recieving object with empty attributes in a post Method using @RequestBody
我尝试将 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.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)
我尝试将 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.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)