Java Code Examples for org.springframework.http.HttpStatus#getReasonPhrase()

The following examples show how to use org.springframework.http.HttpStatus#getReasonPhrase() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CloudControllerResponseErrorHandlerTest.java    From cf-java-client-sap with Apache License 2.0 5 votes vote down vote up
@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);
}
 
Example 2
Source File: ErrorController.java    From spring-mvc-angular-js-hibernate-bootstrap-java-single-page-jwt-auth-rest-api-webapp-framework with MIT License 5 votes vote down vote up
@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);
}
 
Example 3
Source File: MyFallbackProvider.java    From spring-cloud-docker-microservice-book-code with Apache License 2.0 5 votes vote down vote up
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;
    }
  };
}
 
Example 4
Source File: GriffinExceptionResponse.java    From griffin with Apache License 2.0 5 votes vote down vote up
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;
}
 
Example 5
Source File: CloudControllerResponseErrorHandlerTest.java    From cf-java-client-sap with Apache License 2.0 5 votes vote down vote up
@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);
}
 
Example 6
Source File: CloudControllerResponseErrorHandlerTest.java    From cf-java-client-sap with Apache License 2.0 5 votes vote down vote up
@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);
}
 
Example 7
Source File: CloudControllerResponseErrorHandlerTest.java    From cf-java-client-sap with Apache License 2.0 5 votes vote down vote up
@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);
}
 
Example 8
Source File: UacFallbackProvider.java    From paascloud-master with Apache License 2.0 5 votes vote down vote up
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;
		}
	};
}
 
Example 9
Source File: MyFallback.java    From springcloud-study with Apache License 2.0 5 votes vote down vote up
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;
          }
      };
  }
 
Example 10
Source File: CustomErrorController.java    From thymeleafexamples-layouts with Apache License 2.0 5 votes vote down vote up
private String getExceptionMessage(Throwable throwable, Integer statusCode) {
	if (throwable != null) {
		return Throwables.getRootCause(throwable).getMessage();
	}
	HttpStatus httpStatus = HttpStatus.valueOf(statusCode);
	return httpStatus.getReasonPhrase();
}
 
Example 11
Source File: ApiError.java    From urltodrive with MIT License 4 votes vote down vote up
public ApiError(HttpStatus httpStatus, String message, String description) {
    statusCode = httpStatus.value();
    meaning = httpStatus.getReasonPhrase();
    this.message = message;
    this.description = description;
}
 
Example 12
Source File: CloudControllerClientImpl.java    From cf-java-client-sap with Apache License 2.0 4 votes vote down vote up
private CloudOperationException convertV3ClientException(AbstractCloudFoundryException e) {
    HttpStatus httpStatus = HttpStatus.valueOf(e.getStatusCode());
    return new CloudOperationException(httpStatus, httpStatus.getReasonPhrase(), e.getMessage(), e);
}
 
Example 13
Source File: CloudOperationException.java    From cf-java-client-sap with Apache License 2.0 4 votes vote down vote up
public CloudOperationException(HttpStatus statusCode) {
    this(statusCode, statusCode.getReasonPhrase());
}
 
Example 14
Source File: ApiError.java    From Bhadoo-Cloud-Drive with MIT License 4 votes vote down vote up
public ApiError(HttpStatus httpStatus, String message, String description) {
    statusCode = httpStatus.value();
    meaning = httpStatus.getReasonPhrase();
    this.message = message;
    this.description = description;
}
 
Example 15
Source File: SpringAnnotationScanner.java    From smallrye-open-api with Apache License 2.0 4 votes vote down vote up
@Override
public String getReasonPhrase(int statusCode) {
    HttpStatus status = HttpStatus.resolve(statusCode);
    return status != null ? status.getReasonPhrase() : null;
}
 
Example 16
Source File: HttpStatusFormatter.java    From JGiven with Apache License 2.0 4 votes vote down vote up
@Override
public String format( HttpStatus httpStatus, Annotation... annotations ) {
    return httpStatus.getReasonPhrase() + " (" + httpStatus.value() + ")";
}
 
Example 17
Source File: ApiGatewayFallbackProvider.java    From open-capacity-platform with Apache License 2.0 4 votes vote down vote up
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;
			}

		};

	}
 
Example 18
Source File: ApiError.java    From cloud-transfer-backend with MIT License 4 votes vote down vote up
public ApiError(HttpStatus httpStatus, String message, String description) {
    statusCode = httpStatus.value();
    meaning = httpStatus.getReasonPhrase();
    this.message = message;
    this.description = description;
}
 
Example 19
Source File: MessageResponse.java    From acelera-dev-sp-2019 with Apache License 2.0 4 votes vote down vote up
public MessageResponse(HttpStatus status) {
	super();
	this.code = status.value();
	this.message = status.getReasonPhrase();
}
 
Example 20
Source File: XAPIErrorInfo.java    From OpenLRW with Educational Community License v2.0 4 votes vote down vote up
private void getDataFromHttpStatus(final HttpStatus status) {
    this.status = status.value();
    this.reason = status.getReasonPhrase();
}