jooby-project / jooby

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

Request Scoped #602

Closed GramDev1 closed 7 years ago

GramDev1 commented 7 years ago

Hi, If I make a class request scoped, set it via request#set(Class,Object) will I be able to get it via request#require? Currently that does not work for me, or has this feature been implemented?

jknack commented 7 years ago

It does work, here is a simple request counter:

public class App extends Jooby {

  @RequestScoped
  public static class RScope {

    private static AtomicInteger inc = new AtomicInteger(0);
    private Integer id;

    public RScope() {
      this.id = inc.incrementAndGet();
    }

    @Override
    public String toString() {
      return id.toString();
    }
  }

  {
    use("*", "*", (req, rsp) -> {
      req.set(RScope.class, new RScope());
    });

    get("/", req -> req.require(RScope.class));
  }

  public static void main(final String[] args) {
    run(App::new, args);
  }

}
GramDev1 commented 7 years ago

Okay. In my case I have a route filter that sets some information, and then I get it in the next processor. With request attributes it works, but not with request scoped.

jknack commented 7 years ago

did you get an error? Post an example to reproduce the problem.

Thanks

GramDev1 commented 7 years ago

Alright! I managed to fix it by unbinding the class. However, the java docs are a bit confusing on this matter, specifically here: http://jooby.org/apidocs/org/jooby/scope/RequestScoped.html . May want to clear that up. Great framework btw!

jknack commented 7 years ago

Glad you figure it out. Going to clear/update the javadoc.

Thank you and if you like it please don't forget to star/follow the project.