【问题标题】:updating JSON Object in spring restful web services在 Spring RESTful Web 服务中更新 JSON 对象
【发布时间】:2017-11-28 13:58:30
【问题描述】:

我想为更新函数传递一个 json 对象,但它不接受 json 对象并得到一个错误。错误是:

代码是:

(value = "/UpdateUser/", method = RequestMethod.PUT , consumes=MediaType.APPLICATION_JSON_VALUE)
public void UpdateUser(JSONObject RequiredObject)throws UnknownHostException {
     // RequiredObject=new HashMap<>();
     System.out.println("hello into update " + RequiredObject);
     // readJSON.UpdateUser(RequiredObject); 
}

【问题讨论】:

  • 使用复制粘贴在表单中输入代码,选择它,然后点击顶部的{},将其转换为可读的内容。不要使用图片。
  • {@RequestMapping(value = "/UpdateUser/", method = RequestMethod.PUT , consumes=MediaType.APPLICATION_JSON_VALUE) public void UpdateUser(JSONObject RequiredObject)throws UnknownHostException { // RequiredObject=new HashMap (); System.out.println("你好进入更新" + RequiredObject); // readJSON.UpdateUser(RequiredObject); }}
  • 我已经编辑了这个问题给你一个例子。
  • 您是否尝试过使用@RequestBodyJSONObject RequiredObject 为前缀的注解?你也缺少@RequestMapping...最后你的变量不应该使用大写字母(但这是代码约定)

标签: json spring rest web-services


【解决方案1】:

您必须以@RequestBody 的形式接收请求的正文,并且您可以直接以用户对象的形式接收此json 对象

@RequestMapping(value = "/UpdateUser/", method = RequestMethod.PUT , 
consumes=MediaType.APPLICATION_JSON_VALUE) 
public void UpdateUser(@RequestBody User user) throws UnknownHostException { 
// RequiredObject=new HashMap<>(); 
System.out.println("hello into update " + RequiredObject); 
//readJSON.UpdateUser(RequiredObject); 
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-21
    相关资源
    最近更新 更多