SpringMvc下生成标准Json数据的最简单方法

xiaoxiao2021-02-28  78

1.在maven中导入如下的依赖:

<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-annotations --> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-annotations</artifactId> <version>2.8.5</version> </dependency> <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-core --> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-core</artifactId> <version>2.8.5</version> </dependency> <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind --> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.8.5</version> </dependency> 在对应的控制器上做如下的操作:

@Controller public class SpringMVCTest { @Autowired private EmployeeDao employeeDao; @ResponseBody @RequestMapping("/testJson") public Collection<Employee> testJson(){ return employeeDao.getAll(); } } 简单粗暴又好用,注意jsckson的版本号要一致,不然会报错。

转载请注明原文地址: https://www.6miu.com/read-80338.html

最新回复(0)