Search in sources :

Example 31 with HttpClientErrorException

use of org.springframework.web.client.HttpClientErrorException in project tutorials by eugenp.

the class StoreApi method placeOrder.

/**
 * Place an order for a pet
 *
 * <p><b>200</b> - successful operation
 * <p><b>400</b> - Invalid Order
 * @param body order placed for purchasing the pet
 * @return Order
 * @throws RestClientException if an error occurs while attempting to invoke the API
 */
public Order placeOrder(Order body) throws RestClientException {
    Object postBody = body;
    // verify the required parameter 'body' is set
    if (body == null) {
        throw new HttpClientErrorException(HttpStatus.BAD_REQUEST, "Missing the required parameter 'body' when calling placeOrder");
    }
    String path = UriComponentsBuilder.fromPath("/store/order").build().toUriString();
    final MultiValueMap<String, String> queryParams = new LinkedMultiValueMap<String, String>();
    final HttpHeaders headerParams = new HttpHeaders();
    final MultiValueMap<String, Object> formParams = new LinkedMultiValueMap<String, Object>();
    final String[] accepts = { "application/xml", "application/json" };
    final List<MediaType> accept = apiClient.selectHeaderAccept(accepts);
    final String[] contentTypes = {};
    final MediaType contentType = apiClient.selectHeaderContentType(contentTypes);
    String[] authNames = new String[] {};
    ParameterizedTypeReference<Order> returnType = new ParameterizedTypeReference<Order>() {
    };
    return apiClient.invokeAPI(path, HttpMethod.POST, queryParams, postBody, headerParams, formParams, accept, contentType, authNames, returnType);
}
Also used : Order(com.baeldung.petstore.client.model.Order) HttpHeaders(org.springframework.http.HttpHeaders) HttpClientErrorException(org.springframework.web.client.HttpClientErrorException) LinkedMultiValueMap(org.springframework.util.LinkedMultiValueMap) ParameterizedTypeReference(org.springframework.core.ParameterizedTypeReference) MediaType(org.springframework.http.MediaType)

Example 32 with HttpClientErrorException

use of org.springframework.web.client.HttpClientErrorException in project tutorials by eugenp.

the class UserApi method createUsersWithListInput.

/**
 * Creates list of users with given input array
 *
 * <p><b>0</b> - successful operation
 * @param body List of user object
 * @throws RestClientException if an error occurs while attempting to invoke the API
 */
public void createUsersWithListInput(List<User> body) throws RestClientException {
    Object postBody = body;
    // verify the required parameter 'body' is set
    if (body == null) {
        throw new HttpClientErrorException(HttpStatus.BAD_REQUEST, "Missing the required parameter 'body' when calling createUsersWithListInput");
    }
    String path = UriComponentsBuilder.fromPath("/user/createWithList").build().toUriString();
    final MultiValueMap<String, String> queryParams = new LinkedMultiValueMap<String, String>();
    final HttpHeaders headerParams = new HttpHeaders();
    final MultiValueMap<String, Object> formParams = new LinkedMultiValueMap<String, Object>();
    final String[] accepts = { "application/xml", "application/json" };
    final List<MediaType> accept = apiClient.selectHeaderAccept(accepts);
    final String[] contentTypes = {};
    final MediaType contentType = apiClient.selectHeaderContentType(contentTypes);
    String[] authNames = new String[] {};
    ParameterizedTypeReference<Void> returnType = new ParameterizedTypeReference<Void>() {
    };
    apiClient.invokeAPI(path, HttpMethod.POST, queryParams, postBody, headerParams, formParams, accept, contentType, authNames, returnType);
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) HttpClientErrorException(org.springframework.web.client.HttpClientErrorException) LinkedMultiValueMap(org.springframework.util.LinkedMultiValueMap) ParameterizedTypeReference(org.springframework.core.ParameterizedTypeReference) MediaType(org.springframework.http.MediaType)

Example 33 with HttpClientErrorException

use of org.springframework.web.client.HttpClientErrorException in project tutorials by eugenp.

the class UserApi method createUser.

/**
 * Create user
 * This can only be done by the logged in user.
 * <p><b>0</b> - successful operation
 * @param body Created user object
 * @throws RestClientException if an error occurs while attempting to invoke the API
 */
public void createUser(User body) throws RestClientException {
    Object postBody = body;
    // verify the required parameter 'body' is set
    if (body == null) {
        throw new HttpClientErrorException(HttpStatus.BAD_REQUEST, "Missing the required parameter 'body' when calling createUser");
    }
    String path = UriComponentsBuilder.fromPath("/user").build().toUriString();
    final MultiValueMap<String, String> queryParams = new LinkedMultiValueMap<String, String>();
    final HttpHeaders headerParams = new HttpHeaders();
    final MultiValueMap<String, Object> formParams = new LinkedMultiValueMap<String, Object>();
    final String[] accepts = { "application/xml", "application/json" };
    final List<MediaType> accept = apiClient.selectHeaderAccept(accepts);
    final String[] contentTypes = {};
    final MediaType contentType = apiClient.selectHeaderContentType(contentTypes);
    String[] authNames = new String[] {};
    ParameterizedTypeReference<Void> returnType = new ParameterizedTypeReference<Void>() {
    };
    apiClient.invokeAPI(path, HttpMethod.POST, queryParams, postBody, headerParams, formParams, accept, contentType, authNames, returnType);
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) HttpClientErrorException(org.springframework.web.client.HttpClientErrorException) LinkedMultiValueMap(org.springframework.util.LinkedMultiValueMap) ParameterizedTypeReference(org.springframework.core.ParameterizedTypeReference) MediaType(org.springframework.http.MediaType)

Example 34 with HttpClientErrorException

use of org.springframework.web.client.HttpClientErrorException in project tutorials by eugenp.

the class RestTemplateBasicLiveTest method givenFooService_whenCallDelete_thenEntityIsRemoved.

// DELETE
@Test
public void givenFooService_whenCallDelete_thenEntityIsRemoved() {
    final Foo foo = new Foo("remove me");
    final ResponseEntity<Foo> response = restTemplate.postForEntity(fooResourceUrl, foo, Foo.class);
    assertThat(response.getStatusCode(), is(HttpStatus.CREATED));
    final String entityUrl = fooResourceUrl + "/" + response.getBody().getId();
    restTemplate.delete(entityUrl);
    try {
        restTemplate.getForEntity(entityUrl, Foo.class);
        fail();
    } catch (final HttpClientErrorException ex) {
        assertThat(ex.getStatusCode(), is(HttpStatus.NOT_FOUND));
    }
}
Also used : HttpClientErrorException(org.springframework.web.client.HttpClientErrorException) Foo(org.baeldung.web.dto.Foo) Test(org.junit.Test)

Example 35 with HttpClientErrorException

use of org.springframework.web.client.HttpClientErrorException in project openlmis-stockmanagement by OpenLMIS.

the class BaseReferenceDataServiceTest method shouldThrowExceptionIfThereIsOtherProblemWithFindingById.

@Test(expected = DataRetrievalException.class)
public void shouldThrowExceptionIfThereIsOtherProblemWithFindingById() {
    // given
    BaseReferenceDataService<T> service = prepareService();
    UUID id = UUID.randomUUID();
    // when
    when(restTemplate.exchange(any(URI.class), eq(HttpMethod.GET), any(HttpEntity.class), eq(service.getResultClass()))).thenThrow(new HttpClientErrorException(HttpStatus.BAD_REQUEST));
    service.findOne(id);
}
Also used : HttpClientErrorException(org.springframework.web.client.HttpClientErrorException) HttpEntity(org.springframework.http.HttpEntity) UUID(java.util.UUID) URI(java.net.URI) BaseCommunicationServiceTest(org.openlmis.stockmanagement.service.BaseCommunicationServiceTest) Test(org.junit.Test)

Aggregations

HttpClientErrorException (org.springframework.web.client.HttpClientErrorException)113 HttpHeaders (org.springframework.http.HttpHeaders)31 Test (org.junit.Test)25 LinkedMultiValueMap (org.springframework.util.LinkedMultiValueMap)24 HashMap (java.util.HashMap)21 HttpServerErrorException (org.springframework.web.client.HttpServerErrorException)21 MediaType (org.springframework.http.MediaType)19 RestTemplate (org.springframework.web.client.RestTemplate)19 ParameterizedTypeReference (org.springframework.core.ParameterizedTypeReference)18 HttpEntity (org.springframework.http.HttpEntity)16 URI (java.net.URI)15 Map (java.util.Map)8 HttpStatus (org.springframework.http.HttpStatus)8 RestClientException (org.springframework.web.client.RestClientException)8 ResourceAccessException (org.springframework.web.client.ResourceAccessException)7 UriComponentsBuilder (org.springframework.web.util.UriComponentsBuilder)6 Date (java.util.Date)5 List (java.util.List)5 OpenAppNamespaceDTO (com.ctrip.framework.apollo.openapi.dto.OpenAppNamespaceDTO)4 ArrayList (java.util.ArrayList)4