jooby-project / jooby

The modular web framework for Java and Kotlin
https://jooby.io
Apache License 2.0
1.7k stars 199 forks source link

mvc: Add `BindParam` annotation for custom parsing/mapping #3472

Closed jknack closed 2 months ago

jknack commented 2 months ago

In addition to exising *Param annotation, Jooby will support a new annotation BindParam that allow you to convert current HTTP context to a desire type.

Case 1: instance method on controller:

@GET("/new-bind")
public Response doSomething(@BindParam MyBean q) {
   return ...;
}

MyBean bind(Context ctx) {
   // build MyBean from ctx as you want
}

Case 2: static method on Mapping class

@GET("/new-bind")
public Response doSomething(@BindParam(MyConverter.class) MyBean q) {
   return ...;
}
class MyConverter {
   public static MyBean convert(Context ctx) {
    // build MyBean from ctx as you want
   }
}

In both cases mapping is function must take io.jooby.Context as argument and returns the required type. Function/method name is optional.