[重要]:框架中不会再抛出401的http状态码 && 添加RestTemplate的错误处理handler,解决在rest接口报错的情况下,无法拿到responseBody的问题

This commit is contained in:
wangkang
2017-11-13 10:58:29 +08:00
parent 579b82dda1
commit 6d7b4d6653
5 changed files with 77 additions and 5 deletions
@@ -1,5 +1,6 @@
package org.itkk.udf.core;
import org.itkk.udf.core.exception.handle.RmsResponseErrorHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.cloud.context.config.annotation.RefreshScope;
@@ -39,7 +40,9 @@ public class BaseApplication {
*/
@Bean(name = "externalRestTemplate")
public RestTemplate externalRestTemplate(ClientHttpRequestFactory requestFactory) {
return new RestTemplate(requestFactory);
RestTemplate restTemplate = new RestTemplate(requestFactory);
restTemplate.setErrorHandler(new RmsResponseErrorHandler());
return restTemplate;
}
/**
@@ -146,7 +146,6 @@ public class ExceptionController extends AbstractErrorController {
* @return RestResponse
*/
private RestResponse<String> getRestResponse(HttpServletRequest request, HttpStatus status, Map<String, Object> body) { //NOSONAR
log.info(body.toString());
ErrorResult errorResult = new ErrorResult();
if (status == HttpStatus.NOT_FOUND) { //404处理
errorResult.setType(NoHandlerFoundException.class.getName());
@@ -160,7 +159,7 @@ public class ExceptionController extends AbstractErrorController {
if (exception.getCause() instanceof PermissionException) {
status = HttpStatus.FORBIDDEN; //NOSONAR
} else if (exception.getCause() instanceof AuthException) {
status = HttpStatus.UNAUTHORIZED; //NOSONAR
status = HttpStatus.FORBIDDEN; //NOSONAR
}
errorResult = ExceptionHandle.buildError(applicationConfig, exception.getCause());
} else {
@@ -72,7 +72,7 @@ public class ExceptionHandle extends ResponseEntityExceptionHandler {
if (ex instanceof PermissionException) { //权限异常
localHttpStatus = HttpStatus.FORBIDDEN;
} else if (ex instanceof AuthException) { //认证异常
localHttpStatus = HttpStatus.UNAUTHORIZED;
localHttpStatus = HttpStatus.FORBIDDEN;
} else if (ex instanceof ParameterValidException) { //参数校验异常
localHttpStatus = HttpStatus.BAD_REQUEST;
} else if (ex instanceof RestClientResponseException) { //rest请求异常
@@ -0,0 +1,67 @@
package org.itkk.udf.core.exception.handle;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.client.ClientHttpResponse;
import org.springframework.web.client.DefaultResponseErrorHandler;
import org.springframework.web.client.HttpClientErrorException;
import org.springframework.web.client.HttpServerErrorException;
import org.springframework.web.client.RestClientException;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
/**
* RmsResponseErrorHandler
*/
@Slf4j
public class RmsResponseErrorHandler extends DefaultResponseErrorHandler {
@Override
public void handleError(ClientHttpResponse response) throws IOException {
//获得错误信息
String responseBody = getData(response.getBody());
HttpStatus statusCode = getHttpStatusCode(response);
switch (statusCode.series()) {
case CLIENT_ERROR:
throw new HttpClientErrorException(statusCode, response.getStatusText(),
response.getHeaders(), responseBody.getBytes("UTF-8"), getCharset(response));
case SERVER_ERROR:
throw new HttpServerErrorException(statusCode, response.getStatusText(),
response.getHeaders(), responseBody.getBytes("UTF-8"), getCharset(response));
default:
throw new RestClientException("Unknown status code [" + statusCode + "]");
}
}
/**
* 读取返回的信息
*
* @param in 输入流
* @return 数据
* @throws IOException 异常
*/
private String getData(InputStream in) throws IOException {
String result = "";
StringBuilder sb = new StringBuilder();
BufferedReader br = new BufferedReader(new InputStreamReader(in, "utf-8"));
String line;
try {
while ((line = br.readLine()) != null) {
sb.append(line);
}
br.close();
} finally {
if (result != null) {
try {
br.close();
} catch (IOException e) {
log.error("getData", e);
}
}
}
return sb.toString();
}
}
@@ -7,6 +7,7 @@
package org.itkk.udf.rms;
import lombok.Data;
import org.itkk.udf.core.exception.handle.RmsResponseErrorHandler;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
@@ -48,7 +49,9 @@ public class RmsConfig {
@LoadBalanced
@Primary
public RestTemplate restTemplate(ClientHttpRequestFactory requestFactory) {
return new RestTemplate(requestFactory);
RestTemplate restTemplate = new RestTemplate(requestFactory);
restTemplate.setErrorHandler(new RmsResponseErrorHandler());
return restTemplate;
}
/**