【java】采用PrintWriter+FastJson 输出json

采用PrintWriter+FastJson 输出json


// 设置格式为text/json
response.setContentType("text/json");
//设置字符集为'UTF-8'
response.setCharacterEncoding("UTF-8");
try {
    PrintWriter write = response.getWriter();
    ErrorJson errorJson = new ErrorJson();
     errorJson.setData("service occurred");
     String s = JSON.toJSON(errorJson).toString();
     write.write(s);
    write.flush();
    write.close();
} catch (IOException error) {
    error.printStackTrace();
    //TODO 记录报错
    System.out.println("用于记录报错的异常"+error.toString()
    +error.getMessage()+error.getCause()+"结束");
}

你可能感兴趣的:(javaEE)