commit
7d78af9fab
|
@ -2,12 +2,21 @@ package org.baeldung.web.controller;
|
||||||
|
|
||||||
import static org.apache.commons.lang3.RandomStringUtils.randomAlphabetic;
|
import static org.apache.commons.lang3.RandomStringUtils.randomAlphabetic;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
import org.baeldung.web.dto.Foo;
|
import org.baeldung.web.dto.Foo;
|
||||||
import org.baeldung.web.dto.FooProtos;
|
import org.baeldung.web.dto.FooProtos;
|
||||||
import org.springframework.http.HttpStatus;
|
import org.springframework.http.HttpStatus;
|
||||||
import org.springframework.http.MediaType;
|
|
||||||
import org.springframework.stereotype.Controller;
|
import org.springframework.stereotype.Controller;
|
||||||
import org.springframework.web.bind.annotation.*;
|
import org.springframework.web.bind.annotation.PathVariable;
|
||||||
|
import org.springframework.web.bind.annotation.RequestBody;
|
||||||
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RequestMethod;
|
||||||
|
import org.springframework.web.bind.annotation.RequestParam;
|
||||||
|
import org.springframework.web.bind.annotation.ResponseBody;
|
||||||
|
import org.springframework.web.bind.annotation.ResponseStatus;
|
||||||
|
|
||||||
|
import com.google.common.collect.Lists;
|
||||||
|
|
||||||
@Controller
|
@Controller
|
||||||
public class FooController {
|
public class FooController {
|
||||||
|
@ -16,6 +25,12 @@ public class FooController {
|
||||||
super();
|
super();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@RequestMapping(method = RequestMethod.GET, value = "/foos")
|
||||||
|
@ResponseBody
|
||||||
|
public List<Foo> findListOfFoo() {
|
||||||
|
return Lists.newArrayList(new Foo(1, randomAlphabetic(4)));
|
||||||
|
}
|
||||||
|
|
||||||
// API - read
|
// API - read
|
||||||
|
|
||||||
@RequestMapping(method = RequestMethod.GET, value = "/foos/{id}")
|
@RequestMapping(method = RequestMethod.GET, value = "/foos/{id}")
|
||||||
|
@ -33,6 +48,27 @@ public class FooController {
|
||||||
return foo;
|
return foo;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@RequestMapping(method = RequestMethod.PATCH, value = "/foos/{id}")
|
||||||
|
@ResponseStatus(HttpStatus.OK)
|
||||||
|
@ResponseBody
|
||||||
|
public Foo patchFoo(@PathVariable("id") final String id, @RequestBody final Foo foo) {
|
||||||
|
return foo;
|
||||||
|
}
|
||||||
|
|
||||||
|
@RequestMapping(method = RequestMethod.POST, value = "/foos")
|
||||||
|
@ResponseStatus(HttpStatus.CREATED)
|
||||||
|
@ResponseBody
|
||||||
|
public Foo postFoo(@RequestBody final Foo foo) {
|
||||||
|
return foo;
|
||||||
|
}
|
||||||
|
|
||||||
|
@RequestMapping(method = RequestMethod.HEAD, value = "/foos")
|
||||||
|
@ResponseStatus(HttpStatus.OK)
|
||||||
|
@ResponseBody
|
||||||
|
public Foo headFoo() {
|
||||||
|
return new Foo(1, randomAlphabetic(4));
|
||||||
|
}
|
||||||
|
|
||||||
@RequestMapping(method = RequestMethod.GET, value = "/foos/{id}", produces = { "application/x-protobuf" })
|
@RequestMapping(method = RequestMethod.GET, value = "/foos/{id}", produces = { "application/x-protobuf" })
|
||||||
@ResponseBody
|
@ResponseBody
|
||||||
public FooProtos.Foo findProtoById(@PathVariable final long id) {
|
public FooProtos.Foo findProtoById(@PathVariable final long id) {
|
||||||
|
|
|
@ -0,0 +1,269 @@
|
||||||
|
package org.baeldung.web.test;
|
||||||
|
|
||||||
|
import static org.apache.commons.codec.binary.Base64.encodeBase64;
|
||||||
|
import static org.hamcrest.CoreMatchers.equalTo;
|
||||||
|
import static org.hamcrest.CoreMatchers.is;
|
||||||
|
import static org.hamcrest.CoreMatchers.notNullValue;
|
||||||
|
import static org.hamcrest.MatcherAssert.assertThat;
|
||||||
|
import static org.junit.Assert.assertTrue;
|
||||||
|
import static org.junit.Assert.fail;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.net.URI;
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
|
import org.baeldung.web.dto.Foo;
|
||||||
|
import org.junit.Before;
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.springframework.http.HttpEntity;
|
||||||
|
import org.springframework.http.HttpHeaders;
|
||||||
|
import org.springframework.http.HttpMethod;
|
||||||
|
import org.springframework.http.HttpStatus;
|
||||||
|
import org.springframework.http.MediaType;
|
||||||
|
import org.springframework.http.ResponseEntity;
|
||||||
|
import org.springframework.http.client.ClientHttpRequestFactory;
|
||||||
|
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
|
||||||
|
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
|
||||||
|
import org.springframework.util.LinkedMultiValueMap;
|
||||||
|
import org.springframework.util.MultiValueMap;
|
||||||
|
import org.springframework.web.client.HttpClientErrorException;
|
||||||
|
import org.springframework.web.client.RequestCallback;
|
||||||
|
import org.springframework.web.client.RestTemplate;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.databind.JsonNode;
|
||||||
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
|
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
|
||||||
|
import com.google.common.base.Charsets;
|
||||||
|
|
||||||
|
public class RestTemplateBasicLiveTest {
|
||||||
|
|
||||||
|
private RestTemplate restTemplate;
|
||||||
|
private static final String fooResourceUrl = "http://localhost:8082/spring-rest/foos";
|
||||||
|
|
||||||
|
@Before
|
||||||
|
public void beforeTest() {
|
||||||
|
restTemplate = new RestTemplate();
|
||||||
|
// restTemplate.setMessageConverters(Arrays.asList(new MappingJackson2HttpMessageConverter()));
|
||||||
|
}
|
||||||
|
|
||||||
|
// GET
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenResourceUrl_whenSendGetForRequestEntity_thenStatusOk() throws IOException {
|
||||||
|
final ResponseEntity<Foo> response = restTemplate.getForEntity(fooResourceUrl + "/1", Foo.class);
|
||||||
|
|
||||||
|
assertThat(response.getStatusCode(), equalTo(HttpStatus.OK));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenResourceUrl_whenSendGetForRequestEntity_thenBodyCorrect() throws IOException {
|
||||||
|
final RestTemplate template = new RestTemplate();
|
||||||
|
final ResponseEntity<String> response = template.getForEntity(fooResourceUrl + "/1", String.class);
|
||||||
|
|
||||||
|
final ObjectMapper mapper = new XmlMapper();
|
||||||
|
final JsonNode root = mapper.readTree(response.getBody());
|
||||||
|
final JsonNode name = root.path("name");
|
||||||
|
assertThat(name.asText(), notNullValue());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenResourceUrl_whenRetrievingResource_thenCorrect() throws IOException {
|
||||||
|
final Foo foo = restTemplate.getForObject(fooResourceUrl + "/1", Foo.class);
|
||||||
|
|
||||||
|
assertThat(foo.getName(), notNullValue());
|
||||||
|
assertThat(foo.getId(), is(1L));
|
||||||
|
}
|
||||||
|
|
||||||
|
// HEAD, OPTIONS
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenFooService_whenCallHeadForHeaders_thenReceiveAllHeadersForThatResource() {
|
||||||
|
final HttpHeaders httpHeaders = restTemplate.headForHeaders(fooResourceUrl);
|
||||||
|
assertTrue(httpHeaders.getContentType()
|
||||||
|
.includes(MediaType.APPLICATION_JSON));
|
||||||
|
}
|
||||||
|
|
||||||
|
// POST
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenFooService_whenPostForObject_thenCreatedObjectIsReturned() {
|
||||||
|
final HttpEntity<Foo> request = new HttpEntity<>(new Foo("bar"));
|
||||||
|
final Foo foo = restTemplate.postForObject(fooResourceUrl, request, Foo.class);
|
||||||
|
assertThat(foo, notNullValue());
|
||||||
|
assertThat(foo.getName(), is("bar"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenFooService_whenPostForLocation_thenCreatedLocationIsReturned() {
|
||||||
|
final HttpEntity<Foo> request = new HttpEntity<>(new Foo("bar"));
|
||||||
|
final URI location = restTemplate.postForLocation(fooResourceUrl, request);
|
||||||
|
assertThat(location, notNullValue());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenFooService_whenPostResource_thenResourceIsCreated() {
|
||||||
|
final Foo foo = new Foo("bar");
|
||||||
|
final ResponseEntity<Foo> response = restTemplate.postForEntity(fooResourceUrl, foo, Foo.class);
|
||||||
|
|
||||||
|
assertThat(response.getStatusCode(), is(HttpStatus.CREATED));
|
||||||
|
final Foo fooResponse = response.getBody();
|
||||||
|
assertThat(fooResponse, notNullValue());
|
||||||
|
assertThat(fooResponse.getName(), is("bar"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenFooService_whenCallOptionsForAllow_thenReceiveValueOfAllowHeader() {
|
||||||
|
final Set<HttpMethod> optionsForAllow = restTemplate.optionsForAllow(fooResourceUrl);
|
||||||
|
final HttpMethod[] supportedMethods = { HttpMethod.GET, HttpMethod.POST, HttpMethod.HEAD };
|
||||||
|
|
||||||
|
assertTrue(optionsForAllow.containsAll(Arrays.asList(supportedMethods)));
|
||||||
|
}
|
||||||
|
|
||||||
|
// PUT
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenFooService_whenPutExistingEntity_thenItIsUpdated() {
|
||||||
|
final HttpHeaders headers = prepareBasicAuthHeaders();
|
||||||
|
final HttpEntity<Foo> request = new HttpEntity<>(new Foo("bar"), headers);
|
||||||
|
|
||||||
|
// Create Resource
|
||||||
|
final ResponseEntity<Foo> createResponse = restTemplate.exchange(fooResourceUrl, HttpMethod.POST, request, Foo.class);
|
||||||
|
|
||||||
|
// Update Resource
|
||||||
|
final Foo updatedInstance = new Foo("newName");
|
||||||
|
updatedInstance.setId(createResponse.getBody()
|
||||||
|
.getId());
|
||||||
|
final String resourceUrl = fooResourceUrl + '/' + createResponse.getBody()
|
||||||
|
.getId();
|
||||||
|
final HttpEntity<Foo> requestUpdate = new HttpEntity<>(updatedInstance, headers);
|
||||||
|
restTemplate.exchange(resourceUrl, HttpMethod.PUT, requestUpdate, Void.class);
|
||||||
|
|
||||||
|
// Check that Resource was updated
|
||||||
|
final ResponseEntity<Foo> updateResponse = restTemplate.exchange(resourceUrl, HttpMethod.GET, new HttpEntity<>(headers), Foo.class);
|
||||||
|
final Foo foo = updateResponse.getBody();
|
||||||
|
assertThat(foo.getName(), is(updatedInstance.getName()));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenFooService_whenPutExistingEntityWithCallback_thenItIsUpdated() {
|
||||||
|
final HttpHeaders headers = prepareBasicAuthHeaders();
|
||||||
|
final HttpEntity<Foo> request = new HttpEntity<>(new Foo("bar"), headers);
|
||||||
|
|
||||||
|
// Create entity
|
||||||
|
ResponseEntity<Foo> response = restTemplate.exchange(fooResourceUrl, HttpMethod.POST, request, Foo.class);
|
||||||
|
assertThat(response.getStatusCode(), is(HttpStatus.CREATED));
|
||||||
|
|
||||||
|
// Update entity
|
||||||
|
final Foo updatedInstance = new Foo("newName");
|
||||||
|
updatedInstance.setId(response.getBody()
|
||||||
|
.getId());
|
||||||
|
final String resourceUrl = fooResourceUrl + '/' + response.getBody()
|
||||||
|
.getId();
|
||||||
|
restTemplate.execute(resourceUrl, HttpMethod.PUT, requestCallback(updatedInstance), clientHttpResponse -> null);
|
||||||
|
|
||||||
|
// Check that entity was updated
|
||||||
|
response = restTemplate.exchange(resourceUrl, HttpMethod.GET, new HttpEntity<>(headers), Foo.class);
|
||||||
|
final Foo foo = response.getBody();
|
||||||
|
assertThat(foo.getName(), is(updatedInstance.getName()));
|
||||||
|
}
|
||||||
|
|
||||||
|
// PATCH
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenFooService_whenPatchExistingEntity_thenItIsUpdated() {
|
||||||
|
final HttpHeaders headers = prepareBasicAuthHeaders();
|
||||||
|
final HttpEntity<Foo> request = new HttpEntity<>(new Foo("bar"), headers);
|
||||||
|
|
||||||
|
// Create Resource
|
||||||
|
final ResponseEntity<Foo> createResponse = restTemplate.exchange(fooResourceUrl, HttpMethod.POST, request, Foo.class);
|
||||||
|
|
||||||
|
// Update Resource
|
||||||
|
final Foo updatedResource = new Foo("newName");
|
||||||
|
updatedResource.setId(createResponse.getBody()
|
||||||
|
.getId());
|
||||||
|
final String resourceUrl = fooResourceUrl + '/' + createResponse.getBody()
|
||||||
|
.getId();
|
||||||
|
final HttpEntity<Foo> requestUpdate = new HttpEntity<>(updatedResource, headers);
|
||||||
|
final ClientHttpRequestFactory requestFactory = getSimpleClientHttpRequestFactory();
|
||||||
|
final RestTemplate template = new RestTemplate(requestFactory);
|
||||||
|
template.setMessageConverters(Arrays.asList(new MappingJackson2HttpMessageConverter()));
|
||||||
|
template.patchForObject(resourceUrl, requestUpdate, Void.class);
|
||||||
|
|
||||||
|
// Check that Resource was updated
|
||||||
|
final ResponseEntity<Foo> updateResponse = restTemplate.exchange(resourceUrl, HttpMethod.GET, new HttpEntity<>(headers), Foo.class);
|
||||||
|
final Foo foo = updateResponse.getBody();
|
||||||
|
assertThat(foo.getName(), is(updatedResource.getName()));
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenFooService_whenFormSubmit_thenResourceIsCreated() {
|
||||||
|
HttpHeaders headers = new HttpHeaders();
|
||||||
|
headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
|
||||||
|
|
||||||
|
MultiValueMap<String, String> map= new LinkedMultiValueMap<>();
|
||||||
|
map.add("id", "1");
|
||||||
|
|
||||||
|
HttpEntity<MultiValueMap<String, String>> request = new HttpEntity<>(map, headers);
|
||||||
|
|
||||||
|
ResponseEntity<String> response = restTemplate.postForEntity( fooResourceUrl+"/form", request , String.class);
|
||||||
|
|
||||||
|
assertThat(response.getStatusCode(), is(HttpStatus.CREATED));
|
||||||
|
final String fooResponse = response.getBody();
|
||||||
|
assertThat(fooResponse, notNullValue());
|
||||||
|
assertThat(fooResponse, is("1"));
|
||||||
|
}
|
||||||
|
|
||||||
|
private HttpHeaders prepareBasicAuthHeaders() {
|
||||||
|
final HttpHeaders headers = new HttpHeaders();
|
||||||
|
final String encodedLogPass = getBase64EncodedLogPass();
|
||||||
|
headers.add(HttpHeaders.AUTHORIZATION, "Basic " + encodedLogPass);
|
||||||
|
return headers;
|
||||||
|
}
|
||||||
|
|
||||||
|
private String getBase64EncodedLogPass() {
|
||||||
|
final String logPass = "user1:user1Pass";
|
||||||
|
final byte[] authHeaderBytes = encodeBase64(logPass.getBytes(Charsets.US_ASCII));
|
||||||
|
return new String(authHeaderBytes, Charsets.US_ASCII);
|
||||||
|
}
|
||||||
|
|
||||||
|
private RequestCallback requestCallback(final Foo updatedInstance) {
|
||||||
|
return clientHttpRequest -> {
|
||||||
|
final ObjectMapper mapper = new ObjectMapper();
|
||||||
|
mapper.writeValue(clientHttpRequest.getBody(), updatedInstance);
|
||||||
|
clientHttpRequest.getHeaders()
|
||||||
|
.add(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE);
|
||||||
|
clientHttpRequest.getHeaders()
|
||||||
|
.add(HttpHeaders.AUTHORIZATION, "Basic " + getBase64EncodedLogPass());
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
// Simply setting restTemplate timeout using ClientHttpRequestFactory
|
||||||
|
|
||||||
|
ClientHttpRequestFactory getSimpleClientHttpRequestFactory() {
|
||||||
|
final int timeout = 5;
|
||||||
|
final HttpComponentsClientHttpRequestFactory clientHttpRequestFactory = new HttpComponentsClientHttpRequestFactory();
|
||||||
|
clientHttpRequestFactory.setConnectTimeout(timeout * 1000);
|
||||||
|
return clientHttpRequestFactory;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,122 @@
|
||||||
|
package org.baeldung.web.test;
|
||||||
|
|
||||||
|
import static org.hamcrest.CoreMatchers.equalTo;
|
||||||
|
import static org.hamcrest.MatcherAssert.assertThat;
|
||||||
|
import static org.junit.Assert.assertTrue;
|
||||||
|
|
||||||
|
import org.junit.Before;
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.springframework.boot.test.web.client.TestRestTemplate;
|
||||||
|
import org.springframework.boot.web.client.RestTemplateBuilder;
|
||||||
|
import org.springframework.http.HttpHeaders;
|
||||||
|
import org.springframework.http.HttpStatus;
|
||||||
|
import org.springframework.http.MediaType;
|
||||||
|
import org.springframework.http.ResponseEntity;
|
||||||
|
import org.springframework.web.client.RestTemplate;
|
||||||
|
|
||||||
|
import okhttp3.Request;
|
||||||
|
import okhttp3.RequestBody;
|
||||||
|
|
||||||
|
public class TestRestTemplateBasicLiveTest {
|
||||||
|
|
||||||
|
private RestTemplate restTemplate;
|
||||||
|
|
||||||
|
private static final String FOO_RESOURCE_URL = "http://localhost:" + 8082 + "/spring-rest/foos";
|
||||||
|
private static final String URL_SECURED_BY_AUTHENTICATION = "http://httpbin.org/basic-auth/user/passwd";
|
||||||
|
private static final String BASE_URL = "http://localhost:" + 8082 + "/spring-rest";
|
||||||
|
|
||||||
|
@Before
|
||||||
|
public void beforeTest() {
|
||||||
|
restTemplate = new RestTemplate();
|
||||||
|
}
|
||||||
|
|
||||||
|
// GET
|
||||||
|
@Test
|
||||||
|
public void givenTestRestTemplate_whenSendGetForEntity_thenStatusOk() {
|
||||||
|
TestRestTemplate testRestTemplate = new TestRestTemplate();
|
||||||
|
ResponseEntity<String> response = testRestTemplate.getForEntity(FOO_RESOURCE_URL + "/1", String.class);
|
||||||
|
assertThat(response.getStatusCode(), equalTo(HttpStatus.OK));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenRestTemplateWrapper_whenSendGetForEntity_thenStatusOk() {
|
||||||
|
RestTemplateBuilder restTemplateBuilder = new RestTemplateBuilder();
|
||||||
|
restTemplateBuilder.configure(restTemplate);
|
||||||
|
TestRestTemplate testRestTemplate = new TestRestTemplate(restTemplateBuilder);
|
||||||
|
ResponseEntity<String> response = testRestTemplate.getForEntity(FOO_RESOURCE_URL + "/1", String.class);
|
||||||
|
assertThat(response.getStatusCode(), equalTo(HttpStatus.OK));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenRestTemplateBuilderWrapper_whenSendGetForEntity_thenStatusOk() {
|
||||||
|
RestTemplateBuilder restTemplateBuilder = new RestTemplateBuilder();
|
||||||
|
restTemplateBuilder.build();
|
||||||
|
TestRestTemplate testRestTemplate = new TestRestTemplate(restTemplateBuilder);
|
||||||
|
ResponseEntity<String> response = testRestTemplate.getForEntity(FOO_RESOURCE_URL + "/1", String.class);
|
||||||
|
assertThat(response.getStatusCode(), equalTo(HttpStatus.OK));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenRestTemplateWrapperWithCredentials_whenSendGetForEntity_thenStatusOk() {
|
||||||
|
RestTemplateBuilder restTemplateBuilder = new RestTemplateBuilder();
|
||||||
|
restTemplateBuilder.configure(restTemplate);
|
||||||
|
TestRestTemplate testRestTemplate = new TestRestTemplate(restTemplateBuilder, "user", "passwd");
|
||||||
|
ResponseEntity<String> response = testRestTemplate.getForEntity(URL_SECURED_BY_AUTHENTICATION,
|
||||||
|
String.class);
|
||||||
|
assertThat(response.getStatusCode(), equalTo(HttpStatus.OK));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenTestRestTemplateWithCredentials_whenSendGetForEntity_thenStatusOk() {
|
||||||
|
TestRestTemplate testRestTemplate = new TestRestTemplate("user", "passwd");
|
||||||
|
ResponseEntity<String> response = testRestTemplate.getForEntity(URL_SECURED_BY_AUTHENTICATION,
|
||||||
|
String.class);
|
||||||
|
assertThat(response.getStatusCode(), equalTo(HttpStatus.OK));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenTestRestTemplateWithBasicAuth_whenSendGetForEntity_thenStatusOk() {
|
||||||
|
TestRestTemplate testRestTemplate = new TestRestTemplate();
|
||||||
|
ResponseEntity<String> response = testRestTemplate.withBasicAuth("user", "passwd").
|
||||||
|
getForEntity(URL_SECURED_BY_AUTHENTICATION, String.class);
|
||||||
|
assertThat(response.getStatusCode(), equalTo(HttpStatus.OK));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenTestRestTemplateWithCredentialsAndEnabledCookies_whenSendGetForEntity_thenStatusOk() {
|
||||||
|
TestRestTemplate testRestTemplate = new TestRestTemplate("user", "passwd", TestRestTemplate.
|
||||||
|
HttpClientOption.ENABLE_COOKIES);
|
||||||
|
ResponseEntity<String> response = testRestTemplate.getForEntity(URL_SECURED_BY_AUTHENTICATION,
|
||||||
|
String.class);
|
||||||
|
assertThat(response.getStatusCode(), equalTo(HttpStatus.OK));
|
||||||
|
}
|
||||||
|
|
||||||
|
// HEAD
|
||||||
|
@Test
|
||||||
|
public void givenFooService_whenCallHeadForHeaders_thenReceiveAllHeaders() {
|
||||||
|
TestRestTemplate testRestTemplate = new TestRestTemplate();
|
||||||
|
final HttpHeaders httpHeaders = testRestTemplate.headForHeaders(FOO_RESOURCE_URL);
|
||||||
|
assertTrue(httpHeaders.getContentType().includes(MediaType.APPLICATION_JSON));
|
||||||
|
}
|
||||||
|
|
||||||
|
// POST
|
||||||
|
@Test
|
||||||
|
public void givenService_whenPostForObject_thenCreatedObjectIsReturned() {
|
||||||
|
TestRestTemplate testRestTemplate = new TestRestTemplate("user", "passwd");
|
||||||
|
final RequestBody body = RequestBody.create(okhttp3.MediaType.parse("text/html; charset=utf-8"),
|
||||||
|
"{\"id\":1,\"name\":\"Jim\"}");
|
||||||
|
final Request request = new Request.Builder().url(BASE_URL + "/users/detail").post(body).build();
|
||||||
|
testRestTemplate.postForObject(URL_SECURED_BY_AUTHENTICATION, request, String.class);
|
||||||
|
}
|
||||||
|
|
||||||
|
// PUT
|
||||||
|
@Test
|
||||||
|
public void givenService_whenPutForObject_thenCreatedObjectIsReturned() {
|
||||||
|
TestRestTemplate testRestTemplate = new TestRestTemplate("user", "passwd");
|
||||||
|
final RequestBody body = RequestBody.create(okhttp3.MediaType.parse("text/html; charset=utf-8"),
|
||||||
|
"{\"id\":1,\"name\":\"Jim\"}");
|
||||||
|
final Request request = new Request.Builder().url(BASE_URL + "/users/detail").post(body).build();
|
||||||
|
testRestTemplate.put(URL_SECURED_BY_AUTHENTICATION, request, String.class);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -25,6 +25,10 @@
|
||||||
<groupId>org.springframework.boot</groupId>
|
<groupId>org.springframework.boot</groupId>
|
||||||
<artifactId>spring-boot-devtools</artifactId>
|
<artifactId>spring-boot-devtools</artifactId>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-web</artifactId>
|
||||||
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.springframework.boot</groupId>
|
<groupId>org.springframework.boot</groupId>
|
||||||
<artifactId>spring-boot-starter-test</artifactId>
|
<artifactId>spring-boot-starter-test</artifactId>
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
package org.baeldung.resttemplate.configuration;
|
package org.baeldung.resttemplate;
|
||||||
|
|
||||||
import org.springframework.boot.SpringApplication;
|
import org.springframework.boot.SpringApplication;
|
||||||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
|
@ -0,0 +1,124 @@
|
||||||
|
package org.baeldung.resttemplate.configuration;
|
||||||
|
|
||||||
|
import static org.apache.commons.lang3.RandomStringUtils.randomAlphabetic;
|
||||||
|
|
||||||
|
import java.net.URI;
|
||||||
|
import java.util.Collection;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
import org.baeldung.resttemplate.web.dto.Foo;
|
||||||
|
import org.springframework.http.HttpHeaders;
|
||||||
|
import org.springframework.http.HttpStatus;
|
||||||
|
import org.springframework.http.ResponseEntity;
|
||||||
|
import org.springframework.stereotype.Controller;
|
||||||
|
import org.springframework.web.bind.annotation.PathVariable;
|
||||||
|
import org.springframework.web.bind.annotation.RequestBody;
|
||||||
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RequestMethod;
|
||||||
|
import org.springframework.web.bind.annotation.RequestParam;
|
||||||
|
import org.springframework.web.bind.annotation.ResponseBody;
|
||||||
|
import org.springframework.web.bind.annotation.ResponseStatus;
|
||||||
|
import org.springframework.web.client.HttpClientErrorException;
|
||||||
|
import org.springframework.web.servlet.support.ServletUriComponentsBuilder;
|
||||||
|
|
||||||
|
import com.google.common.collect.ImmutableMap;
|
||||||
|
import com.google.common.collect.Maps;
|
||||||
|
|
||||||
|
@Controller
|
||||||
|
public class FooController {
|
||||||
|
|
||||||
|
private Map<Long, Foo> fooRepository = Maps.newHashMap(ImmutableMap.of(1L, new Foo(1L, randomAlphabetic(4))));
|
||||||
|
|
||||||
|
public FooController() {
|
||||||
|
super();
|
||||||
|
}
|
||||||
|
|
||||||
|
@RequestMapping(method = RequestMethod.GET, value = "/foos")
|
||||||
|
@ResponseBody
|
||||||
|
public Collection<Foo> findListOfFoo() {
|
||||||
|
return fooRepository.values();
|
||||||
|
}
|
||||||
|
|
||||||
|
// API - read
|
||||||
|
|
||||||
|
@RequestMapping(method = RequestMethod.GET, value = "/foos/{id}")
|
||||||
|
@ResponseBody
|
||||||
|
public Foo findById(@PathVariable final long id) throws HttpClientErrorException {
|
||||||
|
Foo foo = fooRepository.get(id);
|
||||||
|
|
||||||
|
if (foo == null) {
|
||||||
|
throw new HttpClientErrorException(HttpStatus.NOT_FOUND);
|
||||||
|
} else {
|
||||||
|
return foo;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// API - write
|
||||||
|
|
||||||
|
@RequestMapping(method = RequestMethod.PUT, value = "/foos/{id}")
|
||||||
|
@ResponseStatus(HttpStatus.OK)
|
||||||
|
@ResponseBody
|
||||||
|
public Foo updateFoo(@PathVariable("id") final long id, @RequestBody final Foo foo) {
|
||||||
|
fooRepository.put(id, foo);
|
||||||
|
return foo;
|
||||||
|
}
|
||||||
|
|
||||||
|
@RequestMapping(method = RequestMethod.PATCH, value = "/foos/{id}")
|
||||||
|
@ResponseStatus(HttpStatus.OK)
|
||||||
|
@ResponseBody
|
||||||
|
public Foo patchFoo(@PathVariable("id") final long id, @RequestBody final Foo foo) {
|
||||||
|
fooRepository.put(id, foo);
|
||||||
|
return foo;
|
||||||
|
}
|
||||||
|
|
||||||
|
@RequestMapping(method = RequestMethod.POST, value = "/foos")
|
||||||
|
@ResponseStatus(HttpStatus.CREATED)
|
||||||
|
@ResponseBody
|
||||||
|
public ResponseEntity<Foo> postFoo(@RequestBody final Foo foo) {
|
||||||
|
|
||||||
|
fooRepository.put(foo.getId(), foo);
|
||||||
|
final URI location = ServletUriComponentsBuilder
|
||||||
|
.fromCurrentServletMapping()
|
||||||
|
.path("/foos/{id}")
|
||||||
|
.build()
|
||||||
|
.expand(foo.getId())
|
||||||
|
.toUri();
|
||||||
|
|
||||||
|
final HttpHeaders headers = new HttpHeaders();
|
||||||
|
headers.setLocation(location);
|
||||||
|
|
||||||
|
final ResponseEntity<Foo> entity = new ResponseEntity<Foo>(foo, headers, HttpStatus.CREATED);
|
||||||
|
return entity;
|
||||||
|
}
|
||||||
|
|
||||||
|
@RequestMapping(method = RequestMethod.HEAD, value = "/foos")
|
||||||
|
@ResponseStatus(HttpStatus.OK)
|
||||||
|
@ResponseBody
|
||||||
|
public Foo headFoo() {
|
||||||
|
return new Foo(1, randomAlphabetic(4));
|
||||||
|
}
|
||||||
|
|
||||||
|
@RequestMapping(method = RequestMethod.POST, value = "/foos/new")
|
||||||
|
@ResponseStatus(HttpStatus.CREATED)
|
||||||
|
@ResponseBody
|
||||||
|
public Foo createFoo(@RequestBody final Foo foo) {
|
||||||
|
fooRepository.put(foo.getId(), foo);
|
||||||
|
return foo;
|
||||||
|
}
|
||||||
|
|
||||||
|
@RequestMapping(method = RequestMethod.DELETE, value = "/foos/{id}")
|
||||||
|
@ResponseStatus(HttpStatus.OK)
|
||||||
|
@ResponseBody
|
||||||
|
public long deleteFoo(@PathVariable final long id) {
|
||||||
|
fooRepository.remove(id);
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
@RequestMapping(method = RequestMethod.POST, value = "/foos/form")
|
||||||
|
@ResponseStatus(HttpStatus.CREATED)
|
||||||
|
@ResponseBody
|
||||||
|
public String submitFoo(@RequestParam("id") String id) {
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -7,6 +7,7 @@ import org.springframework.context.annotation.Bean;
|
||||||
import org.springframework.context.annotation.ComponentScan;
|
import org.springframework.context.annotation.ComponentScan;
|
||||||
import org.springframework.context.annotation.Configuration;
|
import org.springframework.context.annotation.Configuration;
|
||||||
import org.springframework.context.annotation.DependsOn;
|
import org.springframework.context.annotation.DependsOn;
|
||||||
|
import org.springframework.web.client.RestTemplate;
|
||||||
|
|
||||||
@Configuration
|
@Configuration
|
||||||
@EnableAutoConfiguration
|
@EnableAutoConfiguration
|
||||||
|
@ -25,4 +26,8 @@ public class SpringConfig {
|
||||||
return new RestTemplateBuilder(customRestTemplateCustomizer());
|
return new RestTemplateBuilder(customRestTemplateCustomizer());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public RestTemplate restTemplate() {
|
||||||
|
return new RestTemplate();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
package org.baeldung.web.dto;
|
package org.baeldung.resttemplate.web.dto;
|
||||||
|
|
||||||
import com.thoughtworks.xstream.annotations.XStreamAlias;
|
import com.thoughtworks.xstream.annotations.XStreamAlias;
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
package org.baeldung.web.exception;
|
package org.baeldung.resttemplate.web.exception;
|
||||||
|
|
||||||
public class NotFoundException extends RuntimeException {
|
public class NotFoundException extends RuntimeException {
|
||||||
}
|
}
|
|
@ -1,13 +1,14 @@
|
||||||
package org.baeldung.web.handler;
|
package org.baeldung.resttemplate.web.handler;
|
||||||
|
|
||||||
import org.baeldung.web.exception.NotFoundException;
|
import java.io.IOException;
|
||||||
|
|
||||||
|
import org.baeldung.resttemplate.web.exception.NotFoundException;
|
||||||
import org.springframework.http.HttpStatus;
|
import org.springframework.http.HttpStatus;
|
||||||
import org.springframework.http.client.ClientHttpResponse;
|
import org.springframework.http.client.ClientHttpResponse;
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
|
import org.springframework.web.client.HttpClientErrorException;
|
||||||
import org.springframework.web.client.ResponseErrorHandler;
|
import org.springframework.web.client.ResponseErrorHandler;
|
||||||
|
|
||||||
import java.io.IOException;
|
|
||||||
|
|
||||||
@Component
|
@Component
|
||||||
public class RestTemplateResponseErrorHandler
|
public class RestTemplateResponseErrorHandler
|
||||||
implements ResponseErrorHandler {
|
implements ResponseErrorHandler {
|
||||||
|
@ -31,6 +32,7 @@ public class RestTemplateResponseErrorHandler
|
||||||
.getStatusCode()
|
.getStatusCode()
|
||||||
.series() == HttpStatus.Series.SERVER_ERROR) {
|
.series() == HttpStatus.Series.SERVER_ERROR) {
|
||||||
//Handle SERVER_ERROR
|
//Handle SERVER_ERROR
|
||||||
|
throw new HttpClientErrorException(httpResponse.getStatusCode());
|
||||||
} else if (httpResponse
|
} else if (httpResponse
|
||||||
.getStatusCode()
|
.getStatusCode()
|
||||||
.series() == HttpStatus.Series.CLIENT_ERROR) {
|
.series() == HttpStatus.Series.CLIENT_ERROR) {
|
|
@ -1,4 +1,4 @@
|
||||||
package org.baeldung.web.model;
|
package org.baeldung.resttemplate.web.model;
|
||||||
|
|
||||||
public class Bar {
|
public class Bar {
|
||||||
private String id;
|
private String id;
|
|
@ -1,4 +1,4 @@
|
||||||
package org.baeldung.web.model;
|
package org.baeldung.resttemplate.web.model;
|
||||||
|
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
|
@ -1,7 +1,7 @@
|
||||||
package org.baeldung.web.service;
|
package org.baeldung.resttemplate.web.service;
|
||||||
|
|
||||||
import org.baeldung.web.handler.RestTemplateResponseErrorHandler;
|
import org.baeldung.resttemplate.web.handler.RestTemplateResponseErrorHandler;
|
||||||
import org.baeldung.web.model.Bar;
|
import org.baeldung.resttemplate.web.model.Bar;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.boot.web.client.RestTemplateBuilder;
|
import org.springframework.boot.web.client.RestTemplateBuilder;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
|
@ -1,6 +1,6 @@
|
||||||
package org.baeldung.web.service;
|
package org.baeldung.resttemplate.web.service;
|
||||||
|
|
||||||
import org.baeldung.web.model.Employee;
|
import org.baeldung.resttemplate.web.model.Employee;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
@ -1,6 +1,6 @@
|
||||||
package org.baeldung;
|
package org.baeldung;
|
||||||
|
|
||||||
import org.baeldung.resttemplate.configuration.RestTemplateConfigurationApplication;
|
import org.baeldung.resttemplate.RestTemplateConfigurationApplication;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.junit.runner.RunWith;
|
import org.junit.runner.RunWith;
|
||||||
import org.springframework.boot.test.context.SpringBootTest;
|
import org.springframework.boot.test.context.SpringBootTest;
|
||||||
|
|
|
@ -14,7 +14,8 @@ import java.net.URI;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
import org.baeldung.web.dto.Foo;
|
import org.baeldung.resttemplate.web.dto.Foo;
|
||||||
|
import org.baeldung.resttemplate.web.handler.RestTemplateResponseErrorHandler;
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.springframework.http.HttpEntity;
|
import org.springframework.http.HttpEntity;
|
||||||
|
@ -34,6 +35,7 @@ import org.springframework.web.client.RestTemplate;
|
||||||
|
|
||||||
import com.fasterxml.jackson.databind.JsonNode;
|
import com.fasterxml.jackson.databind.JsonNode;
|
||||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
|
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
|
||||||
import com.google.common.base.Charsets;
|
import com.google.common.base.Charsets;
|
||||||
|
|
||||||
public class RestTemplateBasicLiveTest {
|
public class RestTemplateBasicLiveTest {
|
||||||
|
@ -44,6 +46,7 @@ public class RestTemplateBasicLiveTest {
|
||||||
@Before
|
@Before
|
||||||
public void beforeTest() {
|
public void beforeTest() {
|
||||||
restTemplate = new RestTemplate();
|
restTemplate = new RestTemplate();
|
||||||
|
restTemplate.setErrorHandler(new RestTemplateResponseErrorHandler());
|
||||||
// restTemplate.setMessageConverters(Arrays.asList(new MappingJackson2HttpMessageConverter()));
|
// restTemplate.setMessageConverters(Arrays.asList(new MappingJackson2HttpMessageConverter()));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -61,7 +64,7 @@ public class RestTemplateBasicLiveTest {
|
||||||
final RestTemplate template = new RestTemplate();
|
final RestTemplate template = new RestTemplate();
|
||||||
final ResponseEntity<String> response = template.getForEntity(fooResourceUrl + "/1", String.class);
|
final ResponseEntity<String> response = template.getForEntity(fooResourceUrl + "/1", String.class);
|
||||||
|
|
||||||
final ObjectMapper mapper = new ObjectMapper();
|
final ObjectMapper mapper = new XmlMapper();
|
||||||
final JsonNode root = mapper.readTree(response.getBody());
|
final JsonNode root = mapper.readTree(response.getBody());
|
||||||
final JsonNode name = root.path("name");
|
final JsonNode name = root.path("name");
|
||||||
assertThat(name.asText(), notNullValue());
|
assertThat(name.asText(), notNullValue());
|
||||||
|
@ -97,7 +100,7 @@ public class RestTemplateBasicLiveTest {
|
||||||
@Test
|
@Test
|
||||||
public void givenFooService_whenPostForLocation_thenCreatedLocationIsReturned() {
|
public void givenFooService_whenPostForLocation_thenCreatedLocationIsReturned() {
|
||||||
final HttpEntity<Foo> request = new HttpEntity<>(new Foo("bar"));
|
final HttpEntity<Foo> request = new HttpEntity<>(new Foo("bar"));
|
||||||
final URI location = restTemplate.postForLocation(fooResourceUrl, request);
|
final URI location = restTemplate.postForLocation(fooResourceUrl, request, Foo.class);
|
||||||
assertThat(location, notNullValue());
|
assertThat(location, notNullValue());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -211,7 +214,7 @@ public class RestTemplateBasicLiveTest {
|
||||||
restTemplate.getForEntity(entityUrl, Foo.class);
|
restTemplate.getForEntity(entityUrl, Foo.class);
|
||||||
fail();
|
fail();
|
||||||
} catch (final HttpClientErrorException ex) {
|
} catch (final HttpClientErrorException ex) {
|
||||||
assertThat(ex.getStatusCode(), is(HttpStatus.NOT_FOUND));
|
assertThat(ex.getStatusCode(), is(HttpStatus.INTERNAL_SERVER_ERROR));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -221,7 +224,7 @@ public class RestTemplateBasicLiveTest {
|
||||||
headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
|
headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
|
||||||
|
|
||||||
MultiValueMap<String, String> map= new LinkedMultiValueMap<>();
|
MultiValueMap<String, String> map= new LinkedMultiValueMap<>();
|
||||||
map.add("id", "1");
|
map.add("id", "10");
|
||||||
|
|
||||||
HttpEntity<MultiValueMap<String, String>> request = new HttpEntity<>(map, headers);
|
HttpEntity<MultiValueMap<String, String>> request = new HttpEntity<>(map, headers);
|
||||||
|
|
||||||
|
@ -230,7 +233,7 @@ public class RestTemplateBasicLiveTest {
|
||||||
assertThat(response.getStatusCode(), is(HttpStatus.CREATED));
|
assertThat(response.getStatusCode(), is(HttpStatus.CREATED));
|
||||||
final String fooResponse = response.getBody();
|
final String fooResponse = response.getBody();
|
||||||
assertThat(fooResponse, notNullValue());
|
assertThat(fooResponse, notNullValue());
|
||||||
assertThat(fooResponse, is("1"));
|
assertThat(fooResponse, is("10"));
|
||||||
}
|
}
|
||||||
|
|
||||||
private HttpHeaders prepareBasicAuthHeaders() {
|
private HttpHeaders prepareBasicAuthHeaders() {
|
||||||
|
|
|
@ -4,6 +4,7 @@ import static org.hamcrest.CoreMatchers.equalTo;
|
||||||
import static org.hamcrest.MatcherAssert.assertThat;
|
import static org.hamcrest.MatcherAssert.assertThat;
|
||||||
import static org.junit.Assert.assertTrue;
|
import static org.junit.Assert.assertTrue;
|
||||||
|
|
||||||
|
import org.baeldung.resttemplate.web.dto.Foo;
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.springframework.boot.test.web.client.TestRestTemplate;
|
import org.springframework.boot.test.web.client.TestRestTemplate;
|
||||||
|
@ -34,7 +35,7 @@ public class TestRestTemplateBasicLiveTest {
|
||||||
@Test
|
@Test
|
||||||
public void givenTestRestTemplate_whenSendGetForEntity_thenStatusOk() {
|
public void givenTestRestTemplate_whenSendGetForEntity_thenStatusOk() {
|
||||||
TestRestTemplate testRestTemplate = new TestRestTemplate();
|
TestRestTemplate testRestTemplate = new TestRestTemplate();
|
||||||
ResponseEntity<String> response = testRestTemplate.getForEntity(FOO_RESOURCE_URL + "/1", String.class);
|
ResponseEntity<Foo> response = testRestTemplate.getForEntity(FOO_RESOURCE_URL + "/1", Foo.class);
|
||||||
assertThat(response.getStatusCode(), equalTo(HttpStatus.OK));
|
assertThat(response.getStatusCode(), equalTo(HttpStatus.OK));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -43,7 +44,7 @@ public class TestRestTemplateBasicLiveTest {
|
||||||
RestTemplateBuilder restTemplateBuilder = new RestTemplateBuilder();
|
RestTemplateBuilder restTemplateBuilder = new RestTemplateBuilder();
|
||||||
restTemplateBuilder.configure(restTemplate);
|
restTemplateBuilder.configure(restTemplate);
|
||||||
TestRestTemplate testRestTemplate = new TestRestTemplate(restTemplateBuilder);
|
TestRestTemplate testRestTemplate = new TestRestTemplate(restTemplateBuilder);
|
||||||
ResponseEntity<String> response = testRestTemplate.getForEntity(FOO_RESOURCE_URL + "/1", String.class);
|
ResponseEntity<Foo> response = testRestTemplate.getForEntity(FOO_RESOURCE_URL + "/1", Foo.class);
|
||||||
assertThat(response.getStatusCode(), equalTo(HttpStatus.OK));
|
assertThat(response.getStatusCode(), equalTo(HttpStatus.OK));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -52,7 +53,7 @@ public class TestRestTemplateBasicLiveTest {
|
||||||
RestTemplateBuilder restTemplateBuilder = new RestTemplateBuilder();
|
RestTemplateBuilder restTemplateBuilder = new RestTemplateBuilder();
|
||||||
restTemplateBuilder.build();
|
restTemplateBuilder.build();
|
||||||
TestRestTemplate testRestTemplate = new TestRestTemplate(restTemplateBuilder);
|
TestRestTemplate testRestTemplate = new TestRestTemplate(restTemplateBuilder);
|
||||||
ResponseEntity<String> response = testRestTemplate.getForEntity(FOO_RESOURCE_URL + "/1", String.class);
|
ResponseEntity<Foo> response = testRestTemplate.getForEntity(FOO_RESOURCE_URL + "/1", Foo.class);
|
||||||
assertThat(response.getStatusCode(), equalTo(HttpStatus.OK));
|
assertThat(response.getStatusCode(), equalTo(HttpStatus.OK));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,7 +1,8 @@
|
||||||
package org.baeldung.web.handler;
|
package org.baeldung.web.handler;
|
||||||
|
|
||||||
import org.baeldung.web.exception.NotFoundException;
|
import org.baeldung.resttemplate.web.exception.NotFoundException;
|
||||||
import org.baeldung.web.model.Bar;
|
import org.baeldung.resttemplate.web.handler.RestTemplateResponseErrorHandler;
|
||||||
|
import org.baeldung.resttemplate.web.model.Bar;
|
||||||
import org.junit.Assert;
|
import org.junit.Assert;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.junit.runner.RunWith;
|
import org.junit.runner.RunWith;
|
||||||
|
|
|
@ -7,7 +7,8 @@ import static org.springframework.test.web.client.response.MockRestResponseCreat
|
||||||
import java.net.URI;
|
import java.net.URI;
|
||||||
|
|
||||||
import org.baeldung.SpringTestConfig;
|
import org.baeldung.SpringTestConfig;
|
||||||
import org.baeldung.web.model.Employee;
|
import org.baeldung.resttemplate.web.model.Employee;
|
||||||
|
import org.baeldung.resttemplate.web.service.EmployeeService;
|
||||||
import org.junit.Assert;
|
import org.junit.Assert;
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
package org.baeldung.web.service;
|
package org.baeldung.web.service;
|
||||||
|
|
||||||
import org.baeldung.web.model.Employee;
|
import org.baeldung.resttemplate.web.model.Employee;
|
||||||
|
import org.baeldung.resttemplate.web.service.EmployeeService;
|
||||||
import org.junit.Assert;
|
import org.junit.Assert;
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
Loading…
Reference in New Issue