在Spring-mvc文件中配置以下内容
<!-- 返回值配置json-->
<mvc:annotation-driven>
<mvc:message-converters register-defaults="true">
<!-- 避免IE执行AJAX时,返回JSON出现下载文件 -->
<bean id="fastJsonHttpMessageConverter" class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>application/json;charset=UTF-8</value>
</list>
</property>
</bean>
</mvc:message-converters>
</mvc:annotation-driven>
controller中不用将java对象转化成字符串再输出,直接在方法处定义与java对象一致的数据类型,输出java对象即可输出对应的json格式
@RestController
@RequestMapping("/user")
public class UserController {
@Autowired
private IUserService userService;
@GetMapping("/findAll")
public List<UserInf> findAll(){
List<UserInf> userInfoList = new ArrayList<>();
UserInf userInfo = new UserInf();
userInfo.setId(222);
userInfo.setUsername("账号22");
userInfo.setPassword("密码22");
userInfoList.add(userInfo);
List<UserInf> userInfos = userService.findAll();
System.out.println(userInfoList);
return userInfos;
}
}