开发者

minimalistic Java web interface like Pythons WSGI

Python has a minimalistic and elegant web interface called WSGI. The simplest possible application looks like this:

def simple_app(environ, start_response):
    status = '200 OK'
    response_headers = [('Content-type', 'text/plain')]
    start_response(status, response_headers)
    return ['Hello world!\n']

environ is something like a request object and start_response is a function to set the resonse headers. The response body is an iterable object, here a list with a single String ('Hello world!\n'). This simple interface unifies what is done in Java with Servlets and 开发者_运维知识库Filters.

I wonder if something similar could be written in Java. To return an iterator in Java would be slower than using a stream, I would expect. So I think that passing in an OutputStream (wrapped in a response object), like the Servlet API does, is the best solution. It would look like this:

public interface Handler {
    void handle(Request req, Response res);
}

However, I'd prefer if the response would be returned:

public interface Handler {
    Response handle(Request req);
}

But then the only way to pass in an OutputStream would be the Request (Request#createResponse()).

ASP.NET actions return "Result" objects, but I don't know much about it and have no idea how streaming is performed by the framework. How would you do something similar in Java?

I'm looking forward to your ideas for a simple and elegant Java web API.


Using native IO and Network API of java, we can create ServerSocket of java.net package.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜