提交 4be31c40 编写于 作者: zlt2000's avatar zlt2000

拆分ResponseUtil‘

上级 540c46e9
package com.central.common.utils;
import com.alibaba.fastjson.JSONObject;
import com.central.common.model.Result;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.core.io.buffer.DataBuffer;
import org.springframework.core.io.buffer.DataBufferFactory;
import org.springframework.core.io.buffer.DataBufferUtils;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.Writer;
import java.nio.charset.Charset;
/**
* @author zlt
......@@ -71,38 +62,4 @@ public class ResponseUtil {
writer.flush();
}
}
/**
* webflux的response返回json对象
*/
public static Mono<Void> responseWriter(ServerWebExchange exchange, int httpStatus, String 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(httpStatus));
response.getHeaders().setContentType(MediaType.APPLICATION_JSON_UTF8);
DataBufferFactory dataBufferFactory = response.bufferFactory();
DataBuffer buffer = dataBufferFactory.wrap(JSONObject.toJSONString(result).getBytes(Charset.defaultCharset()));
return response.writeWith(Mono.just(buffer)).doOnError((error) -> {
DataBufferUtils.release(buffer);
});
}
}
package com.central.common.utils;
import com.alibaba.fastjson.JSONObject;
import com.central.common.model.Result;
import org.springframework.core.io.buffer.DataBuffer;
import org.springframework.core.io.buffer.DataBufferFactory;
import org.springframework.core.io.buffer.DataBufferUtils;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;
import java.nio.charset.Charset;
/**
* @author zlt
* @date 2020/5/5
* <p>
* Blog: https://zlt2000.gitee.io
* Github: https://github.com/zlt2000
*/
public class WebfluxResponseUtil {
/**
* webflux的response返回json对象
*/
public static Mono<Void> responseWriter(ServerWebExchange exchange, int httpStatus, String 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(httpStatus));
response.getHeaders().setContentType(MediaType.APPLICATION_JSON_UTF8);
DataBufferFactory dataBufferFactory = response.bufferFactory();
DataBuffer buffer = dataBufferFactory.wrap(JSONObject.toJSONString(result).getBytes(Charset.defaultCharset()));
return response.writeWith(Mono.just(buffer)).doOnError((error) -> {
DataBufferUtils.release(buffer);
});
}
}
package com.central.gateway.auth;
import com.central.common.utils.ResponseUtil;
import com.central.common.utils.WebfluxResponseUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.security.access.AccessDeniedException;
......@@ -21,6 +21,6 @@ import reactor.core.publisher.Mono;
public class JsonAccessDeniedHandler implements ServerAccessDeniedHandler {
@Override
public Mono<Void> handle(ServerWebExchange exchange, AccessDeniedException e) {
return ResponseUtil.responseFailed(exchange, HttpStatus.FORBIDDEN.value(), e.getMessage());
return WebfluxResponseUtil.responseFailed(exchange, HttpStatus.FORBIDDEN.value(), e.getMessage());
}
}
package com.central.gateway.auth;
import com.central.common.utils.ResponseUtil;
import com.central.common.utils.WebfluxResponseUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.security.core.AuthenticationException;
......@@ -21,6 +21,6 @@ import reactor.core.publisher.Mono;
public class JsonAuthenticationEntryPoint implements ServerAuthenticationEntryPoint {
@Override
public Mono<Void> commence(ServerWebExchange exchange, AuthenticationException e) {
return ResponseUtil.responseFailed(exchange, HttpStatus.UNAUTHORIZED.value(), e.getMessage());
return WebfluxResponseUtil.responseFailed(exchange, HttpStatus.UNAUTHORIZED.value(), e.getMessage());
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册