Commit d222157624d657d830f1cf4cbd883eae91ed1ab8

Authored by 云中非
1 parent 05235187

fix(DEFECT-718): 消息提示,中文乱码问题

@@ -104,6 +104,7 @@ public class ThingsboardErrorResponseHandler extends ResponseEntityExceptionHand @@ -104,6 +104,7 @@ public class ThingsboardErrorResponseHandler extends ResponseEntityExceptionHand
104 public void handle(HttpServletRequest request, HttpServletResponse response, 104 public void handle(HttpServletRequest request, HttpServletResponse response,
105 AccessDeniedException accessDeniedException) throws IOException, 105 AccessDeniedException accessDeniedException) throws IOException,
106 ServletException { 106 ServletException {
  107 + response.setCharacterEncoding("utf-8");
107 if (!response.isCommitted()) { 108 if (!response.isCommitted()) {
108 response.setContentType(MediaType.APPLICATION_JSON_VALUE); 109 response.setContentType(MediaType.APPLICATION_JSON_VALUE);
109 response.setStatus(HttpStatus.FORBIDDEN.value()); 110 response.setStatus(HttpStatus.FORBIDDEN.value());
@@ -168,11 +169,13 @@ public class ThingsboardErrorResponseHandler extends ResponseEntityExceptionHand @@ -168,11 +169,13 @@ public class ThingsboardErrorResponseHandler extends ResponseEntityExceptionHand
168 ThingsboardErrorCode errorCode = thingsboardException.getErrorCode(); 169 ThingsboardErrorCode errorCode = thingsboardException.getErrorCode();
169 HttpStatus status = errorCodeToStatus(errorCode); 170 HttpStatus status = errorCodeToStatus(errorCode);
170 response.setStatus(status.value()); 171 response.setStatus(status.value());
  172 + response.setCharacterEncoding("utf-8");
171 mapper.writeValue(response.getWriter(), ThingsboardErrorResponse.of(thingsboardException.getMessage(), errorCode, status)); 173 mapper.writeValue(response.getWriter(), ThingsboardErrorResponse.of(thingsboardException.getMessage(), errorCode, status));
172 } 174 }
173 175
174 private void handleRateLimitException(HttpServletResponse response, TbRateLimitsException exception) throws IOException { 176 private void handleRateLimitException(HttpServletResponse response, TbRateLimitsException exception) throws IOException {
175 response.setStatus(HttpStatus.TOO_MANY_REQUESTS.value()); 177 response.setStatus(HttpStatus.TOO_MANY_REQUESTS.value());
  178 + response.setCharacterEncoding("utf-8");
176 String message = "Too many requests for current " + exception.getEntityType().name().toLowerCase() + "!"; 179 String message = "Too many requests for current " + exception.getEntityType().name().toLowerCase() + "!";
177 mapper.writeValue(response.getWriter(), 180 mapper.writeValue(response.getWriter(),
178 ThingsboardErrorResponse.of(message, 181 ThingsboardErrorResponse.of(message,
@@ -181,11 +184,13 @@ public class ThingsboardErrorResponseHandler extends ResponseEntityExceptionHand @@ -181,11 +184,13 @@ public class ThingsboardErrorResponseHandler extends ResponseEntityExceptionHand
181 184
182 private void handleSubscriptionException(ThingsboardException subscriptionException, HttpServletResponse response) throws IOException { 185 private void handleSubscriptionException(ThingsboardException subscriptionException, HttpServletResponse response) throws IOException {
183 response.setStatus(HttpStatus.FORBIDDEN.value()); 186 response.setStatus(HttpStatus.FORBIDDEN.value());
  187 + response.setCharacterEncoding("utf-8");
184 mapper.writeValue(response.getWriter(), 188 mapper.writeValue(response.getWriter(),
185 (new ObjectMapper()).readValue(((HttpClientErrorException) subscriptionException.getCause()).getResponseBodyAsByteArray(), Object.class)); 189 (new ObjectMapper()).readValue(((HttpClientErrorException) subscriptionException.getCause()).getResponseBodyAsByteArray(), Object.class));
186 } 190 }
187 191
188 private void handleAccessDeniedException(HttpServletResponse response) throws IOException { 192 private void handleAccessDeniedException(HttpServletResponse response) throws IOException {
  193 + response.setCharacterEncoding("utf-8");
189 response.setStatus(HttpStatus.FORBIDDEN.value()); 194 response.setStatus(HttpStatus.FORBIDDEN.value());
190 mapper.writeValue(response.getWriter(), 195 mapper.writeValue(response.getWriter(),
191 ThingsboardErrorResponse.of(ErrorMessage.NOT_HAVE_PERMISSION.getMessage(), 196 ThingsboardErrorResponse.of(ErrorMessage.NOT_HAVE_PERMISSION.getMessage(),
@@ -195,6 +200,7 @@ public class ThingsboardErrorResponseHandler extends ResponseEntityExceptionHand @@ -195,6 +200,7 @@ public class ThingsboardErrorResponseHandler extends ResponseEntityExceptionHand
195 200
196 private void handleAuthenticationException(AuthenticationException authenticationException, HttpServletResponse response) throws IOException { 201 private void handleAuthenticationException(AuthenticationException authenticationException, HttpServletResponse response) throws IOException {
197 response.setStatus(HttpStatus.UNAUTHORIZED.value()); 202 response.setStatus(HttpStatus.UNAUTHORIZED.value());
  203 + response.setCharacterEncoding("utf-8");
198 if (authenticationException instanceof BadCredentialsException || authenticationException instanceof UsernameNotFoundException) { 204 if (authenticationException instanceof BadCredentialsException || authenticationException instanceof UsernameNotFoundException) {
199 mapper.writeValue(response.getWriter(), ThingsboardErrorResponse.of(ErrorMessage.USERNAME_PASSWORD_INCORRECT.getMessage(), ThingsboardErrorCode.AUTHENTICATION, HttpStatus.UNAUTHORIZED)); 205 mapper.writeValue(response.getWriter(), ThingsboardErrorResponse.of(ErrorMessage.USERNAME_PASSWORD_INCORRECT.getMessage(), ThingsboardErrorCode.AUTHENTICATION, HttpStatus.UNAUTHORIZED));
200 } else if (authenticationException instanceof DisabledException) { 206 } else if (authenticationException instanceof DisabledException) {