ResponseUtils 工具类

package com.stdspringboot.util;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

public class ResponseUtils {

    /**
     * 向前端输出JSON字符串,设置字符集是 UTF-8
     * @param response
     * @param json
     * @throws IOException
     */
    public static void writeJSON(HttpServletResponse response,String json) throws IOException {
        // 一定要在获取流之前调用,否则无效
        response.setContentType("application/json; charset=UTF-8");
        response.setCharacterEncoding("UTF-8"); // 有时不写也可以
        PrintWriter out=response.getWriter();
        out.write(json);
        out.flush();// 在springboot、springmvc中,必须加此行代码,否则可能报错
        out.close();
    }
}

原文出处:https://malaoshi.top/show_1IX2rmww9nux.html