下面列出了org.springframework.http.HttpStatus#getReasonPhrase ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private String getExceptionMessage(Throwable throwable, Integer statusCode) {
if (throwable != null) {
return Throwables.getRootCause(throwable).getMessage();
}
HttpStatus httpStatus = HttpStatus.valueOf(statusCode);
return httpStatus.getReasonPhrase();
}
private ClientHttpResponse response(final HttpStatus status) {
String msg="该"+SERVER_NAME+"服务暂时不可用!";
return new ClientHttpResponse() {
@Override
public HttpStatus getStatusCode() throws IOException {
return status;
}
@Override
public int getRawStatusCode() throws IOException {
return status.value();
}
@Override
public String getStatusText() throws IOException {
return status.getReasonPhrase();
}
@Override
public void close() {
}
@Override
public InputStream getBody() throws IOException {
//可替换成相应的json串的 看业务规定了
return new ByteArrayInputStream(msg.getBytes());
}
@Override
public HttpHeaders getHeaders() {
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
return headers;
}
};
}
private ClientHttpResponse response(final HttpStatus status) {
return new ClientHttpResponse() {
@Override
public HttpStatus getStatusCode() {
return status;
}
@Override
public int getRawStatusCode() {
return status.value();
}
@Override
public String getStatusText() {
return status.getReasonPhrase();
}
@Override
public void close() {
log.info("close");
}
@Override
public InputStream getBody() {
String message = "{\n" +
"\"code\": 200,\n" +
"\"message\": \"微服务故障, 请稍后再试\"\n" +
"}";
return new ByteArrayInputStream(message.getBytes());
}
@Override
public HttpHeaders getHeaders() {
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
return headers;
}
};
}
@Test
public void testWithV2Error() throws IOException {
HttpStatus statusCode = HttpStatus.UNPROCESSABLE_ENTITY;
ClientHttpResponseMock response = new ClientHttpResponseMock(statusCode, getClass().getResourceAsStream("v2-error.json"));
CloudOperationException expectedException = new CloudOperationException(statusCode,
statusCode.getReasonPhrase(),
"Request invalid due to parse error: Field: name, Error: Missing field name, Field: space_guid, Error: Missing field space_guid, Field: service_plan_guid, Error: Missing field service_plan_guid");
testWithError(response, expectedException);
}
@Test
public void testWithV3Error() throws IOException {
HttpStatus statusCode = HttpStatus.BAD_REQUEST;
ClientHttpResponseMock response = new ClientHttpResponseMock(statusCode, getClass().getResourceAsStream("v3-error.json"));
CloudOperationException expectedException = new CloudOperationException(statusCode,
statusCode.getReasonPhrase(),
"memory_in_mb exceeds organization memory quota\ndisk_in_mb exceeds organization disk quota");
testWithError(response, expectedException);
}
@Test
public void testWithInvalidError() throws IOException {
HttpStatus statusCode = HttpStatus.BAD_REQUEST;
ClientHttpResponseMock response = new ClientHttpResponseMock(statusCode, toInputStream("blabla"));
CloudOperationException expectedException = new CloudOperationException(statusCode, statusCode.getReasonPhrase());
testWithError(response, expectedException);
}
@Test
public void testWithEmptyResponse() throws IOException {
HttpStatus statusCode = HttpStatus.BAD_REQUEST;
ClientHttpResponseMock response = new ClientHttpResponseMock(statusCode, toInputStream("{ }"));
CloudOperationException expectedException = new CloudOperationException(statusCode, statusCode.getReasonPhrase());
testWithError(response, expectedException);
}
GriffinExceptionResponse(HttpStatus status, String message, String path,
String exception) {
this.status = status.value();
this.error = status.getReasonPhrase();
this.message = message;
this.path = path;
this.exception = exception;
}
private ClientHttpResponse response(final HttpStatus status) {
return new ClientHttpResponse() {
@Override
public HttpStatus getStatusCode() throws IOException {
return status;
}
@Override
public int getRawStatusCode() throws IOException {
return status.value();
}
@Override
public String getStatusText() throws IOException {
return status.getReasonPhrase();
}
@Override
public void close() {
}
@Override
public InputStream getBody() throws IOException {
return new ByteArrayInputStream("服务不可用,请稍后再试。".getBytes());
}
@Override
public HttpHeaders getHeaders() {
// headers设定
HttpHeaders headers = new HttpHeaders();
MediaType mt = new MediaType("application", "json", Charset.forName("UTF-8"));
headers.setContentType(mt);
return headers;
}
};
}
@RequestMapping("error")
public @ResponseBody APIResponse customError(HttpServletRequest request, HttpServletResponse response) {
Integer statusCode = (Integer) request.getAttribute("javax.servlet.error.status_code");
Throwable throwable = (Throwable) request.getAttribute("javax.servlet.error.exception");
String exceptionMessage = (String) request.getAttribute("javax.servlet.error.message");
HttpStatus httpStatus = HttpStatus.valueOf(statusCode);
String httpReason = httpStatus.getReasonPhrase();
String requestUri = (String) request.getAttribute("javax.servlet.error.request_uri");
if (requestUri == null) {
requestUri = "Unknown";
}
Map<String, Object> resp = new HashMap<>();
String message = MessageFormat.format("{0} {1} returned for {2} with message: {3}",
statusCode, httpReason, requestUri, exceptionMessage);
resp.put("message", message);
resp.put("cause", exceptionMessage);
resp.put("exceptionRootCauseMessage", ExceptionUtils.getRootCauseMessage(throwable));
resp.put("stacktrace", ExceptionUtils.getRootCauseStackTrace(throwable));
LOG.error(message);
ExceptionUtils.printRootCauseStackTrace(throwable);
LOG.error(ExceptionUtils.getFullStackTrace(throwable));
return APIResponse.toExceptionResponse(exceptionMessage, resp);
}
public ApiError(HttpStatus httpStatus, String message, String description) {
statusCode = httpStatus.value();
meaning = httpStatus.getReasonPhrase();
this.message = message;
this.description = description;
}
public MessageResponse(HttpStatus status) {
super();
this.code = status.value();
this.message = status.getReasonPhrase();
}
private ClientHttpResponse response(final HttpStatus status){
return new ClientHttpResponse() {
@Override
public HttpStatus getStatusCode() throws IOException {
return status;
}
@Override
public int getRawStatusCode() throws IOException{
return status.value();
}
@Override
public String getStatusText() throws IOException{
return status.getReasonPhrase();
}
@Override
public void close(){
}
/**
* 当 springms-provider-user 微服务出现宕机后,客户端再请求时候就会返回 fallback 等字样的字符串提示;
* 但对于复杂一点的微服务,我们这里就得好好琢磨该怎么友好提示给用户了;
* 如果请求用户服务失败,返回什么信息给消费者客户端
* @return
* @throws IOException
*/
@Override
public InputStream getBody() throws IOException {
JSONObject r = new JSONObject();
try {
r.put("resp_code", "9999");
r.put("resp_msg", "系统错误,请求失败");
} catch (JSONException e) {
e.printStackTrace();
}
return new ByteArrayInputStream(r.toString().getBytes("UTF-8"));
}
@Override
public HttpHeaders getHeaders(){
// headers设定
HttpHeaders headers = new HttpHeaders();
MediaType mt = new MediaType("application", "json", Charset.forName("UTF-8"));
headers.setContentType(mt);
return headers;
}
};
}
@Override
public String getReasonPhrase(int statusCode) {
HttpStatus status = HttpStatus.resolve(statusCode);
return status != null ? status.getReasonPhrase() : null;
}
public ApiError(HttpStatus httpStatus, String message, String description) {
statusCode = httpStatus.value();
meaning = httpStatus.getReasonPhrase();
this.message = message;
this.description = description;
}
private CloudOperationException convertV3ClientException(AbstractCloudFoundryException e) {
HttpStatus httpStatus = HttpStatus.valueOf(e.getStatusCode());
return new CloudOperationException(httpStatus, httpStatus.getReasonPhrase(), e.getMessage(), e);
}
public CloudOperationException(HttpStatus statusCode) {
this(statusCode, statusCode.getReasonPhrase());
}
@Override
public String format( HttpStatus httpStatus, Annotation... annotations ) {
return httpStatus.getReasonPhrase() + " (" + httpStatus.value() + ")";
}
public ApiError(HttpStatus httpStatus, String message, String description) {
statusCode = httpStatus.value();
meaning = httpStatus.getReasonPhrase();
this.message = message;
this.description = description;
}
private void getDataFromHttpStatus(final HttpStatus status) {
this.status = status.value();
this.reason = status.getReasonPhrase();
}