Using org.jooby.Response directly inside a MVC route

319 views
Skip to first unread message

elect...@gmail.com

unread,
Dec 26, 2015, 6:34:14 PM12/26/15
to jooby-project
Hi,

I'm currently evaluating Jooby and I like it so far, good work!! I may have some more questions/suggestions if I decide to use it for a real project and if you don't mind... But here's my first one! :

I would use MVC routes, mainly because of the Swagger support for them. But I also like to have full control over what I send as a response, when this is needed. So I was happy to see that Jooby automatically injects Request and Response on a route endpoint!

For example :

@GET
@Path("/test")
public void test(Request request, Response response) throws Exception {
    response
.status(418);
    response
.send("test!");
    response
.end();
}

But when I do that, I get an error because, after the method returns, Jooby tries to write another status code by itself, even if the headers are already sent :

[2015-12-26 15:46:14,810]-[utow task-2] ERROR org.jooby.spi.HttpHandler - execution of err handler resulted in exception
java
.lang.IllegalStateException: UT000002: The response has already been started
    at io
.undertow.server.HttpServerExchange.setStatusCode(HttpServerExchange.java:1353) ~[undertow-core-1.3.9.Final.jar:1.3.9.Final]
    at io
.undertow.server.HttpServerExchange.setResponseCode(HttpServerExchange.java:1328) ~[undertow-core-1.3.9.Final.jar:1.3.9.Final]
    at org
.jooby.internal.undertow.UndertowResponse.statusCode(UndertowResponse.java:108) ~[jooby-undertow-0.11.2.jar:na]
    at org
.jooby.internal.ResponseImpl.status(ResponseImpl.java:219) ~[jooby-0.11.2.jar:na]
    at org
.jooby.internal.HttpHandlerImpl.handleErr(HttpHandlerImpl.java:250) [jooby-0.11.2.jar:na]
    at org
.jooby.internal.HttpHandlerImpl.handle(HttpHandlerImpl.java:197) [jooby-0.11.2.jar:na]
    at org
.jooby.internal.undertow.UndertowHandler.handleRequest(UndertowHandler.java:59) [jooby-undertow-0.11.2.jar:na]
   
....

So is it possible to control the response by ourself inside a MVC route or this is not a recommended thing to do?

Also, I have Jackson registered :

use(new Jackson());

Is there a way to bypass this filter for a particular route? In my previous example, "test!" is not send as plain/text because of Jackson...
Will all routes registered after a filter like Jackson inevitably be affected, without the possibility to bypass the filter?

Thanks you!

Julien


Edgar Espina

unread,
Dec 26, 2015, 6:53:44 PM12/26/15
to jooby-project
Hi Julien,

 Thanks for trying Jooby and glad you like it!

 You can control response from inline or mvc routes with response or the utility class Results.*:

 For example here is your example as inline route:

get("/test", (req, rsp) -> {

    response
.status(418);
    response
.send("test!");
    response
.end();
});

Or via Results:

get("/test", req -> {
 
return Results.with("test!", 418);
});

Mvc with results:
@GET
@Path("/test")
public Result test() throws Exception {
  return Results.with("test!", 418);
}

Today, method return type control the response in MVC routes, that is way your code fails to produces a response... Still think your example looks good and it should work too, can you file an issue at github?

If you need to send plain/text, you have to set the content-type:

public Result test() throws Exception {
  return Results.with("test!", 418).type(MediaType.plain);
}

Next release 0.13.0 will improves Swagger integration for MVC routes. For example, it will pick your javadoc and convert that to Swagger Spec. Also, I'm planning to add Swagger supports to inline routes ;)

I'm planning to release 1.0 on 2016 (February probably), so please share all your thoughts and ideas to improves and makes Jooby even better.

Thanks

btw: Jooby was implemented in a dozen of small and medium size projects  for the company I work for (all them are privates)
Reply all
Reply to author
Forward
0 new messages