Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New controller for Spring MVC using Lambda [SPR-12954] #17546

Closed
spring-projects-issues opened this issue Apr 25, 2015 · 1 comment
Closed

New controller for Spring MVC using Lambda [SPR-12954] #17546

spring-projects-issues opened this issue Apr 25, 2015 · 1 comment
Assignees
Labels
in: web Issues in web modules (web, webmvc, webflux, websocket) type: enhancement A general enhancement
Milestone

Comments

@spring-projects-issues
Copy link
Collaborator

spring-projects-issues commented Apr 25, 2015

Toshiaki Maki opened SPR-12954 and commented

After Java SE 8 has been released, some frameworks using lambda appear.
For example Spark, Siden.

These frameworks are inspired by Sinatra and will resolve "Annotation Hell" which revealed after "XML Hell" had been cleared.

Spring MVC has a good mechanism to abstract controller layer (HandlerMapping, HandlerAdopter).
To implement these, Spring MVC can have a power to make use of lambda.

I've created a prototype for Spring MVC to provide a router like Sinatra.
https://github.com/making/new-controller

RouterHandlerMapping is a key component.

So far, this prototype does not use the url matching algorithm which @MVC uses. But you can see how this controller could be used.

Sample code is following:

@SpringBootApplication
public class App {
    public static void main(String[] args) {
        SpringApplication.run(App.class, args);
    }
    // omitted

    // Define request mapping using lambda
    @Bean
    RouterDefinition<Handler> routerDef() {
        return router -> router
                /* curl localhost:8080 ==> Sample*/
                .get("/", (req, res) -> res.body("Sample"))
                /* curl localhost:8080/hello ==> Hello World! */
                .get("/hello", (req, res) -> res.body("Hello World!"))
                /* curl localhost:8080/json ==> {"name":"John","age":30} */
                .get("/json", (req, res) -> res
                        .contentType(MediaType.APPLICATION_JSON)
                        .body(new Person("John", 30)))
                /* curl localhost:8080/xml ==> <?xml version="1.0" encoding="UTF-8" standalone="yes"?><person><age>30</age><name>John</name></person> */
                .get("/xml", (req, res) -> res
                        .contentType(MediaType.APPLICATION_XML)
                        .body(new Person("John", 30)))
                /* curl localhost:8080/template ==> [templates/hello.html will be rendered via Thymeleaf] */
                .get("/template", (req, res) -> {
                    req.put("message", "Hello World!");
                    return res.view("hello");
                })
                /* curl localhost:8080/bar/aaa ==> foo = aaa */
                .get("/bar/{foo}", (req, res) ->
                        res.body("foo = " + req.param("foo")
                                .orElse("??")))
                /* curl localhost:8080/param -d name=John ==> Hi John */
                .post("/echo", (req, res) ->
                        res.body(req.param("name")
                                .map(name -> "Hi " + name)
                                .orElse("Please input name!")))
                /* curl localhost:8080/param -d name=John -d age=30 ==> {"name":"John","age":30} */
                .post("/param", (req, res) -> {
                    Person person = req.params(Person.class);
                    return res.body(person);
                })
                /* curl localhost:8080/body -H 'Content-Type: application/json' -d '{"name":"John","age":30}' ==> {"name":"John","age":30} */
                .post("/body", (req, res) -> {
                    Person person = req.body(Person.class);
                    return res.body(person);
                });
    }
}

How do you think about this idea?


Issue Links:

2 votes, 9 watchers

@spring-projects-issues
Copy link
Collaborator Author

Arjen Poutsma commented

Done. See f1319f5 and the forthcoming blog post on this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: web Issues in web modules (web, webmvc, webflux, websocket) type: enhancement A general enhancement
Projects
None yet
Development

No branches or pull requests

2 participants