Codementor Events

Functional programming in Spring MVC

Published Apr 13, 2019Last updated Oct 09, 2019

Spring 5.0 embraced ReactiveStreams specification, and introduced a new Reactive Stack as an alternative of the traditional Servlet stack. And it also brought a new functional programming model to developers, but it is only supported in the Reactive stack.

The good news is that in the incoming 5.2 the functional like APIs are being ported back to the Servlet stack. For those developers who are stick on Servlet stack and want to experience the new programming model, it is absolutely a startling news.

In this post, let's take a glance at the new functional feature in Spring MVC.

Create a Spring Boot project using Spring initializr(http://start.spring.io), add Web, JPA, Lombok, and H2 starters as dependencies.

NOTE: Please select the new Spring Boot version 2.2.0.BUILD-SNAPSHOT to get the new Spring 5.2.M1 in its dependencies.

Create a simple JPA Entity Post.

@Data
@ToString
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Entity
class Post {

    @Id
    @GeneratedValue(strategy = AUTO)
    private Long id;
    private String title;
    private String content;

}

And create a Repository for the Post Entity.

interface PostRepository extends JpaRepository<Post, Long> {}

Define a RouterFuncation bean to handle the routing rules.

@Bean
public RouterFunction<ServerResponse> routes(PostHandler postController) {
    return route(GET("/posts"), postController::all)
        .andRoute(POST("/posts"), postController::create)
        .andRoute(GET("/posts/{id}"), postController::get)
        .andRoute(PUT("/posts/{id}"), postController::update)
        .andRoute(DELETE("/posts/{id}"), postController::delete);
}

The codes are almost same as the ones we have used in Reactive stack, but note here the ServerRequest, ServerResponse and RouterFunction are imported from the new package:org.springframework.web.servlet.function.

Let's have a look at the details of PostHandler.

@Component
class PostHandler {

    private final PostRepository posts;

    public PostHandler(PostRepository posts) {
        this.posts = posts;
    }

    public ServerResponse all(ServerRequest req) {
        return ServerResponse.ok().body(this.posts.findAll());
    }

    public ServerResponse create(ServerRequest req) throws ServletException, IOException {

        var saved = this.posts.save(req.body(Post.class));
        return ServerResponse.created(URI.create("/posts/" + saved.getId())).build();
    }

    public ServerResponse get(ServerRequest req) {
        return this.posts.findById(Long.valueOf(req.pathVariable("id")))
            .map(post -> ServerResponse.ok().body(post))
            .orElse(ServerResponse.notFound().build());
    }

    public ServerResponse update(ServerRequest req) throws ServletException, IOException {
        var data = req.body(Post.class);

        return this.posts.findById(Long.valueOf(req.pathVariable("id")))
            .map(
                post -> {
                    post.setTitle(data.getTitle());
                    post.setContent(data.getContent());
                    return post;
                }
            )
            .map(post -> this.posts.save(post))
            .map(post -> ServerResponse.noContent().build())
            .orElse(ServerResponse.notFound().build());

    }

    public ServerResponse delete(ServerRequest req) {
        return this.posts.findById(Long.valueOf(req.pathVariable("id")))
            .map(
                post -> {
                    this.posts.delete(post);
                    return ServerResponse.noContent().build();
                }
            )
            .orElse(ServerResponse.notFound().build());
    }

}

It is very similar to the codes of Reactive stack, and but the methods return a ServerResponse instead of Mono<ServerResponse>.

Like the RouterFunctionDSL feature provided in Reactive stack, the routing rules also can be written in Kotlin DSL.

 router {
        "/posts".nest {
            GET("", postHandler::all)
            GET("{id}", postHandler::get)
            POST("", postHandler::create)
            PUT("{id}", postHandler::update)
            DELETE("{id}", postHandler::delete)
        }

    }

Besides these, MockMvc also gets support of Kotlin DSL, you can write your tests in a fluent style like the following.

  @Test
  fun `Get all posts should ok`() {
        mockMvc
                .get("/posts") {
                    accept = APPLICATION_JSON
                    headers {
                        contentLanguage = Locale.ENGLISH
                    }
                }
                .andExpect {
                    status { isOk }
                    content { contentType(APPLICATION_JSON_UTF8) }
                    jsonPath("$[0].title") { value(containsString("post")) } 
                }
                .andDo {
                    print()
                }
    }

Check out the source codes from my Github , and compare it with the codes that I had written to demonstrate Reactive stack .

Discover and read more posts from Hantsy Bai
get started
post commentsBe the first to share your opinion
Hantsy Bai
5 years ago

Hi Ray, I just updated to Spring Boot 2.0.0.M2 and cleaned the POM file, hope it is helpful for you to resolve the dependencies. Please check the source codes on Github to try it yourself.

Ray Norman
5 years ago

Used the exact same pom. Tried downloading the demo project as well, from Spring starter. Still not able to access org.springframework.web.servlet.function

Show more replies