规范异常返回json
This commit is contained in:
+2
-3
@@ -4,7 +4,6 @@ import com.central.common.utils.ResponseUtil;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.security.access.AccessDeniedException;
|
||||
import org.springframework.security.oauth2.provider.error.OAuth2AccessDeniedHandler;
|
||||
import org.springframework.security.oauth2.provider.expression.OAuth2WebSecurityExpressionHandler;
|
||||
@@ -30,7 +29,7 @@ public class DefaultSecurityHandlerConfig {
|
||||
*/
|
||||
@Bean
|
||||
public AuthenticationEntryPoint authenticationEntryPoint() {
|
||||
return (request, response, authException) -> ResponseUtil.responseWriter(objectMapper, response, authException.getMessage(), HttpStatus.UNAUTHORIZED.value());
|
||||
return (request, response, authException) -> ResponseUtil.responseFailed(objectMapper, response, authException.getMessage());
|
||||
}
|
||||
|
||||
@Bean
|
||||
@@ -49,7 +48,7 @@ public class DefaultSecurityHandlerConfig {
|
||||
|
||||
@Override
|
||||
public void handle(HttpServletRequest request, HttpServletResponse response, AccessDeniedException authException) throws IOException, ServletException {
|
||||
ResponseUtil.responseWriter(objectMapper, response, authException.getMessage(), HttpStatus.FORBIDDEN.value());
|
||||
ResponseUtil.responseFailed(objectMapper, response, authException.getMessage());
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@@ -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