四时宝库

程序员的知识宝库

04,SpringMVC响应json格式数据(spring json处理)

(1)json依赖

        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-redis</artifactId>
            <version>1.7.11.RELEASE</version>
        </dependency>

(2)响应json格式数据

@Controller
@RequestMapping(value = "/studentConter")
public class StudentController {
    // 路径映射
    @RequestMapping(value = "/studentQuery", method = RequestMethod.GET)
    @ResponseBody // 返回客户端json数据格式
    public String studentQuery(HttpServletRequest request) {
        List<StudentVo> studentVoList = initList();
        String jsonList = JSONArray.toJSONString(studentVoList);
        return jsonList;
    }
    /****
     * @date: 2022/11/2 20:31
     * @desc: 初始化集合值
     * */
    private List<StudentVo> initList( ) {
        StudentVo studentVo1 = new StudentVo();
        studentVo1.setsName("zhangsan");
        studentVo1.setsAge(18);
        StudentVo studentVo2 = new StudentVo();
        studentVo2.setsName("lisi");
        studentVo2.setsAge(19);

        ArrayList<StudentVo> studentVoArrayList = new ArrayList<StudentVo>();
        studentVoArrayList.add(studentVo1);
        studentVoArrayList.add(studentVo2);
        return studentVoArrayList;
    }
}

(3)请求测试

http://localhost:8085/bl_mave_wf_war_exploded/studentConter/studentQuery?stuName=zhangsan

(4)响应截图

发表评论:

控制面板
您好,欢迎到访网站!
  查看权限
网站分类
最新留言
    友情链接