Skip to content

Commit 0a47594

Browse files
committed
Document RestTemplateBuilder and @restclienttest
See spring-projectsgh-6030 See spring-projectsgh-5507
1 parent 2eafb3d commit 0a47594

File tree

1 file changed

+73
-2
lines changed

1 file changed

+73
-2
lines changed

spring-boot-docs/src/main/asciidoc/spring-boot-features.adoc

Lines changed: 73 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4227,6 +4227,42 @@ reached.
42274227

42284228

42294229

4230+
[[boot-features-restclient]]
4231+
== Calling REST services
4232+
If you need to call remote REST services from your application, you can use Spring
4233+
Framework's `RestTemplate` class. Since `RestTemplate` instances often needs to be
4234+
customized before being used, Spring Boot does not provide any single auto-configured
4235+
`RestTemplate` bean. It does, however, auto-configure a `RestTemplateBuilder` which can be
4236+
used to create `RestTemplate` instances when needed. The auto-configured
4237+
`RestTemplateBuilder` will ensure that sensible `HttpMessageConverters` are applied
4238+
to `RestTemplate` instances.
4239+
4240+
Here's a typical example:
4241+
4242+
[source,java,indent=0]
4243+
----
4244+
@Service
4245+
public class MyBean {
4246+
4247+
private final RestTemplate restTemplate;
4248+
4249+
public MyBean(RestTemplateBuilder restTemplateBuilder) {
4250+
this.restTemplate = restTemplateBuilder.build();
4251+
}
4252+
4253+
public String someRestCall(String name) {
4254+
return this.restTemplate.getForObject("/{name}/details", Details.class, name);
4255+
}
4256+
4257+
}
4258+
----
4259+
4260+
TIP: `RestTemplateBuilder` includes a number of useful methods that can be used to quickly
4261+
configure a `RestTemplate`. For example, to add BASIC auth support you can use
4262+
`build.basicAuthorization("user', "password").build()`.
4263+
4264+
4265+
42304266
[[boot-features-email]]
42314267
== Sending email
42324268
The Spring Framework provides an easy abstraction for sending email using the
@@ -4893,7 +4929,7 @@ and/or a `WebDriver` bean. Here is an example that uses HtmlUnit:
48934929

48944930
[[boot-features-testing-spring-boot-applications-testing-autoconfigured-jpa-test]]
48954931
==== Auto-configured Data JPA tests
4896-
The `@DataJpaTest` can be used if you want to test JPA applications. By default it will
4932+
`@DataJpaTest` can be used if you want to test JPA applications. By default it will
48974933
configure an in-memory embedded database, scan for `@Entity` classes and configure Spring
48984934
Data JPA repositories. Regular `@Component` beans will not be loaded into the
48994935
`ApplicationContext`.
@@ -4972,9 +5008,44 @@ database you can use the `@AutoConfigureTestDatabase` annotation:
49725008

49735009

49745010

5011+
5012+
[[boot-features-testing-spring-boot-applications-testing-autoconfigured-rest-client]]
5013+
==== Auto-configured REST clients
5014+
Use `@RestClientTest` annotation can be used if you want to test REST clients. By default
5015+
it will auto configure Jackson and GSON support, configure a `RestTemplateBuilder` and
5016+
add support for `MockRestServiceServer`. The specific beans that you want to test should
5017+
be specified using `value` or `components` attribute of `@RestClientTest`:
5018+
5019+
5020+
[source,java,indent=0]
5021+
----
5022+
@RunWith(SpringRunner.class)
5023+
@RestClientTest(RemoteVehicleDetailsService.class)
5024+
public class ExampleRestClientTest {
5025+
5026+
@Autowired
5027+
private MyService service;
5028+
5029+
@Autowired
5030+
private MockRestServiceServer server;
5031+
5032+
@Test
5033+
public void getVehicleDetailsWhenResultIsSuccessShouldReturnDetails()
5034+
throws Exception {
5035+
this.server.expect(requestTo("/greet/details"))
5036+
.andRespond(withSuccess("hello", MediaType.TEXT_PLAIN));
5037+
String greeting = this.service.callRestService();
5038+
assertThat(greeting).isEqualTo("hello");
5039+
}
5040+
5041+
}
5042+
----
5043+
5044+
5045+
49755046
[[boot-features-testing-spring-boot-applications-testing-autoconfigured-rest-docs]]
49765047
==== Auto-configured Spring REST Docs tests
4977-
`@AutoConfigureRestDocs` annotation can be used if you want to use Spring REST Docs
5048+
The `@AutoConfigureRestDocs` annotation can be used if you want to use Spring REST Docs
49785049
in your tests. It will automatically configure `MockMvc` to use Spring REST Docs and
49795050
remove the need for Spring REST Docs' JUnit rule.
49805051

0 commit comments

Comments
 (0)