zoukankan      html  css  js  c++  java
  • Spring MVC返回JSON的几种方法

    方法一

    使用jackson-databind直接返回对象。

    方法二

    使用Gson将对象转换成String再返回,要设置contentType为application/json。

    方法三

    将对象转换成json,然后直接写入到HttpServletResponse中。

    例子如下

    package com.woaigsc.controller;
    
    import com.woaigsc.model.Greeting;
    import com.woaigsc.utils.JsonUtil;
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.*;
    
    import javax.servlet.http.HttpServletResponse;
    import java.io.IOException;
    import java.io.PrintWriter;
    import java.util.concurrent.atomic.AtomicLong;
    
    /**
     * Created by chuiyuan on 3/19/16.
     */
    @Controller
    public class GreetController {
        private static final String template ="Hello, %s" ;
        private final AtomicLong counter = new AtomicLong();
    
        @RequestMapping("/greeting")
        @ResponseBody
        public Greeting greeting(@RequestParam(value = "name", defaultValue = "World")String name ) {
            return new Greeting(counter.incrementAndGet(), String.format(template, name));
        }
    
        @RequestMapping("/greeting1")
        public void greeting1(@RequestParam(value = "name",defaultValue = "world")String name, HttpServletResponse resp){
            Greeting greeting = new Greeting(counter.incrementAndGet(),String.format(template,name ));
            resp.setContentType("application/json");
            resp.setCharacterEncoding("UTF-8");
            try {
                PrintWriter writer = resp.getWriter() ;
                writer.print(JsonUtil.toJson(greeting));
            }catch (IOException e){
                e.printStackTrace();
            }
        }
    
        @RequestMapping(value = "/greeting2",method = RequestMethod.GET, produces = "application/json")
        @ResponseBody
        public String greeting2(@RequestParam(value = "name",defaultValue = "lisj")String name){
            Greeting greeting = new Greeting(counter.incrementAndGet(),String.format(template,name));
            return JsonUtil.toJson(greeting);
        }
    
    }
    

     JsonUtil如下

    package com.woaigsc.utils;
    
    import com.google.gson.Gson;
    import com.google.gson.JsonNull;
    
    /**
     * Created by chuiyuan on 3/21/16.
     */
    public class JsonUtil {
        private static Gson gson = new Gson();
    
        public static String toJson(Object src){
            if (src== null){
                return gson.toJson(JsonNull.INSTANCE);
            }
            return gson.toJson(src);
        }
    }
    
  • 相关阅读:
    Linux平台下快速搭建FTP服务器
    CentOS7添加/删除用户和用户组
    Centos7上安装Nginx两种方法
    Centos7 yum安装tomcat
    Centos7下查看端口占用
    Centos7开放及查看端口
    ERROR 1819 (HY000): Your password does not satisfy the current policy requirements
    连接mysql数据库时提示2003 can't connect to MySQL server on ip(10060)的解决办法
    CentOS7 在线安装jdk8
    MySQL全方位练习(学生表 教师表 课程表 分数表)
  • 原文地址:https://www.cnblogs.com/chuiyuan/p/5301027.html
Copyright © 2011-2022 走看看