You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This guide walks you through the process of creating a "Hello, Spring!" RESTful web service with Spring WebFlux (new as of version 5) and then consumes that service with a WebClient (also new as of version 5).
You’ll build a RESTful web service with Spring Webflux and a WebClient consumer of that service. You’ll be able to see output in both System.out and at:
Like most Spring Getting Started guides, you can start from scratch and complete each step, or you can bypass basic setup steps that are already familiar to you. Either way, you end up with working code.
Download and unzip the source repository for this guide, or clone it using Git: git clone https://github.com/spring-guides/gs-reactive-rest-service.git
It collects all the jars on the classpath and builds a single, runnable "über-jar", which makes it more convenient to execute and transport your service.
It searches for the public static void main() method to flag as a runnable class.
It provides a built-in dependency resolver that sets the version number to match Spring Boot dependencies. You can override any version you wish, but it will default to Boot’s chosen set of versions.
Build with Maven
First you set up a basic build script. You can use any build system you like when building apps with Spring, but the code you need to work with Maven is included here. If you’re not familiar with Maven, refer to Building Java Projects with Maven.
Create the directory structure
In a project directory of your choosing, create the following subdirectory structure; for example, with mkdir -p src/main/java/hello on *nix systems:
It collects all the jars on the classpath and builds a single, runnable "über-jar", which makes it more convenient to execute and transport your service.
It searches for the public static void main() method to flag as a runnable class.
It provides a built-in dependency resolver that sets the version number to match Spring Boot dependencies. You can override any version you wish, but it will default to Boot’s chosen set of versions.
This simple reactive class always returns "Hello, Spring!" It could return many other things, including a stream of items from a database, a stream of items that were generated by calculations, and so on. Note the reactive code: a Mono object that holds a ServerResponse body.
Create a Router
In this application, we use a router to handle the only route we expose ("/hello"), as shown in the following example:
The router listens for traffic on the /hello path and returns the value provided by our reactive handler class.
Create a WebClient
The Spring MVC RestTemplate class is, by nature, blocking. Consequently, we don’t want to use it in a reactive application. For reactive applications, Spring offers the WebClient class, which is non-blocking. We’ll use a WebClient implementation to consume our RESTful service:
The WebClient uses reactive features, in the form of a Mono to hold the content of the URI we specify and a function (in the getResult method) to turn that content into a string. If we had different requirements, we might turn it into something other than a string. Since we’re going to put the result into System.out, a string will do here.
WebClient can be used to communicate with non-reactive, blocking services, too.
Make the application executable
Although it is possible to package this service as a traditional WAR file for deployment to an external application server, the simpler approach demonstrated below creates a standalone application. You package everything in a single, executable JAR file, driven by a Java main() method. Also, you use Spring’s support for embedding the Tomcat servlet container as the HTTP runtime, instead of deploying to an external instance.
@SpringBootApplication is a convenience annotation that adds all of the following:
@Configuration tags the class as a source of bean definitions for the application context.
@EnableAutoConfiguration tells Spring Boot to start adding beans based on classpath settings, other beans, and various property settings.
Normally you would add @EnableWebMvc for a Spring MVC app, but Spring Boot adds it automatically when it sees spring-webmvc on the classpath. This flags the application as a web application and activates key behaviors such as setting up a DispatcherServlet.
@ComponentScan tells Spring to look for other components, configurations, and services in the hello package, allowing it to find the controllers.
The main() method uses Spring Boot’s SpringApplication.run() method to launch an application. Did you notice that there wasn’t a single line of XML? No web.xml file either. This web application is 100% pure Java and you didn’t have to deal with configuring any plumbing or infrastructure.
Build an executable JAR
You can run the application from the command line with Gradle or Maven. Or you can build a single executable JAR file that contains all the necessary dependencies, classes, and resources, and run that. This makes it easy to ship, version, and deploy the service as an application throughout the development lifecycle, across different environments, and so forth.
If you are using Gradle, you can run the application using ./gradlew bootRun. Or you can build the JAR file using ./gradlew build. Then you can run the JAR file:
If you are using Maven, you can run the application using ./mvnw spring-boot:run. Or you can build the JAR file with ./mvnw clean package. Then you can run the JAR file:
The procedure above will create a runnable JAR. You can also opt to build a classic WAR file instead.
Logging output is displayed. The service should be up and running within a few seconds.
Once the service has started, you’ll see a line that reads:
>> result = Hello, Spring!
That line comes from the reactive content being consumed by the WebClient. Naturally, you can find something more interesting to do with your output than put it in System.out.
Test the Application
Now that the application is running, you can test it. To start with, you can open a browser and go to http://localhost:8080/hello and see, "Hello, Spring!" For this guide, we also created a test class to get you started on testing with the WebTestClient class.
src/test/java/hello/GreetingRouterTest.java
packagehello;
importorg.springframework.beans.factory.annotation.Autowired;
importorg.springframework.boot.test.context.SpringBootTest;
importorg.springframework.http.MediaType;
importorg.springframework.test.context.junit4.SpringRunner;
importorg.springframework.test.web.reactive.server.WebTestClient;
importorg.junit.Test;
importorg.junit.runner.RunWith;
@RunWith(SpringRunner.class)
// We create a `@SpringBootTest`, starting an actual server on a `RANDOM_PORT`@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
publicclassGreetingRouterTest {
// Spring Boot will create a `WebTestClient` for you,// already configure and ready to issue requests against "localhost:RANDOM_PORT"@AutowiredprivateWebTestClientwebTestClient;
@TestpublicvoidtestHello() {
webTestClient// Create a GET request to test an endpoint
.get().uri("/hello")
.accept(MediaType.TEXT_PLAIN)
.exchange()
// and use the dedicated DSL to test assertions against the response
.expectStatus().isOk()
.expectBody(String.class).isEqualTo("Hello, Spring!");
}
}
Summary
Congratulations! You have developed a Reactive Spring application that includes a WebClient to consume a RESTful service!
The text was updated successfully, but these errors were encountered:
Building a Reactive RESTful Web Service
This guide walks you through the process of creating a "Hello, Spring!" RESTful web service with Spring WebFlux (new as of version 5) and then consumes that service with a WebClient (also new as of version 5).
What You’ll Build
You’ll build a RESTful web service with Spring Webflux and a WebClient consumer of that service. You’ll be able to see output in both System.out and at:
What you’ll need
How to complete this guide
Like most Spring Getting Started guides, you can start from scratch and complete each step, or you can bypass basic setup steps that are already familiar to you. Either way, you end up with working code.
To start from scratch, move on to Build with Gradle.
To skip the basics, do the following:
git clone https://github.com/spring-guides/gs-reactive-rest-service.git
gs-reactive-rest-service/initial
When you’re finished, you can check your results against the code in
gs-reactive-rest-service/complete
.Build with Gradle
First you set up a basic build script. You can use any build system you like when building apps with Spring, but the code you need to work with Gradle and Maven is included here. If you’re not familiar with either, refer to Building Java Projects with Gradle or Building Java Projects with Maven.
Create the directory structure
In a project directory of your choosing, create the following subdirectory structure; for example, with
mkdir -p src/main/java/hello
on *nix systems:Create a Gradle build file
Below is the initial Gradle build file.
build.gradle
The Spring Boot gradle plugin provides many convenient features:
public static void main()
method to flag as a runnable class.Build with Maven
First you set up a basic build script. You can use any build system you like when building apps with Spring, but the code you need to work with Maven is included here. If you’re not familiar with Maven, refer to Building Java Projects with Maven.
Create the directory structure
In a project directory of your choosing, create the following subdirectory structure; for example, with
mkdir -p src/main/java/hello
on *nix systems:pom.xml
The Spring Boot Maven plugin provides many convenient features:
public static void main()
method to flag as a runnable class.Build with your IDE
Create a WebFlux Handler
In the Spring Reactive approach, we use a handler to handle the request and create a response, as shown in the following example:
src/main/java/hello/GreetingHandler.java
This simple reactive class always returns "Hello, Spring!" It could return many other things, including a stream of items from a database, a stream of items that were generated by calculations, and so on. Note the reactive code: a
Mono
object that holds aServerResponse
body.Create a Router
In this application, we use a router to handle the only route we expose ("/hello"), as shown in the following example:
src/main/java/hello/GreetingRouter.java
The router listens for traffic on the
/hello
path and returns the value provided by our reactive handler class.Create a WebClient
The Spring MVC RestTemplate class is, by nature, blocking. Consequently, we don’t want to use it in a reactive application. For reactive applications, Spring offers the WebClient class, which is non-blocking. We’ll use a WebClient implementation to consume our RESTful service:
src/main/java/hello/GreetingWebClient.java
The WebClient uses reactive features, in the form of a Mono to hold the content of the URI we specify and a function (in the
getResult
method) to turn that content into a string. If we had different requirements, we might turn it into something other than a string. Since we’re going to put the result into System.out, a string will do here.Make the application executable
Although it is possible to package this service as a traditional WAR file for deployment to an external application server, the simpler approach demonstrated below creates a standalone application. You package everything in a single, executable JAR file, driven by a Java
main()
method. Also, you use Spring’s support for embedding the Tomcat servlet container as the HTTP runtime, instead of deploying to an external instance.src/main/java/hello/Application.java
@SpringBootApplication
is a convenience annotation that adds all of the following:@Configuration
tags the class as a source of bean definitions for the application context.@EnableAutoConfiguration
tells Spring Boot to start adding beans based on classpath settings, other beans, and various property settings.@EnableWebMvc
for a Spring MVC app, but Spring Boot adds it automatically when it sees spring-webmvc on the classpath. This flags the application as a web application and activates key behaviors such as setting up aDispatcherServlet
.@ComponentScan
tells Spring to look for other components, configurations, and services in thehello
package, allowing it to find the controllers.The
main()
method uses Spring Boot’sSpringApplication.run()
method to launch an application. Did you notice that there wasn’t a single line of XML? No web.xml file either. This web application is 100% pure Java and you didn’t have to deal with configuring any plumbing or infrastructure.Build an executable JAR
You can run the application from the command line with Gradle or Maven. Or you can build a single executable JAR file that contains all the necessary dependencies, classes, and resources, and run that. This makes it easy to ship, version, and deploy the service as an application throughout the development lifecycle, across different environments, and so forth.
If you are using Gradle, you can run the application using
./gradlew bootRun
. Or you can build the JAR file using./gradlew build
. Then you can run the JAR file:If you are using Maven, you can run the application using
./mvnw spring-boot:run
. Or you can build the JAR file with./mvnw clean package
. Then you can run the JAR file:Logging output is displayed. The service should be up and running within a few seconds.
Once the service has started, you’ll see a line that reads:
>> result = Hello, Spring!
That line comes from the reactive content being consumed by the WebClient. Naturally, you can find something more interesting to do with your output than put it in System.out.
Test the Application
Now that the application is running, you can test it. To start with, you can open a browser and go to http://localhost:8080/hello and see, "Hello, Spring!" For this guide, we also created a test class to get you started on testing with the WebTestClient class.
src/test/java/hello/GreetingRouterTest.java
Summary
Congratulations! You have developed a Reactive Spring application that includes a WebClient to consume a RESTful service!
The text was updated successfully, but these errors were encountered: