Running this single line of code will start a web server embedded in your application.
By default, the server listens on port 8888 and address 0.0.0.0 (matching any IP address).
Navigating to http://localhost:8888/size?msg=abc will return 3.
Verb | Path | Zone | Content type | MVC | View name | Roles |
---|---|---|---|---|---|---|
GET | /size | main | json |
Verb | Path | Zone | Content type | MVC | View name | Roles |
---|---|---|---|---|---|---|
GET POST | /hi | main | html | hi |
A web application can be defined as a collection of HTTP request handlers. The HTTP requests are routed to the appropriate lambda, based on the combination of:
Verb | Path | Zone | Content type | MVC | View name | Roles |
---|---|---|---|---|---|---|
GET | / | main | json | |||
GET | /x | main | html | |||
POST | /x | main | json | |||
DELETE | /x | main | html |
Verb | Path | Zone | Content type | MVC | View name | Roles |
---|---|---|---|---|---|---|
GET | /showData | main | json | |||
GET | /showPath | main | json | |||
GET | /showUri | main | json | |||
GET | /showVerb | main | json |
String verb() |
Gets the verb of the HTTP request. |
String uri() |
Gets the uri of the HTTP request. |
String path() |
Gets the path of the HTTP request. |
String query() |
Gets the query of the HTTP request. |
byte[] body() |
Gets the raw body data of the HTTP request. |
String contextPath() |
Gets the context path of the application zone handling the request. The default context path is / for the On API, and /_ for the Admin API. |
Map<String, String> params() |
Gets the URL parameters of the HTTP request. |
Map<String, Object> posted() |
Gets the posted parameters of the HTTP request body. |
Map<String, List<Upload>> files() |
Gets the uploaded files from the HTTP request body. |
Map<String, Object> data() |
Gets the data parameters (URL parameters + posted parameters + uploaded files) of the HTTP request. |
Map<String, String> headers() |
Gets the headers of the HTTP request. |
Map<String, String> cookies() |
Gets the cookies of the HTTP request. |
Verb | Path | Zone | Content type | MVC | View name | Roles |
---|---|---|---|---|---|---|
GET | / | main | html |
Resp body(byte[] body) |
Sets the HTTP response body from a byte[] data that is written as a HTTP response body when rendered. |
Resp body(ByteBuffer body) |
Sets the HTTP response body from a ByteBuffer data that is written as a HTTP response body when rendered. |
Object body() |
Gets the HTTP response body data (of type byte[] or ByteBuffer) that is written as a HTTP response body when rendered. |
Resp code(int code) |
Sets the status code (e.g. 200, 404, 500) of the HTTP response. |
int code() |
Gets the status code (e.g. 200, 404, 500) of the HTTP response. |
Resp contentType(MediaType contentType) |
Sets the Content-Type header to be rendered in the HTTP response. |
MediaType contentType() |
Gets the Content-Type header to be rendered in the HTTP response. |
Map<String, String> headers() |
Provides read/write access to the headers of the HTTP response. |
Map<String, String> cookies() |
Provides read/write access to the cookies of the HTTP response. |
Resp html(Object content) |
Sets the Content-Type: text/html; charset=utf-8 header and the content of the HTTP response. Alias to contentType(MediaType.HTML_UTF_8).body(content) . |
Resp json(Object content) |
Sets the Content-Type: application/json; charset=utf-8 header and the content of the HTTP
response. Alias to contentType(MediaType.JSON).body(content) . |
if
. The traditional {{#x}}...{{/x}} remains unchanged, having both foreach
and if
semantics.You have 12 new messages.
You have 100 new messages.
Verb | Path | Zone | Content type | MVC | View name | Roles |
---|---|---|---|---|---|---|
GET | /abc | main | html | msg | ||
GET POST | /msg | main | html | msg |
Resp view(String viewName) |
Sets a custom name of the view (V from MVC) of the HTTP response. This also sets mvc to true . The default view name equals the request path without the "/" prefix, except for the "/" path, where the view name is "index". E.g. "/abc" -> "abc", "/" -> "index", "/my/books" -> "my/books". |
String view() |
Gets the (default or customized) name of the view (V from MVC) of the HTTP response. The default view name equals the request path without the "/" prefix, except for the "/" path, where the view name is "index". E.g. "/abc" -> "abc", "/" -> "index", "/my/books" -> "my/books". |
Resp noView() |
Disables the view rendering for the target MVC route. The page decorator remains enabled. |
Map<String, Object> model() |
Provides read/write access to the model (M from MVC) that will be rendered by the view renderer. |
Resp model(String name, Object value) |
Sets an attribute of the model (M from MVC) that will be rendered by the view renderer. |
Verb | Path | Zone | Content type | MVC | View name | Roles |
---|---|---|---|---|---|---|
GET | / | main | html |
Verb | Path | Zone | Content type | MVC | View name | Roles |
---|---|---|---|---|---|---|
GET | / | main | html | |||
POST | /_login | main | json | |||
GET | /_logout | main | json | logged_in | ||
GET | /hi | main | json |
administrator
manager
moderator
|
||
GET | /manage | main | html | manager |
The server-side session is a simple in-memory storage in the web server. While this is not a problem when running only one web server, scaling out is non-trivial, requiring sticky sessions or replicating the session, or storing it into some datastore.
String sessionId() |
Returns the ID of the session (the value of the "JSESSIONID" cookie). If a session doesn't exist, a new session is created. |
boolean hasSession() |
Does the HTTP request have a server-side session attached? |
Map<String, Serializable> session() |
Provides read/write access to the server-side session attributes of the HTTP request/response. |
T session(String name) |
Returns the value of the specified server-side session attribute from the HTTP request/response, or throws a runtime exception if it is not found. |
T session(String name, T defaultValue) |
Returns the value of the specified server-side session attribute from the HTTP request/response, or the specified default value, if it is not found. |
Map<String, Serializable> session() |
Provides read/write access to the server-side session attributes of the HTTP request/response. |
Resp session(String name, Serializable value) |
Sets a session attribute of the HTTP response. |
boolean hasToken() |
Does the HTTP request have a token attached? |
Map<String, Serializable> token() |
Provides read/write access to the token attributes of the HTTP request/response. |
T token(String name) |
Returns the value of the specified token attribute from the HTTP request/response, or throws a runtime exception if it is not found. |
T token(String name, T defaultValue) |
Returns the value of the specified token attribute from the HTTP request/response, or the specified default value, if it is not found. |
Map<String, Serializable> token() |
Provides read/write access to the token attributes of the HTTP request/response. |
Resp token(String name, Serializable value) |
Sets a token attribute of the HTTP response. |
The content type of the the HTTP response is automatically set to application/json for RESTful services, and to text/html for web pages.
However, it's easy to modify it using the Resp#contentType method:
Verb | Path | Zone | Content type | MVC | View name | Roles |
---|---|---|---|---|---|---|
GET | / | main | html |
MediaType contentType() |
Gets the Content-Type header of the HTTP response if it has been assigned,
or the default value as configured in the HTTP route. |
Resp contentType(MediaType contentType) |
Sets the Content-Type header to be rendered in the HTTP response. |
MediaType contentType() |
Gets the Content-Type header to be rendered in the HTTP response. |
The HTTP response code is automatically set to 200 when the lambda executes successfully. In case of redirect, it is automatically set to 303. In case of error, it is automatically set to 500, or if a lambda was not found for the request, it is set to 404.
However, it's easy to modify it using the Resp#code method:
Verb | Path | Zone | Content type | MVC | View name | Roles |
---|---|---|---|---|---|---|
GET | / | main | html |
Resp code(int code) |
Sets the status code (e.g. 200, 404, 500) of the HTTP response. |
int code() |
Gets the status code (e.g. 200, 404, 500) of the HTTP response. |
Verb | Path | Zone | Content type | MVC | View name | Roles |
---|---|---|---|---|---|---|
GET | /size | main | json | |||
GET | /upper | main | json |
Verb | Path | Zone | Content type | MVC | View name | Roles |
---|---|---|---|---|---|---|
GET | / | main | json |
Verb | Path | Zone | Content type | MVC | View name | Roles |
---|---|---|---|---|---|---|
GET | / | main | json |
Req async() |
Informs the HTTP server that the request will be handled asynchronously (typically on another thread). When the
response is complete, the Req#done() or Resp#done() method must be called, to
inform the server. |
boolean isAsync() |
Is/was the request being handled in asynchronous mode? |
Req done() |
Informs the HTTP server that the asynchronous handling has finished and the response is complete. |
boolean isDone() |
Has the request handling and response construction finished? |
Resp done() |
Informs the HTTP server that the asynchronous handling has finished and the response is complete. Alias to request().done() . |
Verb | Path | Zone | Content type | MVC | View name | Roles |
---|---|---|---|---|---|---|
GET | /hey/{name}/{age:\d+} | main | json | |||
POST | /size/{s} | main | json |
Verb | Path | Zone | Content type | MVC | View name | Roles |
---|---|---|---|---|---|---|
POST | /parts | main | json | |||
GET | /upper/{s} | main | json |
Verb | Path | Zone | Content type | MVC | View name | Roles |
---|---|---|---|---|---|---|
GET | /echo | main | json |
Verb | Path | Zone | Content type | MVC | View name | Roles |
---|---|---|---|---|---|---|
GET | /sum | main | json |
Verb | Path | Zone | Content type | MVC | View name | Roles |
---|---|---|---|---|---|---|
GET POST | / | main | html | index |
Verb | Path | Zone | Content type | MVC | View name | Roles |
---|---|---|---|---|---|---|
GET POST | /hi/{name} | main | html | hi/name |
RAW HTML!
Verb | Path | Zone | Content type | MVC | View name | Roles |
---|---|---|---|---|---|---|
GET POST | /simple | main | html | simple |
Key | Value |
---|---|
1 | one |
5 | five |
Number | As Text |
---|---|
1 | one |
5 | five |
Key | Value |
---|---|
1 | one! |
5 | five! |
Verb | Path | Zone | Content type | MVC | View name | Roles |
---|---|---|---|---|---|---|
GET POST | / | main | html | index |
Verb | Path | Zone | Content type | MVC | View name | Roles |
---|---|---|---|---|---|---|
GET POST | / | main | html | index |
Key | Value |
---|---|
name | Rambo |
age | 50 |
weapon | gun |
Verb | Path | Zone | Content type | MVC | View name | Roles |
---|---|---|---|---|---|---|
GET POST | /table | main | html | table |
Verb | Path | Zone | Content type | MVC | View name | Roles |
---|---|---|---|---|---|---|
GET | /bar/hi | main | json | |||
GET | /foo/hi | main | json |
Verb | Path | Zone | Content type | MVC | View name | Roles |
---|---|---|---|---|---|---|
GET POST | / | main | html | index |
Key | Value |
---|---|
msg | hello from Foo! |
desc | simple example |
Verb | Path | Zone | Content type | MVC | View name | Roles |
---|---|---|---|---|---|---|
GET | /hi | main | json | |||
GET | /my | main | html | my | ||
GET | /profiles | main | html | profiles |
Verb | Path | Zone | Content type | MVC | View name | Roles |
---|---|---|---|---|---|---|
GET | /books | main | json | |||
POST | /books | main | json |
Name | Desc | Default value |
---|---|---|
config | configuration filename prefix | config |
dev | run in DEV mode | auto-detected |
production | run in PRODUCTION mode | auto-detected |
test | run in TEST mode | auto-detected |
secret=<SECRET> | configure secret key for cryptography | random |
profiles=<P1,P2...> | comma-separated list of application profiles (e.g. mysql,prod) | the 'default' profile |
on.port=<P> | the default App server will listen at port P | 8888 |
on.address=<ADDR> | the default App server will listen at address ADDR | 0.0.0.0 |
admin.port=<P> | the Admin server will listen at port P | same as on.port |
admin.address=<ADDR> | the Admin server will listen at address ADDR | on.address |
app.services=<S1,S2...> | comma-separated list of services to bootstrap on the App server | none |
admin.services=<S1,S2...> | comma-separated list of services to bootstrap on the Admin server | none |
You can use Rapidoid as a Network Protocol Framework, to implement your custom TCP-based protocols.
For a quick and easy start, please take a look at the following Echo protocol example:
The low-level HTTP API provides access to the extremely fast Rapidoid HTTP server components.
Rapidoid is implemented on top of Java NIO, without native libraries, nor external dependencies.
It is great to see how Java performance can match C++ and outperform all the other languages in network programming.
Rapidoid was designed for high performance since its inception, and several parts of it played a critical role:
If we want to implement any protocol in the fastest way posible, then we need to do it on a lower level of abstraction.
Let's see how we can implement one of the fastest web servers in the world! (based on transparent TechEmpower benchmarks)
The following HTTP server features 2 web handlers:
/plaintext
- which implements the TechEmpower's Plaintext test/json
- which implements the TechEmpower's JSON serialization testHTTP REQUEST DATA |
|
String verb() |
Gets the verb of the HTTP request. |
String uri() |
Gets the uri of the HTTP request. |
String path() |
Gets the path of the HTTP request. |
String query() |
Gets the query of the HTTP request. |
byte[] body() |
Gets the raw body data of the HTTP request. |
String host() |
Gets the value of the Host header of the HTTP request. |
String zone() |
Gets the name of the application zone handling the request. The default zone name is main for the On API, and admin for the Admin API. |
String contextPath() |
Gets the context path of the application zone handling the request. The default context path is / for the On API, and /_ for the Admin API. |
String clientIpAddress() |
Gets the IP address of the HTTP client directly sending the request. This can be the address of a real user, or a HTTP proxy (if the user uses such), or a reverse proxy (if the application/server uses such). |
String realIpAddress() |
A best-effort attempt to infer the real IP address of the end user/client/client proxy. If a reverse proxy is detected with high confidence (or configured), its headers will be used to get the real IP address of the user. Otherwise, the value of Req#clientIpAddress() is returned. |
long connectionId() |
Gets the HTTP connection ID, which is unique per HTTP server instance. |
long requestId() |
Gets the HTTP request ID, which is unique per HTTP server instance. |
URL PARAMETERS: |
|
Map<String, String> params() |
Gets the URL parameters of the HTTP request. |
String param(String name) |
Returns the value of the specified mandatory URL parameter from the HTTP request, or throws a runtime exception if it is not found. |
String param(String name, String defaultValue) |
Returns the value of the specified optional URL parameter from the HTTP request, or the specified default value, if not found. |
T param(Class<T> beanType) |
Returns a new instance of the specified bean type, with properties initialized from the URL parameters of the HTTP request. |
POSTED PARAMETERS IN THE REQUEST BODY: |
|
Map<String, Object> posted() |
Gets the posted parameters of the HTTP request body. |
T posted(String name) |
Returns the value of the specified posted parameter from the HTTP request body, or throws a runtime exception if it is not found. |
T posted(String name, T defaultValue) |
Returns the value of the specified posted parameter from the HTTP request body, or the specified default value, if it is not found. |
T posted(Class<T> beanType) |
Returns a new instance of the specified bean type, with properties initialized from the posted parameters of the HTTP request. |
UPLOADED FILES IN THE REQUEST BODY: |
|
Map<String, List<Upload>> files() |
Gets the uploaded files from the HTTP request body. |
List<Upload> files(String name) |
Returns the uploaded files with the specified form parameter name (not filename) from the HTTP request body, or throws a runtime exception if not found. |
Upload file(String name) |
Returns exactly one posted file with the specified form parameter name (not filename) from the HTTP request body, or throws a runtime exception if not found. |
REQUEST DATA PARAMETERS (URL PARAMETERS + POSTED PARAMETERS + UPLOADED FILES): |
|
Map<String, Object> data() |
Gets the data parameters (URL parameters + posted parameters + uploaded files) of the HTTP request. |
T data(String name) |
Returns the value of the specified data parameter from the HTTP request, or throws a runtime exception if it is not found. |
T data(String name, T defaultValue) |
Returns the value of the specified data parameter from the HTTP request, or the specified default value, if it is not found. |
T data(Class<T> beanType) |
Returns a new instance of the specified bean type, with properties initialized from the data parameters of the HTTP request. |
EXTRA ATTRIBUTES ATTACHED TO THE REQUEST: |
|
Map<String, Object> attrs() |
Gets the extra attributes of the HTTP request. |
T attr(String name) |
Returns the value of an extra attribute from the HTTP request, or throws a runtime exception if it is not found. |
T attr(String name, T defaultValue) |
Returns the value of the specified extra attribute from the HTTP request, or the specified default value, if it is not found. |
SERVER-SIDE SESSION: |
|
String sessionId() |
Returns the ID of the session (the value of the "JSESSIONID" cookie). If a session doesn't exist, a new session is created. |
boolean hasSession() |
Does the HTTP request have a server-side session attached? |
Map<String, Serializable> session() |
Provides read/write access to the server-side session attributes of the HTTP request/response. |
T session(String name) |
Returns the value of the specified server-side session attribute from the HTTP request/response, or throws a runtime exception if it is not found. |
T session(String name, T defaultValue) |
Returns the value of the specified server-side session attribute from the HTTP request/response, or the specified default value, if it is not found. |
TOKEN DATA: |
|
boolean hasToken() |
Does the HTTP request have a token attached? |
Map<String, Serializable> token() |
Provides read/write access to the token attributes of the HTTP request/response. |
T token(String name) |
Returns the value of the specified token attribute from the HTTP request/response, or throws a runtime exception if it is not found. |
T token(String name, T defaultValue) |
Returns the value of the specified token attribute from the HTTP request/response, or the specified default value, if it is not found. |
REQUEST HEADERS: |
|
Map<String, String> headers() |
Gets the headers of the HTTP request. |
String header(String name) |
Returns the value of the specified header from the HTTP request, or throws a runtime exception if it is not found. |
String header(String name, String defaultValue) |
Returns the value of the specified header from the HTTP request, or the specified default value, if it is not found. |
REQUEST COOKIES: |
|
Map<String, String> cookies() |
Gets the cookies of the HTTP request. |
String cookie(String name) |
Returns the value of the specified cookie from the HTTP request, or throws a runtime exception if it is not found. |
String cookie(String name, String defaultValue) |
Returns the value of the specified cookie from the HTTP request, or the specified default value, if it is not found. |
RESPONSE: |
|
Resp response() |
Gets the reference to the response object. |
ASYNCHRONOUS REQUEST HANDLING: |
|
Req async() |
Informs the HTTP server that the request will be handled asynchronously (typically on another thread). When the
response is complete, the Req#done() or Resp#done() method must be called, to
inform the server. |
boolean isAsync() |
Is/was the request being handled in asynchronous mode? |
Req done() |
Informs the HTTP server that the asynchronous handling has finished and the response is complete. |
boolean isDone() |
Has the request handling and response construction finished? |
WEB APPLICATION SETUP: |
|
HttpRoutes routes() |
Provides access to the HTTP routes of the web application setup. |
Route route() |
Provides access to the matching HTTP route (if any) of the web application setup. In case a generic handler handles the request, or no matching route was found, null is returned. |
Customization custom() |
Provides access to the customization of the web application setup. |
void revert() |
Reverts the previous processing of the request, usually with intention to process the same request again. |
OutputStream out() |
First renders the response headers, then returns an OutputStream representing the response body. The response body will be constructed by writing to the OutputStream. |
MediaType contentType() |
Gets the Content-Type header of the HTTP response if it has been assigned,
or the default value as configured in the HTTP route. |
long handle() |
Returns the request handle, which is used when resuming the request handling in asynchronous way. See Resp#resume . |
Resp result(Object content) |
Sets the content to be serialized into a body when the HTTP response is rendered. |
Object result() |
Gets the content to be serialized into a body when the HTTP response is rendered. |
Resp body(byte[] body) |
Sets the HTTP response body from a byte[] data that is written as a HTTP response body when rendered. |
Resp body(ByteBuffer body) |
Sets the HTTP response body from a ByteBuffer data that is written as a HTTP response body when rendered. |
Object body() |
Gets the HTTP response body data (of type byte[] or ByteBuffer) that is written as a HTTP response body when rendered. |
Resp raw(byte[] raw) |
Sets the raw HTTP response (headers and body) from a byte[] data that is written as a HTTP response when rendered. |
Resp raw(ByteBuffer raw) |
Sets the raw HTTP response (headers and body) from a ByteBuffer data that is written as a HTTP response when rendered. |
Object raw() |
Gets the raw HTTP response (headers and body) data (of type byte[] or ByteBuffer) that is written as a HTTP response when rendered. |
Resp code(int code) |
Sets the status code (e.g. 200, 404, 500) of the HTTP response. |
int code() |
Gets the status code (e.g. 200, 404, 500) of the HTTP response. |
Resp contentType(MediaType contentType) |
Sets the Content-Type header to be rendered in the HTTP response. |
MediaType contentType() |
Gets the Content-Type header to be rendered in the HTTP response. |
Resp redirect(String redirectURI) |
Sets the redirect URI of the HTTP response. Setting this will cause a HTTP 30x redirect response. |
String redirect() |
Gets the redirect URI of the HTTP response. |
Resp filename(String filename) |
Sets the filename when serving a file in the HTTP response. |
String filename() |
Gets the filename when serving a file in the HTTP response. |
Resp view(String viewName) |
Sets a custom name of the view (V from MVC) of the HTTP response. This also sets mvc to true . The default view name equals the request path without the "/" prefix, except for the "/" path, where the view name is "index". E.g. "/abc" -> "abc", "/" -> "index", "/my/books" -> "my/books". |
String view() |
Gets the (default or customized) name of the view (V from MVC) of the HTTP response. The default view name equals the request path without the "/" prefix, except for the "/" path, where the view name is "index". E.g. "/abc" -> "abc", "/" -> "index", "/my/books" -> "my/books". |
Resp noView() |
Disables the view rendering for the target MVC route. The page decorator remains enabled. |
Resp file(File file) |
Sets the file to be served when the HTTP response is rendered. |
File file() |
Gets the file to be served when the HTTP response is rendered. |
Map<String, String> headers() |
Provides read/write access to the headers of the HTTP response. |
Resp header(String name, String value) |
Sets a header of the HTTP response. |
Map<String, String> cookies() |
Provides read/write access to the cookies of the HTTP response. |
Resp cookie(String name, String value, String... extras) |
Sets a cookie of the HTTP response. |
Map<String, Serializable> session() |
Provides read/write access to the server-side session attributes of the HTTP request/response. |
Resp session(String name, Serializable value) |
Sets a session attribute of the HTTP response. |
Map<String, Serializable> token() |
Provides read/write access to the token attributes of the HTTP request/response. |
Resp token(String name, Serializable value) |
Sets a token attribute of the HTTP response. |
Map<String, Object> model() |
Provides read/write access to the model (M from MVC) that will be rendered by the view renderer. |
Resp model(String name, Object value) |
Sets an attribute of the model (M from MVC) that will be rendered by the view renderer. |
Resp done() |
Informs the HTTP server that the asynchronous handling has finished and the response is complete. Alias to request().done() . |
Resp plain(Object content) |
Sets the Content-Type: text/plain; charset=utf-8 header and the content of the HTTP response. Alias to contentType(MediaType.PLAIN_TEXT_UTF_8).body(content) . |
Resp html(Object content) |
Sets the Content-Type: text/html; charset=utf-8 header and the content of the HTTP response. Alias to contentType(MediaType.HTML_UTF_8).body(content) . |
Resp json(Object content) |
Sets the Content-Type: application/json header and the content of the HTTP response. Alias to contentType(MediaType.JSON).body(content) . |
Resp binary(Object content) |
Sets the Content-Type: application/octet-stream header and the content of the HTTP response. Alias to contentType(MediaType.BINARY).body(content) . |
boolean mvc() |
Checks whether the response model and view will be rendered in a MVC fashion. A typical renderer would use Resp#view to get the view name, and Resp#model to get the model.
A custom view renderer can be configured/implemented via the On.custom().viewResolver(...) method. |
Resp mvc(boolean mvc) |
Sets whether the response model and view will be rendered in a MVC fashion. A typical renderer would use Resp#view to get the view name, and Resp#model to get the model.
A custom view renderer can be configured/implemented via the On.custom().viewResolver(...) method. |
OutputStream out() |
First renders the response headers, then returns an OutputStream representing the response body. The response body will be constructed by writing to the OutputStream. |
Req request() |
Gets the reference to the request object. |
boolean login(String username, String password) |
Initiates a user login process with the specified username and password. After a successful login, the username will be persisted in the token. Returns information whether the login was successful |
void logout() |
Initiates a user logout process, clearing the login information (username) from the token. |
Screen screen() |
Provides access to the screen model for custom (MVC) page rendering. |
void resume(AsyncLogic asyncLogic) |
Resumes the asynchronous request handling. |