规范异常返回json
This commit is contained in:
@@ -19,31 +19,26 @@ public class Result<T> implements Serializable {
|
||||
private String resp_msg;
|
||||
|
||||
public static <T> Result<T> succeed(String msg) {
|
||||
return succeedWith(null, CodeEnum.SUCCESS.getCode(), msg);
|
||||
return of(null, CodeEnum.SUCCESS.getCode(), msg);
|
||||
}
|
||||
|
||||
public static <T> Result<T> succeed(T model, String msg) {
|
||||
return succeedWith(model, CodeEnum.SUCCESS.getCode(), msg);
|
||||
return of(model, CodeEnum.SUCCESS.getCode(), msg);
|
||||
}
|
||||
|
||||
public static <T> Result<T> succeed(T model) {
|
||||
return succeedWith(model, CodeEnum.SUCCESS.getCode(), "");
|
||||
return of(model, CodeEnum.SUCCESS.getCode(), "");
|
||||
}
|
||||
|
||||
public static <T> Result<T> succeedWith(T datas, Integer code, String msg) {
|
||||
public static <T> Result<T> of(T datas, Integer code, String msg) {
|
||||
return new Result<>(datas, code, msg);
|
||||
}
|
||||
|
||||
public static <T> Result<T> failed(String msg) {
|
||||
return failedWith(null, CodeEnum.ERROR.getCode(), msg);
|
||||
return of(null, CodeEnum.ERROR.getCode(), msg);
|
||||
}
|
||||
|
||||
public static <T> Result<T> failed(T model, String msg) {
|
||||
return failedWith(model, CodeEnum.ERROR.getCode(), msg);
|
||||
return of(model, CodeEnum.ERROR.getCode(), msg);
|
||||
}
|
||||
|
||||
public static <T> Result<T> failedWith(T datas, Integer code, String msg) {
|
||||
return new Result<>(datas, code, msg);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+20
-3
@@ -35,7 +35,7 @@ public class ResponseUtil {
|
||||
* @throws IOException
|
||||
*/
|
||||
public static void responseWriter(ObjectMapper objectMapper, HttpServletResponse response, String msg, int httpStatus) throws IOException {
|
||||
Result result = Result.succeedWith(null, httpStatus, msg);
|
||||
Result result = Result.of(null, httpStatus, msg);
|
||||
responseWrite(objectMapper, response, result);
|
||||
}
|
||||
|
||||
@@ -76,11 +76,28 @@ public class ResponseUtil {
|
||||
* webflux的response返回json对象
|
||||
*/
|
||||
public static Mono<Void> responseWriter(ServerWebExchange exchange, int httpStatus, String msg) {
|
||||
Result result = Result.succeedWith(null, httpStatus, msg);
|
||||
Result result = Result.of(null, httpStatus, msg);
|
||||
return responseWrite(exchange, httpStatus, result);
|
||||
}
|
||||
|
||||
public static Mono<Void> responseFailed(ServerWebExchange exchange, String msg) {
|
||||
Result result = Result.failed(msg);
|
||||
return responseWrite(exchange, HttpStatus.INTERNAL_SERVER_ERROR.value(), result);
|
||||
}
|
||||
|
||||
public static Mono<Void> responseFailed(ServerWebExchange exchange, int httpStatus, String msg) {
|
||||
Result result = Result.failed(msg);
|
||||
return responseWrite(exchange, httpStatus, result);
|
||||
}
|
||||
|
||||
public static Mono<Void> responseWrite(ServerWebExchange exchange, int httpStatus, Result result) {
|
||||
if (httpStatus == 0) {
|
||||
httpStatus = HttpStatus.INTERNAL_SERVER_ERROR.value();
|
||||
}
|
||||
ServerHttpResponse response = exchange.getResponse();
|
||||
response.getHeaders().setAccessControlAllowCredentials(true);
|
||||
response.getHeaders().setAccessControlAllowOrigin("*");
|
||||
response.setStatusCode(HttpStatus.valueOf(result.getResp_code()));
|
||||
response.setStatusCode(HttpStatus.valueOf(httpStatus));
|
||||
response.getHeaders().setContentType(MediaType.APPLICATION_JSON_UTF8);
|
||||
DataBufferFactory dataBufferFactory = response.bufferFactory();
|
||||
DataBuffer buffer = dataBufferFactory.wrap(JSONObject.toJSONString(result).getBytes(Charset.defaultCharset()));
|
||||
|
||||
Reference in New Issue
Block a user