{"id":127815,"date":"2024-10-28T17:22:00","date_gmt":"2024-10-28T15:22:00","guid":{"rendered":"https:\/\/www.javacodegeeks.com\/?p=127815"},"modified":"2024-10-24T09:35:26","modified_gmt":"2024-10-24T06:35:26","slug":"spring-reactive-switchifempty-example","status":"publish","type":"post","link":"https:\/\/www.javacodegeeks.com\/spring-reactive-switchifempty-example.html","title":{"rendered":"Spring Reactive switchIfEmpty() Example"},"content":{"rendered":"<p>The <code>switchIfEmpty<\/code> operator in Spring Reactive allows developers to provide an alternative publisher that is subscribed to when the source publisher completes without emitting any items. Let us delve into understanding how the spring reactive <code>switchIfEmpty()<\/code> operator works, exploring its behavior and impact on reactive streams.<\/p>\n<h2><a name=\"section-1\"><\/a>1. Introduction<\/h2>\n<p>Spring Reactive is part of the Spring Framework, designed to facilitate the development of asynchronous, non-blocking applications using the reactive programming paradigm. This approach allows developers to create systems that are highly responsive, resilient, and scalable. The core building blocks of Spring Reactive include <code>Flux<\/code> and <code>Mono<\/code>, which represent sequences of 0 to N and 0 to 1 items, respectively. For more information on Spring Reactive, you can visit the official documentation: <a href=\"https:\/\/docs.spring.io\/spring-framework\/docs\/current\/reference\/html\/web-reactive.html\" target=\"_blank\" rel=\"noopener\">Spring WebFlux Documentation<\/a>.<\/p>\n<h3>1.1 Pros<\/h3>\n<p>The advantages of using Spring Reactive include:<\/p>\n<ul>\n<li>Asynchronous Processing: It enables handling requests without blocking threads, improving resource utilization.<\/li>\n<li>Backpressure Support: It provides built-in mechanisms to handle varying rates of data production and consumption.<\/li>\n<li>Improved Scalability: Applications can scale more efficiently, especially under heavy load, due to non-blocking I\/O operations.<\/li>\n<li>Integration with Reactive Streams: Spring Reactive integrates seamlessly with the Reactive Streams API, allowing for a standardized way to handle asynchronous data flows.<\/li>\n<li>Simplified Error Handling: It offers powerful error-handling capabilities within reactive streams.<\/li>\n<\/ul>\n<h2>2. Use of switchIfEmpty() and Defer()<\/h2>\n<p>The <code>switchIfEmpty()<\/code> operator is a powerful tool for handling situations where a reactive stream emits no items. This operator allows developers to specify an alternative publisher that should be subscribed to if the source publisher completes without emitting any items.<\/p>\n<p>The <code>defer()<\/code> operator can be used to create a new publisher each time it is subscribed to, ensuring that the fallback logic is executed only when necessary.<\/p>\n<h3>2.1 Code Example<\/h3>\n<p>Below is a complete Spring Boot application demonstrating the use of <code>switchIfEmpty()<\/code> in conjunction with <code>defer()<\/code>:<\/p>\n<pre class=\"brush:java; wrap-lines:false;\">\nimport org.springframework.boot.SpringApplication;\nimport org.springframework.boot.autoconfigure.SpringBootApplication;\nimport org.springframework.stereotype.Service;\nimport org.springframework.web.bind.annotation.GetMapping;\nimport org.springframework.web.bind.annotation.RestController;\nimport reactor.core.publisher.Flux;\n\n@SpringBootApplication\npublic class SpringReactiveDemoApplication {\n  public static void main(String[] args) {\n    SpringApplication.run(SpringReactiveDemoApplication.class, args);\n  }\n}\n\n@Service\nclass ItemService {\n  public Flux&lt;String&gt; getItems() {\n    return Flux\n        .empty() \/\/ Simulating an empty response\n        .switchIfEmpty(Flux.defer(() -&gt; Flux.just(\"Default Item\")));\n  }\n}\n\n@RestController\nclass ItemController {\n  private final ItemService itemService;\n\n  public ItemController(ItemService itemService) {\n    this.itemService = itemService;\n  }\n\n  @GetMapping(\"\/items\")\n  public Flux&lt;String&gt; fetchItems() {\n    return itemService.getItems();\n  }\n}\n<\/pre>\n<p>The code begins by importing the necessary Spring Boot and Reactor libraries. The <code>@SpringBootApplication<\/code> annotation is used to denote the main class of the Spring application, <code>SpringReactiveDemoApplication<\/code>. This annotation combines three functionalities: <code>@Configuration<\/code>, <code>@EnableAutoConfiguration<\/code>, and <code>@ComponentScan<\/code>. It indicates that this class is the primary configuration class for the Spring application.<div style=\"display:inline-block; margin: 15px 0;\"> <div id=\"adngin-JavaCodeGeeks_incontent_video-0\" style=\"display:inline-block;\"><\/div> <\/div><\/p>\n<p>Inside the <code>SpringReactiveDemoApplication<\/code> class, the <code>main<\/code> method serves as the entry point of the application. It calls <code>SpringApplication.run()<\/code>, which bootstraps the Spring application, starting the embedded web server and initializing the application context.<\/p>\n<p>The code also defines an <code>ItemService<\/code> class, annotated with <code>@Service<\/code>, indicating that it is a service component in the Spring context. This class contains a method named <code>getItems<\/code> that returns a <code>Flux<\/code> of strings. The method simulates fetching items from a data source by returning an empty <code>Flux<\/code>. If no items are found, the <code>switchIfEmpty()<\/code> operator is used to provide a fallback value, returning a <code>Flux<\/code> that emits a default item, &#8220;Default Item&#8221;.<\/p>\n<p>Next, the <code>ItemController<\/code> class is defined, which is annotated with <code>@RestController<\/code>. This annotation indicates that the class handles HTTP requests and returns data directly to the client. The <code>ItemController<\/code> has a dependency on <code>ItemService<\/code>, which is injected via its constructor. The <code>@GetMapping(\"\/items\")<\/code> annotation maps HTTP GET requests to the <code>fetchItems<\/code> method, allowing clients to retrieve items from the service. This method calls <code>getItems<\/code> from <code>ItemService<\/code> and returns the resulting <code>Flux<\/code> of strings.<\/p>\n<p>Overall, this code demonstrates a simple Spring Reactive application that utilizes the reactive programming model to handle empty streams and provide default responses through the use of the <code>switchIfEmpty()<\/code> operator.<\/p>\n<h3>2.2 Testing<\/h3>\n<p>Testing the functionality of <code>switchIfEmpty()<\/code> can be accomplished using <code>JUnit<\/code> and <code>Reactor Test<\/code>. Here\u2019s an example test case for the <code>ItemService<\/code> class:<\/p>\n<pre class=\"brush:java; wrap-lines:false;\">\nimport org.junit.jupiter.api.Test;\nimport org.springframework.beans.factory.annotation.Autowired;\nimport org.springframework.boot.test.context.SpringBootTest;\nimport reactor.test.StepVerifier;\n\n@SpringBootTest\npublic class ItemServiceTest {\n  @Autowired private ItemService itemService;\n\n  @Test\n  public void testGetItems() {\n    StepVerifier.create(itemService.getItems())\n        .expectNext(\"Default Item\")\n        .verifyComplete();\n  }\n}\n<\/pre>\n<h3>2.3 Use Cases of switchIfEmpty()<\/h3>\n<h4>2.3.1 Use Cases Without defer()<\/h4>\n<ul>\n<li>Empty Source: When querying a database that may return no results, <code>switchIfEmpty()<\/code> can provide a default message or object, ensuring that the consumer always receives a value.\n<pre class=\"brush:java; wrap-lines:false;\">\nimport reactor.core.publisher.Flux;\n\npublic class SwitchIfEmptyExample {\n    public static void main(String[] args) {\n        \/\/ Empty Source\n        Flux&lt;String&gt; emptySource = Flux.empty();\n\n        \/\/ Using switchIfEmpty\n        emptySource.switchIfEmpty(Flux.just(\"Default Item\"))\n                   .subscribe(System.out::println); \/\/ Outputs: Default Item\n    }\n}\n<\/pre>\n<\/li>\n<li>Non-Empty Source: If a source emits data, <code>switchIfEmpty()<\/code> allows the application to continue processing the emitted items without invoking the fallback logic, maintaining performance.\n<pre class=\"brush:java; wrap-lines:false;\">\nimport reactor.core.publisher.Flux;\n\npublic class SwitchIfEmptyNonEmptyExample {\n    public static void main(String[] args) {\n        \/\/ Non-Empty Source\n        Flux&lt;String&gt; nonEmptySource = Flux.just(\"Item 1\", \"Item 2\");\n\n        \/\/ Using switchIfEmpty\n        nonEmptySource.switchIfEmpty(Flux.just(\"Default Item\"))\n                      .subscribe(System.out::println); \/\/ Outputs: Item 1, Item 2\n    }\n}\n<\/pre>\n<\/li>\n<\/ul>\n<h4>2.3.2 Use Cases With defer()<\/h4>\n<ul>\n<li>Empty Source: Using <code>defer()<\/code> allows for the creation of a new fallback publisher each time the source is subscribed to, ensuring that the fallback logic is evaluated only when needed.\n<pre class=\"brush:java; wrap-lines:false;\">\nimport reactor.core.publisher.Flux;\n\npublic class SwitchIfEmptyDeferExample {\n    public static void main(String[] args) {\n        \/\/ Empty Source\n        Flux&lt;String&gt; emptySource = Flux.empty();\n\n        \/\/ Using switchIfEmpty with defer\n        emptySource.switchIfEmpty(Flux.defer(() -&gt; Flux.just(\"Default Item\")))\n                   .subscribe(System.out::println); \/\/ Outputs: Default Item\n    }\n}\n<\/pre>\n<\/li>\n<li>Non-Empty Source: Even when using <code>defer()<\/code> if the source emits data, the fallback publisher is ignored, allowing the system to avoid unnecessary resource allocation for the fallback logic.\n<pre class=\"brush:java; wrap-lines:false;\">\nimport reactor.core.publisher.Flux;\n\npublic class SwitchIfEmptyDeferNonEmptyExample {\n    public static void main(String[] args) {\n        \/\/ Non-Empty Source\n        Flux&lt;String&gt; nonEmptySource = Flux.just(\"Item 1\", \"Item 2\");\n\n        \/\/ Using switchIfEmpty with defer\n        nonEmptySource.switchIfEmpty(Flux.defer(() -&gt; Flux.just(\"Default Item\")))\n                      .subscribe(System.out::println); \/\/ Outputs: Item 1, Item 2\n    }\n}\n<\/pre>\n<\/li>\n<\/ul>\n<h4>2.3.3 Combined Use Cases<\/h4>\n<ul>\n<li>Dynamic Fallback Logic: Using <code>defer()<\/code> in conjunction with <code>switchIfEmpty()<\/code> can allow for dynamic generation of fallback values based on runtime conditions, making applications more flexible.\n<pre class=\"brush:java; wrap-lines:false;\">\nimport reactor.core.publisher.Flux;\n\npublic class SwitchIfEmptyDynamicExample {\n    public static void main(String[] args) {\n        \/\/ Dynamic Source\n        Flux&lt;String&gt; dynamicSource = Flux.empty();\n\n        \/\/ Using switchIfEmpty with defer to generate a dynamic fallback\n        dynamicSource.switchIfEmpty(Flux.defer(() -&gt; Flux.just(\"Dynamic Item: \" + System.currentTimeMillis())))\n                     .subscribe(System.out::println); \/\/ Outputs: Dynamic Item: [current timestamp]\n    }\n}\n<\/pre>\n<\/li>\n<li>Improved Resource Management: With <code>defer()<\/code>, applications can efficiently manage resources by deferring the creation of fallback publishers until necessary, particularly in high-load scenarios.\n<pre class=\"brush:java; wrap-lines:false;\">\nimport reactor.core.publisher.Flux;\n\npublic class SwitchIfEmptyResourceManagementExample {\n    public static void main(String[] args) {\n        \/\/ Resource Management Source\n        Flux&lt;String&gt; resourceManagementSource = Flux.empty();\n\n        \/\/ Using switchIfEmpty with defer for resource management\n        resourceManagementSource.switchIfEmpty(Flux.defer(() -&gt; Flux.just(\"Managed Resource\")))\n                                .subscribe(System.out::println); \/\/ Outputs: Managed Resource\n    }\n}\n<\/pre>\n<\/li>\n<\/ul>\n<h2><a name=\"section-3\"><\/a>3. Conclusion<\/h2>\n<p>The <code>switchIfEmpty()<\/code> operator in Spring Reactive provides a robust mechanism for handling empty streams, allowing developers to define fallback logic seamlessly. By combining it with <code>defer()<\/code>, you can ensure that alternative publishers are created on demand, improving resource management and overall application performance. This functionality is invaluable in reactive applications where data availability can be unpredictable.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The switchIfEmpty operator in Spring Reactive allows developers to provide an alternative publisher that is subscribed to when the source publisher completes without emitting any items. Let us delve into understanding how the spring reactive switchIfEmpty() operator works, exploring its behavior and impact on reactive streams. 1. Introduction Spring Reactive is part of the Spring &hellip;<\/p>\n","protected":false},"author":26931,"featured_media":112,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8],"tags":[1715],"class_list":["post-127815","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-enterprise-java","tag-spring-webflux"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.5 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Spring Reactive switchIfEmpty() Example - Java Code Geeks<\/title>\n<meta name=\"description\" content=\"Spring reactive switchifempty: Learn how Spring Reactive&#039;s switchIfEmpty handles empty streams with fallback publishers.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.javacodegeeks.com\/spring-reactive-switchifempty-example.html\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Spring Reactive switchIfEmpty() Example - Java Code Geeks\" \/>\n<meta property=\"og:description\" content=\"Spring reactive switchifempty: Learn how Spring Reactive&#039;s switchIfEmpty handles empty streams with fallback publishers.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.javacodegeeks.com\/spring-reactive-switchifempty-example.html\" \/>\n<meta property=\"og:site_name\" content=\"Java Code Geeks\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/javacodegeeks\" \/>\n<meta property=\"article:published_time\" content=\"2024-10-28T15:22:00+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/enterprise-java-logo.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"150\" \/>\n\t<meta property=\"og:image:height\" content=\"150\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Yatin Batra\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@javacodegeeks\" \/>\n<meta name=\"twitter:site\" content=\"@javacodegeeks\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Yatin Batra\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/spring-reactive-switchifempty-example.html#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/spring-reactive-switchifempty-example.html\"},\"author\":{\"name\":\"Yatin Batra\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#\\\/schema\\\/person\\\/cda31a4c1965373fed40c8907dc09b8d\"},\"headline\":\"Spring Reactive switchIfEmpty() Example\",\"datePublished\":\"2024-10-28T15:22:00+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/spring-reactive-switchifempty-example.html\"},\"wordCount\":763,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#organization\"},\"image\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/spring-reactive-switchifempty-example.html#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2012\\\/10\\\/enterprise-java-logo.jpg\",\"keywords\":[\"Spring WebFlux\"],\"articleSection\":[\"Enterprise Java\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.javacodegeeks.com\\\/spring-reactive-switchifempty-example.html#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/spring-reactive-switchifempty-example.html\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/spring-reactive-switchifempty-example.html\",\"name\":\"Spring Reactive switchIfEmpty() Example - Java Code Geeks\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/spring-reactive-switchifempty-example.html#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/spring-reactive-switchifempty-example.html#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2012\\\/10\\\/enterprise-java-logo.jpg\",\"datePublished\":\"2024-10-28T15:22:00+00:00\",\"description\":\"Spring reactive switchifempty: Learn how Spring Reactive's switchIfEmpty handles empty streams with fallback publishers.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/spring-reactive-switchifempty-example.html#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.javacodegeeks.com\\\/spring-reactive-switchifempty-example.html\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/spring-reactive-switchifempty-example.html#primaryimage\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2012\\\/10\\\/enterprise-java-logo.jpg\",\"contentUrl\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2012\\\/10\\\/enterprise-java-logo.jpg\",\"width\":150,\"height\":150,\"caption\":\"java-interview-questions-answers\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/spring-reactive-switchifempty-example.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.javacodegeeks.com\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Java\",\"item\":\"https:\\\/\\\/www.javacodegeeks.com\\\/category\\\/java\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Enterprise Java\",\"item\":\"https:\\\/\\\/www.javacodegeeks.com\\\/category\\\/java\\\/enterprise-java\"},{\"@type\":\"ListItem\",\"position\":4,\"name\":\"Spring Reactive switchIfEmpty() Example\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#website\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/\",\"name\":\"Java Code Geeks\",\"description\":\"Java Developers Resource Center\",\"publisher\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#organization\"},\"alternateName\":\"JCG\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.javacodegeeks.com\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#organization\",\"name\":\"Exelixis Media P.C.\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2022\\\/06\\\/exelixis-logo.png\",\"contentUrl\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2022\\\/06\\\/exelixis-logo.png\",\"width\":864,\"height\":246,\"caption\":\"Exelixis Media P.C.\"},\"image\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#\\\/schema\\\/logo\\\/image\\\/\"},\"sameAs\":[\"https:\\\/\\\/www.facebook.com\\\/javacodegeeks\",\"https:\\\/\\\/x.com\\\/javacodegeeks\"]},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#\\\/schema\\\/person\\\/cda31a4c1965373fed40c8907dc09b8d\",\"name\":\"Yatin Batra\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2022\\\/12\\\/Yatin.batra_.jpg\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2022\\\/12\\\/Yatin.batra_.jpg\",\"contentUrl\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2022\\\/12\\\/Yatin.batra_.jpg\",\"caption\":\"Yatin Batra\"},\"description\":\"An experience full-stack engineer well versed with Core Java, Spring\\\/Springboot, MVC, Security, AOP, Frontend (Angular &amp; React), and cloud technologies (such as AWS, GCP, Jenkins, Docker, K8).\",\"sameAs\":[\"https:\\\/\\\/www.javacodegeeks.com\"],\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/author\\\/yatin-batra\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Spring Reactive switchIfEmpty() Example - Java Code Geeks","description":"Spring reactive switchifempty: Learn how Spring Reactive's switchIfEmpty handles empty streams with fallback publishers.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.javacodegeeks.com\/spring-reactive-switchifempty-example.html","og_locale":"en_US","og_type":"article","og_title":"Spring Reactive switchIfEmpty() Example - Java Code Geeks","og_description":"Spring reactive switchifempty: Learn how Spring Reactive's switchIfEmpty handles empty streams with fallback publishers.","og_url":"https:\/\/www.javacodegeeks.com\/spring-reactive-switchifempty-example.html","og_site_name":"Java Code Geeks","article_publisher":"https:\/\/www.facebook.com\/javacodegeeks","article_published_time":"2024-10-28T15:22:00+00:00","og_image":[{"width":150,"height":150,"url":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/enterprise-java-logo.jpg","type":"image\/jpeg"}],"author":"Yatin Batra","twitter_card":"summary_large_image","twitter_creator":"@javacodegeeks","twitter_site":"@javacodegeeks","twitter_misc":{"Written by":"Yatin Batra","Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.javacodegeeks.com\/spring-reactive-switchifempty-example.html#article","isPartOf":{"@id":"https:\/\/www.javacodegeeks.com\/spring-reactive-switchifempty-example.html"},"author":{"name":"Yatin Batra","@id":"https:\/\/www.javacodegeeks.com\/#\/schema\/person\/cda31a4c1965373fed40c8907dc09b8d"},"headline":"Spring Reactive switchIfEmpty() Example","datePublished":"2024-10-28T15:22:00+00:00","mainEntityOfPage":{"@id":"https:\/\/www.javacodegeeks.com\/spring-reactive-switchifempty-example.html"},"wordCount":763,"commentCount":0,"publisher":{"@id":"https:\/\/www.javacodegeeks.com\/#organization"},"image":{"@id":"https:\/\/www.javacodegeeks.com\/spring-reactive-switchifempty-example.html#primaryimage"},"thumbnailUrl":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/enterprise-java-logo.jpg","keywords":["Spring WebFlux"],"articleSection":["Enterprise Java"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.javacodegeeks.com\/spring-reactive-switchifempty-example.html#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.javacodegeeks.com\/spring-reactive-switchifempty-example.html","url":"https:\/\/www.javacodegeeks.com\/spring-reactive-switchifempty-example.html","name":"Spring Reactive switchIfEmpty() Example - Java Code Geeks","isPartOf":{"@id":"https:\/\/www.javacodegeeks.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.javacodegeeks.com\/spring-reactive-switchifempty-example.html#primaryimage"},"image":{"@id":"https:\/\/www.javacodegeeks.com\/spring-reactive-switchifempty-example.html#primaryimage"},"thumbnailUrl":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/enterprise-java-logo.jpg","datePublished":"2024-10-28T15:22:00+00:00","description":"Spring reactive switchifempty: Learn how Spring Reactive's switchIfEmpty handles empty streams with fallback publishers.","breadcrumb":{"@id":"https:\/\/www.javacodegeeks.com\/spring-reactive-switchifempty-example.html#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.javacodegeeks.com\/spring-reactive-switchifempty-example.html"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.javacodegeeks.com\/spring-reactive-switchifempty-example.html#primaryimage","url":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/enterprise-java-logo.jpg","contentUrl":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/enterprise-java-logo.jpg","width":150,"height":150,"caption":"java-interview-questions-answers"},{"@type":"BreadcrumbList","@id":"https:\/\/www.javacodegeeks.com\/spring-reactive-switchifempty-example.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.javacodegeeks.com\/"},{"@type":"ListItem","position":2,"name":"Java","item":"https:\/\/www.javacodegeeks.com\/category\/java"},{"@type":"ListItem","position":3,"name":"Enterprise Java","item":"https:\/\/www.javacodegeeks.com\/category\/java\/enterprise-java"},{"@type":"ListItem","position":4,"name":"Spring Reactive switchIfEmpty() Example"}]},{"@type":"WebSite","@id":"https:\/\/www.javacodegeeks.com\/#website","url":"https:\/\/www.javacodegeeks.com\/","name":"Java Code Geeks","description":"Java Developers Resource Center","publisher":{"@id":"https:\/\/www.javacodegeeks.com\/#organization"},"alternateName":"JCG","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.javacodegeeks.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.javacodegeeks.com\/#organization","name":"Exelixis Media P.C.","url":"https:\/\/www.javacodegeeks.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.javacodegeeks.com\/#\/schema\/logo\/image\/","url":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png","contentUrl":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png","width":864,"height":246,"caption":"Exelixis Media P.C."},"image":{"@id":"https:\/\/www.javacodegeeks.com\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/javacodegeeks","https:\/\/x.com\/javacodegeeks"]},{"@type":"Person","@id":"https:\/\/www.javacodegeeks.com\/#\/schema\/person\/cda31a4c1965373fed40c8907dc09b8d","name":"Yatin Batra","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2022\/12\/Yatin.batra_.jpg","url":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2022\/12\/Yatin.batra_.jpg","contentUrl":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2022\/12\/Yatin.batra_.jpg","caption":"Yatin Batra"},"description":"An experience full-stack engineer well versed with Core Java, Spring\/Springboot, MVC, Security, AOP, Frontend (Angular &amp; React), and cloud technologies (such as AWS, GCP, Jenkins, Docker, K8).","sameAs":["https:\/\/www.javacodegeeks.com"],"url":"https:\/\/www.javacodegeeks.com\/author\/yatin-batra"}]}},"_links":{"self":[{"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/posts\/127815","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/users\/26931"}],"replies":[{"embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/comments?post=127815"}],"version-history":[{"count":0,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/posts\/127815\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/media\/112"}],"wp:attachment":[{"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/media?parent=127815"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/categories?post=127815"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/tags?post=127815"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}