Spring Web

Spring 5.0 introduced compatibility with Reactive Streams, a library interoperability standardization effort co-lead by Lightbend (with Akka Streams) along with Kaazing, Netflix, Pivotal, Red Hat, Twitter and many others.

Thanks to adopting Reactive Streams, multiple libraries can now inter-op since the same interfaces are implemented by all these libraries. Akka Streams by-design, hides the raw reactive-streams types from end-users, since it allows for detaching these types from RS and allows for a painless migration to java.util.concurrent.Flow which was introduced in Java 9.

This Alpakka module makes it possible to directly return a Source in your Spring Web endpoints.

Project Info: Alpakka Spring Web
Artifact
com.lightbend.akka
akka-stream-alpakka-spring-web
1.0-M2
JDK versions
OpenJDK 8
Scala versions2.12.7, 2.11.12
JPMS module nameakka.stream.alpakka.spring.web
License
Readiness level
Community-driven
Since 0.14, 2017-10-14
Home pagehttps://doc.akka.io/docs/alpakka/current/
API documentation
Forums
Release notesIn the documentation
IssuesGithub issues
Sourceshttps://github.com/akka/alpakka

Artifacts

sbt
libraryDependencies += "com.lightbend.akka" %% "akka-stream-alpakka-spring-web" % "1.0-M2"
Maven
<dependency>
  <groupId>com.lightbend.akka</groupId>
  <artifactId>akka-stream-alpakka-spring-web_2.12</artifactId>
  <version>1.0-M2</version>
</dependency>
Gradle
dependencies {
  compile group: 'com.lightbend.akka', name: 'akka-stream-alpakka-spring-web_2.12', version: '1.0-M2'
}

The table below shows direct dependencies of this module and the second tab shows all libraries it depends on transitively.

Direct dependencies
OrganizationArtifactVersionLicense
com.typesafe.akkaakka-stream_2.122.5.19Apache License, Version 2.0
org.scala-langscala-library2.12.7BSD 3-Clause
org.springframework.bootspring-boot-autoconfigure1.5.14.RELEASEApache License, Version 2.0
org.springframeworkspring-context5.0.7.RELEASEApache License, Version 2.0
org.springframeworkspring-core5.0.7.RELEASEApache License, Version 2.0
org.springframeworkspring-webflux5.0.7.RELEASEApache License, Version 2.0
org.springframeworkspring-webmvc5.0.7.RELEASEApache License, Version 2.0
Dependency tree
com.typesafe.akka    akka-stream_2.12    2.5.19    Apache License, Version 2.0
    com.typesafe.akka    akka-actor_2.12    2.5.19    Apache License, Version 2.0
        com.typesafe    config    1.3.3    Apache License, Version 2.0
        org.scala-lang.modules    scala-java8-compat_2.12    0.8.0    BSD 3-clause
            org.scala-lang    scala-library    2.12.7    BSD 3-Clause
        org.scala-lang    scala-library    2.12.7    BSD 3-Clause
    com.typesafe.akka    akka-protobuf_2.12    2.5.19    Apache License, Version 2.0
        org.scala-lang    scala-library    2.12.7    BSD 3-Clause
    com.typesafe    ssl-config-core_2.12    0.3.6    Apache-2.0
        com.typesafe    config    1.3.3    Apache License, Version 2.0
        org.scala-lang.modules    scala-parser-combinators_2.12    1.1.1    BSD 3-clause
            org.scala-lang    scala-library    2.12.7    BSD 3-Clause
        org.scala-lang    scala-library    2.12.7    BSD 3-Clause
    org.reactivestreams    reactive-streams    1.0.2    CC0
    org.scala-lang    scala-library    2.12.7    BSD 3-Clause
org.scala-lang    scala-library    2.12.7    BSD 3-Clause
org.springframework.boot    spring-boot-autoconfigure    1.5.14.RELEASE    Apache License, Version 2.0
    org.springframework.boot    spring-boot    1.5.14.RELEASE    Apache License, Version 2.0
        org.springframework    spring-context    5.0.7.RELEASE    Apache License, Version 2.0
            org.springframework    spring-aop    5.0.7.RELEASE    Apache License, Version 2.0
                org.springframework    spring-beans    5.0.7.RELEASE    Apache License, Version 2.0
                    org.springframework    spring-core    5.0.7.RELEASE    Apache License, Version 2.0
                        org.springframework    spring-jcl    5.0.7.RELEASE    Apache License, Version 2.0
                org.springframework    spring-core    5.0.7.RELEASE    Apache License, Version 2.0
                    org.springframework    spring-jcl    5.0.7.RELEASE    Apache License, Version 2.0
            org.springframework    spring-beans    5.0.7.RELEASE    Apache License, Version 2.0
                org.springframework    spring-core    5.0.7.RELEASE    Apache License, Version 2.0
                    org.springframework    spring-jcl    5.0.7.RELEASE    Apache License, Version 2.0
            org.springframework    spring-core    5.0.7.RELEASE    Apache License, Version 2.0
                org.springframework    spring-jcl    5.0.7.RELEASE    Apache License, Version 2.0
            org.springframework    spring-expression    5.0.7.RELEASE    Apache License, Version 2.0
                org.springframework    spring-core    5.0.7.RELEASE    Apache License, Version 2.0
                    org.springframework    spring-jcl    5.0.7.RELEASE    Apache License, Version 2.0
        org.springframework    spring-core    5.0.7.RELEASE    Apache License, Version 2.0
            org.springframework    spring-jcl    5.0.7.RELEASE    Apache License, Version 2.0
org.springframework    spring-context    5.0.7.RELEASE    Apache License, Version 2.0
    org.springframework    spring-aop    5.0.7.RELEASE    Apache License, Version 2.0
        org.springframework    spring-beans    5.0.7.RELEASE    Apache License, Version 2.0
            org.springframework    spring-core    5.0.7.RELEASE    Apache License, Version 2.0
                org.springframework    spring-jcl    5.0.7.RELEASE    Apache License, Version 2.0
        org.springframework    spring-core    5.0.7.RELEASE    Apache License, Version 2.0
            org.springframework    spring-jcl    5.0.7.RELEASE    Apache License, Version 2.0
    org.springframework    spring-beans    5.0.7.RELEASE    Apache License, Version 2.0
        org.springframework    spring-core    5.0.7.RELEASE    Apache License, Version 2.0
            org.springframework    spring-jcl    5.0.7.RELEASE    Apache License, Version 2.0
    org.springframework    spring-core    5.0.7.RELEASE    Apache License, Version 2.0
        org.springframework    spring-jcl    5.0.7.RELEASE    Apache License, Version 2.0
    org.springframework    spring-expression    5.0.7.RELEASE    Apache License, Version 2.0
        org.springframework    spring-core    5.0.7.RELEASE    Apache License, Version 2.0
            org.springframework    spring-jcl    5.0.7.RELEASE    Apache License, Version 2.0
org.springframework    spring-core    5.0.7.RELEASE    Apache License, Version 2.0
    org.springframework    spring-jcl    5.0.7.RELEASE    Apache License, Version 2.0
org.springframework    spring-webflux    5.0.7.RELEASE    Apache License, Version 2.0
    io.projectreactor    reactor-core    3.1.8.RELEASE    The Apache Software License, Version 2.0
        org.reactivestreams    reactive-streams    1.0.2    CC0
    org.springframework    spring-beans    5.0.7.RELEASE    Apache License, Version 2.0
        org.springframework    spring-core    5.0.7.RELEASE    Apache License, Version 2.0
            org.springframework    spring-jcl    5.0.7.RELEASE    Apache License, Version 2.0
    org.springframework    spring-core    5.0.7.RELEASE    Apache License, Version 2.0
        org.springframework    spring-jcl    5.0.7.RELEASE    Apache License, Version 2.0
    org.springframework    spring-web    5.0.7.RELEASE    Apache License, Version 2.0
        org.springframework    spring-beans    5.0.7.RELEASE    Apache License, Version 2.0
            org.springframework    spring-core    5.0.7.RELEASE    Apache License, Version 2.0
                org.springframework    spring-jcl    5.0.7.RELEASE    Apache License, Version 2.0
        org.springframework    spring-core    5.0.7.RELEASE    Apache License, Version 2.0
            org.springframework    spring-jcl    5.0.7.RELEASE    Apache License, Version 2.0
org.springframework    spring-webmvc    5.0.7.RELEASE    Apache License, Version 2.0
    org.springframework    spring-aop    5.0.7.RELEASE    Apache License, Version 2.0
        org.springframework    spring-beans    5.0.7.RELEASE    Apache License, Version 2.0
            org.springframework    spring-core    5.0.7.RELEASE    Apache License, Version 2.0
                org.springframework    spring-jcl    5.0.7.RELEASE    Apache License, Version 2.0
        org.springframework    spring-core    5.0.7.RELEASE    Apache License, Version 2.0
            org.springframework    spring-jcl    5.0.7.RELEASE    Apache License, Version 2.0
    org.springframework    spring-beans    5.0.7.RELEASE    Apache License, Version 2.0
        org.springframework    spring-core    5.0.7.RELEASE    Apache License, Version 2.0
            org.springframework    spring-jcl    5.0.7.RELEASE    Apache License, Version 2.0
    org.springframework    spring-context    5.0.7.RELEASE    Apache License, Version 2.0
        org.springframework    spring-aop    5.0.7.RELEASE    Apache License, Version 2.0
            org.springframework    spring-beans    5.0.7.RELEASE    Apache License, Version 2.0
                org.springframework    spring-core    5.0.7.RELEASE    Apache License, Version 2.0
                    org.springframework    spring-jcl    5.0.7.RELEASE    Apache License, Version 2.0
            org.springframework    spring-core    5.0.7.RELEASE    Apache License, Version 2.0
                org.springframework    spring-jcl    5.0.7.RELEASE    Apache License, Version 2.0
        org.springframework    spring-beans    5.0.7.RELEASE    Apache License, Version 2.0
            org.springframework    spring-core    5.0.7.RELEASE    Apache License, Version 2.0
                org.springframework    spring-jcl    5.0.7.RELEASE    Apache License, Version 2.0
        org.springframework    spring-core    5.0.7.RELEASE    Apache License, Version 2.0
            org.springframework    spring-jcl    5.0.7.RELEASE    Apache License, Version 2.0
        org.springframework    spring-expression    5.0.7.RELEASE    Apache License, Version 2.0
            org.springframework    spring-core    5.0.7.RELEASE    Apache License, Version 2.0
                org.springframework    spring-jcl    5.0.7.RELEASE    Apache License, Version 2.0
    org.springframework    spring-core    5.0.7.RELEASE    Apache License, Version 2.0
        org.springframework    spring-jcl    5.0.7.RELEASE    Apache License, Version 2.0
    org.springframework    spring-expression    5.0.7.RELEASE    Apache License, Version 2.0
        org.springframework    spring-core    5.0.7.RELEASE    Apache License, Version 2.0
            org.springframework    spring-jcl    5.0.7.RELEASE    Apache License, Version 2.0
    org.springframework    spring-web    5.0.7.RELEASE    Apache License, Version 2.0
        org.springframework    spring-beans    5.0.7.RELEASE    Apache License, Version 2.0
            org.springframework    spring-core    5.0.7.RELEASE    Apache License, Version 2.0
                org.springframework    spring-jcl    5.0.7.RELEASE    Apache License, Version 2.0
        org.springframework    spring-core    5.0.7.RELEASE    Apache License, Version 2.0
            org.springframework    spring-jcl    5.0.7.RELEASE    Apache License, Version 2.0

Usage

Using Akka Streams in Spring Web (or Boot for that matter) is very simple, as Alpakka provides autoconfiguration to the framework, which means that Spring is made aware of Sources and Sinks etc.

All you need to do is include the above dependency (akka-stream-alpakka-spring-web), start your app as usual:

Java
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class DemoApplication {

  public static void main(String[] args) {
    SpringApplication.run(DemoApplication.class, args);
  }
}

And you’ll be able to return Akka Streams in HTTP endpoints directly:

Java
import akka.NotUsed;
import akka.actor.ActorSystem;
import akka.stream.Materializer;
import akka.stream.javadsl.Source;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class SampleController {

  @RequestMapping("/")
  public Source<String, NotUsed> index() {
    return Source.repeat("Hello world!").intersperse("\n").take(10);
  }
}

Both javadsl and scaladsl Akka Stream types are supported.

In fact, since Akka supports Java 9 and the java.util.concurrent.Flow.* types already, before Spring, you could use it to adapt those types in your applications as well.

The provided configuration

The automatically enabled configuration is as follows:

Java

import akka.actor.ActorSystem; import akka.stream.ActorMaterializer; import akka.stream.Materializer; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.core.ReactiveAdapterRegistry; import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter; @Configuration @ConditionalOnClass(akka.stream.javadsl.Source.class) public class SpringWebAkkaStreamsConfiguration { private final ActorSystem system; private final ActorMaterializer mat; @Autowired public SpringWebAkkaStreamsConfiguration( RequestMappingHandlerAdapter requestMappingHandlerAdapter) { final ReactiveAdapterRegistry registry = requestMappingHandlerAdapter.getReactiveAdapterRegistry(); system = ActorSystem.create("SpringWebAkkaStreamsSystem"); mat = ActorMaterializer.create(system); new AkkaStreamsRegistrar(mat).registerAdapters(registry); } @Bean @ConditionalOnMissingBean(ActorSystem.class) public ActorSystem getActorSystem() { return system; } @Bean @ConditionalOnMissingBean(Materializer.class) public ActorMaterializer getMaterializer() { return mat; } }

In case you’d like to manually configure it slightly differently.

Shameless plug: Akka HTTP

While the integration presented here works, it’s not quite the optimal way of using Akka in conjunction with serving HTTP apps. If you’re new to reactive systems and picking technologies, you may want to have a look at Akka HTTP.

If, for some reason, you decided use Spring MVC this integration should help you achieve the basic streaming scenarios though.

Found an error in this documentation? The source code for this page can be found here. Please feel free to edit and contribute a pull request.