SpringMVC框架--@ResponseBody返回json格式对象

@Responsebody 的介绍

@Responsebody 注解表示该方法的返回的结果直接写入 HTTP 响应正文(ResponseBody)中,一般在异步获取数据时使用,通常是在使用 @RequestMapping 后,返回值通常解析为跳转路径,加上 @Responsebody 后返回结果不会被解析为跳转路径,而是直接写入HTTP 响应正文中。

@Responsebody 的使用及遇到的问题

 1.No converter found for return value of type

解决方案:缺少jar包


通过Maven导入jar包

<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.9.5</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.9.5</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.5</version>
</dependency>

2.在使用@Responsebody时报错:Servlet.init() for servlet SpringDemo-dispatcher threw exception

解决方案:Jason相关jar包的版本问题,替换为新的版本

测试的代码

@RequestMapping(value = "/backJason")
@ResponseBody
public Student testJason(){
Student student = new Student();
student.setId("2");
student.setName("王");
student.setSex("男");
return student;
}
效果

猜你喜欢

转载自blog.csdn.net/weixin_42228338/article/details/80349307