Code Monkey home page Code Monkey logo

getting-started-guides's Introduction

tags projects

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).

Note
This guide shows the functional way of using Spring WebFlux. You can also use annotations with WebFlux.

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:

http://localhost:8080/greeting

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

link:complete/src/main/java/hello/GreetingHandler.java[role=include]

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:

src/main/java/hello/GreetingRouter.java

link:complete/src/main/java/hello/GreetingRouter.java[role=include]

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

link:complete/src/main/java/hello/GreetingWebClient.java[role=include]

The WebClient uses reactive features, in the form 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.

Tip
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 good old Java main() method. Along the way, you use Reactive Spring’s support for embedding the Netty server as the HTTP runtime, instead of deploying to an external instance.

src/main/java/hello/Application.java

link:complete/src/main/java/hello/Application.java[role=include]

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

link:complete/src/test/java/hello/GreetingRouterTest.java[role=include]

Summary

Congratulations! You have developed a Reactive Spring application that includes a WebClient to consume a RESTful service!

getting-started-guides's People

Contributors

buzzardo avatar gregturn avatar mminella avatar royclarkson avatar

Watchers

 avatar  avatar

Forkers

mminella

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.