zoukankan      html  css  js  c++  java
  • spring mvc返回json字符串的方式

    一.返回ModelAndView,其中包含map集

    复制代码
    复制代码
    /*
         * 返回ModelAndView类型的结果
         * 检查用户名的合法性,如果用户已经存在,返回false,否则返回true(返回json数据,格式为{"valid",true})
         */
        @RequestMapping(value = "/checkNameExistsMethod2", produces = "application/json;charset=UTF-8") //这里的produces值在不设置的情况下将根据返回结果自动决定
        public @ResponseBody
        ModelAndView checkNameValidMethod2(@RequestParam String name) {
            boolean result = true;
           //...
            Map<String, Boolean> map = new HashMap<>();
            map.put("valid", result);
            return new ModelAndView(new MappingJackson2JsonView(), map);
        }
    复制代码
    复制代码
     

    二.返回String类型的json,这里有两种方式。

    方式一:使用jackson-databind-x.x.x.jar包中的ObjectMapper将Map型数据改写为String并返回

    复制代码
    复制代码
      /*
         * 返回String类型的结果
         * 检查用户名的合法性,如果用户已经存在,返回false,否则返回true(返回json数据,格式为{"valid",true})
         */
        @RequestMapping(value = "/checkNameExistsMethod1", produces = "application/json;charset=UTF-8")
        public @ResponseBody
        String checkNameValidMethod1(@RequestParam String name) {
            boolean result = true;
            //...
            Map<String, Boolean> map = new HashMap<>();
            map.put("valid", result);
            ObjectMapper mapper = new ObjectMapper();
            String resultString = "";
            try {
                resultString = mapper.writeValueAsString(map);
            } catch (JsonProcessingException e) {
                e.printStackTrace();
            }
            return resultString;
        }
    复制代码
    复制代码

    方式二:

    直接返回字符串,主要key/value值必须使用含有转义字符的双引号,单引号无效 

    复制代码
    复制代码
     /*
         * 返回String类型的结果
         * 检查用户名的合法性,如果用户已经存在,返回false,否则返回true(返回json数据,格式为{"valid",true})
         */
        @RequestMapping(value = "/checkNameExistsMethod1", produces = "application/json;charset=UTF-8")
        public @ResponseBody
        String checkNameValidMethod1(@RequestParam String name) {
            boolean result = true;
    
        String resultString </span>= "{"result":true}"; <span style="color: #008000;">//</span><span style="color: #ff0000;">注意一定是双引号 "{"result":"success"}"</span>
      
        <span style="color: #0000ff;">return</span><span style="color: #000000;"> resultString;
    }</span></pre>
    
    复制代码
    复制代码

    三.返回任何预定义class类型的结果:

    复制代码
    复制代码
    @RequestMapping(value = "/findEmployeebyName")
        public @ResponseBody
        Employee findEmployeebyName(String name) {
            List<Employee> lstEmployees = employeeService.getAllEmployees();
            for (Employee employee : lstEmployees) {
                if (employee.getName().equals(name))
                    return employee;
            }
            return null;
        }
    复制代码
    复制代码

    这里的Employ必须事先定义好。

    四.使用HttpServletResponse对象的response.getWriter().write(xxx)方法

    复制代码
    复制代码
    @RequestMapping(value="/forbiddenUser")
        public void forbiddenUser(int id,HttpServletRequest request,HttpServletResponse response) {
            String resultString="{"result":"success"}";//注意一定是双引号 "{"result":true}"    
            try {
                response.setContentType("application/json");
                response.getWriter().write(resultString);
            } catch (IOException e) {
                e.printStackTrace();
            }        
        }
    复制代码
    复制代码

    原文地址:https://www.cnblogs.com/franson-2016/p/5613610.html

  • 相关阅读:
    2013-06-28,“万能数据库查询分析器”在中关村本月数据库类下载排行中重返前10位
    Oracle 存储过程
    强化学习精要:核心算法与TensorFlow实现
    深入理解TensorFlow:架构设计与实现原理
    Vue.js实战
    TensorFlow机器学习实战指南
    深入浅出React和Redux
    Flutter技术入门与实战
    TensorFlow:实战Google深度学习框架
    深度学习:一起玩转TensorLayer
  • 原文地址:https://www.cnblogs.com/jpfss/p/11056795.html
Copyright © 2011-2022 走看看