在点击 REST 控制器 POST 方法发送 "Request Body" 时获取 "malformed syntax"

Getting "malformed syntax" while hitting REST Controller POST method sending "Request Body"

这是 Rest Controller 服务签名

@RequestMapping(value = "/getFilteredReport", method = RequestMethod.POST)
 public void getFilteredReport(@RequestBody FilteredReportVO filteredReportVO,HttpServletRequest request,HttpServletResponse response) {

下面是我发送的JSON结构

{
"filterAttributesFactory":{
"930000":{
"metaDataId":930000,
"displayText":"Select Category",
"attributeType":211009,
"userInputValue":null,
"dropDownoptions":null,
"isMandatory":false,
"isDropDown":false,
"defaultValue":null,
"isdefaultValue":false,
"constraintId":null
},
"930001":{
"metaDataId":930001,
"displayText":"Item Status",
"attributeType":211005,
"userInputValue":null,
"dropDownoptions":{
"157005":"FC - fake scrap",
 "157006":"FH - firearm hold",
 "157008":"IN - inventory"
  },
  "isMandatory":false,
  "isDropDown":true,
  "defaultValue":null,
  "isdefaultValue":false,
  "constraintId":213007
  }
 },
"reportId":132030,
"location":1202
}

这里是 FilteredReportVO POJO

public class FilteredReportVO {

private HashMap<Integer,FilterAttributeVO> filterAttributesFactory=new HashMap<Integer,FilterAttributeVO>();

private Integer reportId;

private Long location; .....GETTERS and setters below

FilterAttributeVO pojo 结构如下..

public class FilterAttributeVO {
Integer metaDataId;
//String elementName;
String displayText;
Integer attributeType;
Object userInputValue;
Map<Integer,String> dropDownoptions;
Boolean isMandatory=false;;
Boolean isDropDown=false;
Object defaultValue;
Boolean isdefaultValue=false;
Integer constraintId=null;...Getters n setters..

我正在通过 POSTMAN 插件访问服务。 出现错误:

"The request could not be understood by the server due to malformed syntax. The client SHOULD NOT repeat the request without modifications".

仅供参考,在 POSTMAN 中,我将 JSON 结构放在 "Body" 中,选择 "raw",键入 "JSON(application/json)"。

注意我正在使用 2 object type 属性 userInputValue FilteredAttributeVO 内的默认值。 R 我们允许保留对象类型?

这里的问题在哪里?

如果您使用 JSONlint 看到输入 JSON 的屏幕截图,您的 json 不是 valid.Please 修复您的 json 对象并使用 [=20= 验证它]lint

您可以在测试中尝试使用以下代码来解决此问题,因为 Spring 内部使用 Jackson 库

ObjectMapper mapper = new ObjectMapper();
Staff obj = mapper.readValue(jsonInString, FilteredReportVO.class);

如果这工作正常,那么它们应该与您的 Postman RequestBody 准备有关,否则您将获得详细的堆栈跟踪:)

问题出在 FilteredReportVO POJO 上。我正在通过构造函数设置属性“location”和“reportId”的值。 没有为这 2 个属性定义 setter 方法

这是我的错,如果我能发布完整的 POJO class 你们一定已经明白了。无论如何感谢大家的帮助