报错No converter found for return value of type: class org.json.JSONObject

前后端分离,更新用户信息的时候报错No converter found for return value of type: class org.json.JSONObject
运行报错:

Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; 
nested exception is java.lang.NullPointerException] with root cause
java.lang.NullPointerException:
····································

这里看不出来具体什么错误

去前端页面F12一看显示:No converter found for return value of type: class org.json.JSONObject

Controller层代码:

//    更新用户信息
@ResponseBody
@RequestMapping(value = "/user/update", method = RequestMethod.POST)
public Object updateUserMsg(HttpServletRequest req) {
JSONObject jsonObject = new JSONObject();
String id = req.getParameter("id").trim();
String username = req.getParameter("username").trim();
String password = req.getParameter("password").trim();
String sex = req.getParameter("sex").trim();
String phone = req.getParameter("phone").trim();
String email = req.getParameter("email").trim();
String birth = req.getParameter("birth").trim();
String introduction = req.getParameter("introduction").trim();
String location = req.getParameter("location").trim();
// String avatar = req.getParameter("avatar").trim();
// System.out.println(username + " " + password + " " + sex + " " + phone + " " + email + " " + birth + " " + introduction + " " + location);

if (username.equals("") || username == null) {
jsonObject.put("code", 0);
jsonObject.put("msg", "用户名或密码错误");
return jsonObject;
}
Consumer consumer = new Consumer();
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
Date myBirth = new Date();
try {
myBirth = dateFormat.parse(birth);
} catch (Exception e) {
e.printStackTrace();
}
consumer.setId(Integer.parseInt(id));
consumer.setUsername(username);
consumer.setPassword(password);
consumer.setSex(new Byte(sex));
consumer.setPhone(phone);
consumer.setEmail(email);
consumer.setBirth(myBirth);
consumer.setIntroduction(introduction);
consumer.setLocation(location);
// consumer.setAvatar(avatar);
consumer.setUpdateTime(new Date());

boolean res = consumerService.updateUserMsg(consumer);
if (res) {
jsonObject.put("code", 1);
jsonObject.put("msg", "修改成功");
return jsonObject;
} else {
jsonObject.put("code", 0);
jsonObject.put("msg", "修改失败");
return jsonObject;
}
}

  

报错原因:
java原生态org.json.JSONObject不能作为返回值返回json数据

解决:
需要用到 com.alibaba.fastjson的JSONObject对象返回json数据

在pom中引入:

<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.47</version>
</dependency>

 

参考博客:No converter found for return value of type: class org.json.JSONObject_程序员白小白的博客-CSDN博客

posted @ 2021-12-31 14:37  Maples922  阅读(269)  评论(0)    收藏  举报