org.macrocloud.kernel.toolkit.response.ResponseUtil Maven / Gradle / Ivy
package org.macrocloud.kernel.toolkit.response;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.http.HttpServletResponse;
public class ResponseUtil {
private ResponseUtil() {
throw new IllegalStateException("Utility class");
}
public static void responseWriter(ObjectMapper objectMapper, HttpServletResponse response, String msg,
int httpStatus) throws IOException {
ResponseData result = new ResponseData(httpStatus, msg, (Object) null);
responseWrite(objectMapper, response, result);
}
private static void responseWrite(ObjectMapper objectMapper, HttpServletResponse response, ResponseData result)
throws IOException {
response.setContentType("application/json;charset=UTF-8");
PrintWriter writer = response.getWriter();
try {
writer.write(objectMapper.writeValueAsString(result));
writer.flush();
} catch (Throwable var7) {
if (writer != null) {
try {
writer.close();
} catch (Throwable var6) {
var7.addSuppressed(var6);
}
}
throw var7;
}
if (writer != null) {
writer.close();
}
}
public static void responseSucceed(ObjectMapper objectMapper, HttpServletResponse response, Object obj)
throws IOException {
ResponseData result = new ResponseData(HttpCode.OK.value(), HttpCode.OK.msg(), obj);
responseWrite(objectMapper, response, result);
}
public static void responseFailed(ObjectMapper objectMapper, HttpServletResponse response, String msg)
throws IOException {
ResponseData result = new ResponseData(1, msg, (Object) null);
responseWrite(objectMapper, response, result);
}
public static void responseFailed(ObjectMapper objectMapper, HttpServletResponse response, String msg, int httpCode)
throws IOException {
ResponseData result = new ResponseData(httpCode, msg, (Object) null);
responseWrite(objectMapper, response, result);
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy