Search in sources :

Example 1 with HttpStatus

use of org.springframework.http.HttpStatus in project java-chassis by ServiceComb.

the class SpringmvcConsumerResponseMapper method mapResponse.

@Override
public Object mapResponse(Response response) {
    HttpStatus status = HttpStatus.valueOf(response.getStatusCode());
    HttpHeaders httpHeaders = null;
    Map<String, List<Object>> headers = response.getHeaders().getHeaderMap();
    if (headers != null) {
        httpHeaders = new HttpHeaders();
        for (Entry<String, List<Object>> entry : headers.entrySet()) {
            for (Object value : entry.getValue()) {
                httpHeaders.add(entry.getKey(), String.valueOf(value));
            }
        }
    }
    return new ResponseEntity<Object>(response.getResult(), httpHeaders, status);
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) ResponseEntity(org.springframework.http.ResponseEntity) HttpStatus(org.springframework.http.HttpStatus) List(java.util.List)

Example 2 with HttpStatus

use of org.springframework.http.HttpStatus in project rhino by PLOS.

the class RestController method reportClientError.

/**
   * Report an error condition to the REST client. The brief error message is sent as the response body, with the
   * response code specified when the exception object was created. The stack trace is not included because we generally
   * expect the client to fix the error with a simple change to input.
   *
   * @param e the exception that Spring wants to handle
   * @return the RESTful response body
   */
@ExceptionHandler(RestClientException.class)
public ResponseEntity<String> reportClientError(RestClientException e) {
    HttpStatus status = e.getResponseStatus();
    log.info("Reporting error to client (" + status + ")", e);
    String message = e.getMessage().trim() + '\n';
    return respondWithPlainText(message, status);
}
Also used : HttpStatus(org.springframework.http.HttpStatus) ExceptionHandler(org.springframework.web.bind.annotation.ExceptionHandler)

Example 3 with HttpStatus

use of org.springframework.http.HttpStatus in project spring-boot by spring-projects.

the class HealthMvcEndpoint method invoke.

@ActuatorGetMapping
@ResponseBody
public Object invoke(HttpServletRequest request, Principal principal) {
    if (!getDelegate().isEnabled()) {
        // Shouldn't happen because the request mapping should not be registered
        return getDisabledResponse();
    }
    Health health = getHealth(request, principal);
    HttpStatus status = getStatus(health);
    if (status != null) {
        return new ResponseEntity<>(health, status);
    }
    return health;
}
Also used : ResponseEntity(org.springframework.http.ResponseEntity) Health(org.springframework.boot.actuate.health.Health) HttpStatus(org.springframework.http.HttpStatus) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Example 4 with HttpStatus

use of org.springframework.http.HttpStatus in project spring-framework by spring-projects.

the class RestTemplateTests method mapNullTemplateVariable.

@Test
public void mapNullTemplateVariable() throws Exception {
    given(requestFactory.createRequest(new URI("http://example.com/-foo"), HttpMethod.GET)).willReturn(request);
    given(request.execute()).willReturn(response);
    given(errorHandler.hasError(response)).willReturn(false);
    HttpStatus status = HttpStatus.OK;
    given(response.getStatusCode()).willReturn(status);
    given(response.getStatusText()).willReturn(status.getReasonPhrase());
    Map<String, String> vars = new HashMap<>(2);
    vars.put("first", null);
    vars.put("last", "foo");
    template.execute("http://example.com/{first}-{last}", HttpMethod.GET, null, null, vars);
    verify(response).close();
}
Also used : HttpStatus(org.springframework.http.HttpStatus) HashMap(java.util.HashMap) URI(java.net.URI) Test(org.junit.Test)

Example 5 with HttpStatus

use of org.springframework.http.HttpStatus in project spring-framework by spring-projects.

the class RestTemplateTests method delete.

@Test
public void delete() throws Exception {
    given(requestFactory.createRequest(new URI("http://example.com"), HttpMethod.DELETE)).willReturn(request);
    given(request.execute()).willReturn(response);
    given(errorHandler.hasError(response)).willReturn(false);
    HttpStatus status = HttpStatus.OK;
    given(response.getStatusCode()).willReturn(status);
    given(response.getStatusText()).willReturn(status.getReasonPhrase());
    template.delete("http://example.com");
    verify(response).close();
}
Also used : HttpStatus(org.springframework.http.HttpStatus) URI(java.net.URI) Test(org.junit.Test)

Aggregations

HttpStatus (org.springframework.http.HttpStatus)165 ResponseEntity (org.springframework.http.ResponseEntity)43 HttpHeaders (org.springframework.http.HttpHeaders)38 Test (org.junit.jupiter.api.Test)26 MediaType (org.springframework.http.MediaType)17 Mono (reactor.core.publisher.Mono)17 IOException (java.io.IOException)16 ExceptionHandler (org.springframework.web.bind.annotation.ExceptionHandler)15 Collections (java.util.Collections)13 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)13 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)13 URI (java.net.URI)12 List (java.util.List)11 Optional (java.util.Optional)11 Test (org.junit.Test)11 Map (java.util.Map)10 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)9 ClassPathResource (org.springframework.core.io.ClassPathResource)9 Resource (org.springframework.core.io.Resource)9 HashMap (java.util.HashMap)8