优化token返回前端的json为标准格式
This commit is contained in:
+30
-7
@@ -1,13 +1,12 @@
|
||||
package com.central.common.utils;
|
||||
|
||||
import com.central.common.model.Result;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import org.springframework.http.MediaType;
|
||||
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.io.IOException;
|
||||
import java.io.Writer;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* @author zlt
|
||||
@@ -27,15 +26,39 @@ public class ResponseUtil {
|
||||
* @throws IOException
|
||||
*/
|
||||
public static void responseWriter(ObjectMapper objectMapper, HttpServletResponse response, String msg, int httpStatus) throws IOException {
|
||||
Map<String, String> rsp = new HashMap<>(2);
|
||||
response.setStatus(httpStatus);
|
||||
rsp.put("resp_code", String.valueOf(httpStatus));
|
||||
rsp.put("resp_msg", msg);
|
||||
Result result = Result.succeedWith(null, httpStatus, msg);
|
||||
responseWrite(objectMapper, response, result);
|
||||
}
|
||||
|
||||
/**
|
||||
* 通过流写到前端
|
||||
* @param objectMapper 对象序列化
|
||||
* @param response
|
||||
* @param obj
|
||||
*/
|
||||
public static void responseSucceed(ObjectMapper objectMapper, HttpServletResponse response, Object obj) throws IOException {
|
||||
Result result = Result.succeed(obj);
|
||||
responseWrite(objectMapper, response, result);
|
||||
}
|
||||
|
||||
/**
|
||||
* 通过流写到前端
|
||||
* @param objectMapper
|
||||
* @param response
|
||||
* @param msg
|
||||
* @throws IOException
|
||||
*/
|
||||
public static void responseFailed(ObjectMapper objectMapper, HttpServletResponse response, String msg) throws IOException {
|
||||
Result result = Result.failed(msg);
|
||||
responseWrite(objectMapper, response, result);
|
||||
}
|
||||
|
||||
private static void responseWrite(ObjectMapper objectMapper, HttpServletResponse response, Result result) throws IOException {
|
||||
response.setContentType(MediaType.APPLICATION_JSON_UTF8_VALUE);
|
||||
try (
|
||||
Writer writer = response.getWriter()
|
||||
) {
|
||||
writer.write(objectMapper.writeValueAsString(rsp));
|
||||
writer.write(objectMapper.writeValueAsString(result));
|
||||
writer.flush();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user