Skip to content

Commit

Permalink
perf($ReactiveStarter): set HTTP header as application/json
Browse files Browse the repository at this point in the history
major browsers like Chrome now comply with the specification
and interpret correctly UTF-8 special characters
without requiring a charset=UTF-8 parameter.
  • Loading branch information
Johnny Miller (锺俊) committed Dec 23, 2020
1 parent 235bcb8 commit 886124a
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ public Mono<Void> handle(ServerWebExchange exchange, Throwable ex) {
if (response.isCommitted()) {
return Mono.error(ex);
}
// Set HTTP header
// Set HTTP header, major browsers like Chrome now comply with the specification and interpret correctly
// UTF-8 special characters without requiring a charset=UTF-8 parameter.
response.getHeaders().setContentType(MediaType.APPLICATION_JSON);
return response.writeWith(Mono.fromSupplier(() -> {
DataBufferFactory bufferFactory = response.bufferFactory();
Expand All @@ -71,7 +72,8 @@ private ResponseBodyBean<?> setResponseBody(ServerHttpResponse response, Throwab
return ResponseBodyBean.ofStatus(((ResponseStatusException) ex).getStatus());
} else if (ex instanceof HystrixRuntimeException) {
response.setStatusCode(HttpStatus.SERVICE_UNAVAILABLE);
return ResponseBodyBean.ofStatus(HttpStatus.SERVICE_UNAVAILABLE, ex.getMessage());
return ResponseBodyBean.ofStatus(HttpStatus.SERVICE_UNAVAILABLE,
String.format("%s %s", ex.getMessage(), ex.getCause().getMessage()));
}
response.setStatusCode(HttpStatus.INTERNAL_SERVER_ERROR);
return ResponseBodyBean.ofStatus(HttpStatus.INTERNAL_SERVER_ERROR, ex.getMessage());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import lombok.val;
import org.springframework.core.io.buffer.DataBufferFactory;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.lang.Nullable;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;
Expand All @@ -33,7 +34,9 @@ public static Mono<Void> renderJson(@NonNull ServerWebExchange exchange, @NonNul
exchange.getResponse().setStatusCode(httpStatus);
val response = exchange.getResponse();
response.setStatusCode(httpStatus);
response.getHeaders().set("Content-Type", "application/json;charset=UTF-8");
// Set HTTP header, major browsers like Chrome now comply with the specification and interpret correctly
// UTF-8 special characters without requiring a charset=UTF-8 parameter.
response.getHeaders().setContentType(MediaType.APPLICATION_JSON);
return response.writeWith(Mono.fromSupplier(() -> {
DataBufferFactory bufferFactory = response.bufferFactory();
final var responseBody = ResponseBodyBean.ofStatus(httpStatus, data);
Expand Down

0 comments on commit 886124a

Please sign in to comment.