Javalin é um simples web framework para Java e Kotlin.
Criei um projeto de aplicação Java com Maven, e vou adicionar a dependência do Javalin e do slf4j-simple no pom.xml.
<dependencies>
<dependency>
<groupId>io.javalin</groupId>
<artifactId>javalin</artifactId>
<version>3.1.0</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>1.7.26</version>
</dependency>
</dependencies>
Na classe principal vou adicionar os seguintes comandos:
public class Start {
public static void main(String[] args) {
Javalin app = Javalin.create().start(7000);
app.get(“/”, ctx -> ctx.result(“Hello World”));
}
}
Agora execute, se de tudo certo você vai aparecer algo assim:
Se você acessar o http://localhost:7000/ vai ver a mensagem Hello World.
Manipuladores (Handlers)
Javalin tem três tipos de manipuladores: before-handlers, endpoint-handlers e after-handlers. (Há tembém o exception-handlers e error-handlers, iremos vê-los depois). O before-, endpoint- e o after-handlers são formados por três partes:
Um verbo: exemplo, before, get, post, put, delete, after
Um destino: exemplo, /, /hello-world
Uma implementação de manipulador: ctx -> {…}
A interface Handler tem o retorno do tipo void. Você utiliza ctx.result() para configurar a resposta que retornará para o usuário.
Before Handlers
Before-handlers são utilizados antes de qualquer request (incluindo arquivos estáticos, se você habilitar isto).
- Java
- Kotlin
1 2 3 4 5 6 7 |
<span class="n">app</span><span class="o">.</span><span class="na">before</span><span class="o">(</span><span class="n">ctx</span> <span class="o">-></span> <span class="o">{</span> <span class="c1">// runs before all requests</span> <span class="o">});</span> <span class="n">app</span><span class="o">.</span><span class="na">before</span><span class="o">(</span><span class="s">"/path/*"</span><span class="o">,</span> <span class="n">ctx</span> <span class="o">-></span> <span class="o">{</span> <span class="c1">// runs before request to /path/*</span> <span class="o">});</span> |
1 2 3 4 5 6 7 |
<span class="n">app</span><span class="p">.</span><span class="n">before</span> <span class="p">{</span> <span class="n">ctx</span> <span class="p">-></span> <span class="c1">// runs before all requests </span><span class="p">}</span> <span class="n">app</span><span class="p">.</span><span class="n">before</span><span class="p">(</span><span class="s">"/path/*"</span><span class="p">)</span> <span class="p">{</span> <span class="n">ctx</span> <span class="p">-></span> <span class="c1">// runs before request to /path/* </span><span class="p">}</span> |
Endpoint Handlers
Endpoint-handlers são utilizados na ordem que foram definidos.
- Java
- Kotlin
1 2 3 4 5 6 7 8 9 10 |
<span class="n">app</span><span class="o">.</span><span class="na">get</span><span class="o">(</span><span class="s">"/"</span><span class="o">,</span> <span class="n">ctx</span> <span class="o">-></span> <span class="o">{</span> <span class="c1">// some code</span> <span class="n">ctx</span><span class="o">.</span><span class="na">json</span><span class="o">(</span><span class="n">object</span><span class="o">);</span> <span class="o">});</span> <span class="n">app</span><span class="o">.</span><span class="na">post</span><span class="o">(</span><span class="s">"/"</span><span class="o">,</span> <span class="n">ctx</span> <span class="o">-></span> <span class="o">{</span> <span class="c1">// some code</span> <span class="n">ctx</span><span class="o">.</span><span class="na">status</span><span class="o">(</span><span class="mi">201</span><span class="o">);</span> <span class="o">});</span> |
1 2 3 4 5 6 7 8 9 10 |
<span class="n">app</span><span class="p">.</span><span class="k">get</span><span class="p">(</span><span class="s">"/"</span><span class="p">)</span> <span class="p">{</span> <span class="n">ctx</span> <span class="p">-></span> <span class="c1">// some code </span> <span class="n">ctx</span><span class="p">.</span><span class="n">json</span><span class="p">(</span><span class="k">object</span><span class="p">)</span> <span class="p">}</span> <span class="n">app</span><span class="p">.</span><span class="n">post</span><span class="p">(</span><span class="s">"/"</span><span class="p">)</span> <span class="p">{</span> <span class="n">ctx</span> <span class="p">-></span> <span class="c1">// some code </span> <span class="n">ctx</span><span class="p">.</span><span class="n">status</span><span class="p">(</span><span class="m">201</span><span class="p">)</span> <span class="p">}</span> |
Destinos do Handler podem incluir parâmetros. Eles são passados via ctx.pathParam(“key”):
- Java
- Kotlin
1 2 3 4 |
<span class="n">app</span><span class="o">.</span><span class="na">get</span><span class="o">(</span><span class="s">"/hello/:name"</span><span class="o">,</span> <span class="n">ctx</span> <span class="o">-></span> <span class="o">{</span> <span class="n">ctx</span><span class="o">.</span><span class="na">result</span><span class="o">(</span><span class="s">"Hello: "</span> <span class="o">+</span> <span class="n">ctx</span><span class="o">.</span><span class="na">pathParam</span><span class="o">(</span><span class="s">"name"</span><span class="o">));</span> <span class="o">});</span> |
1 2 3 4 |
<span class="n">app</span><span class="p">.</span><span class="k">get</span><span class="p">(</span><span class="s">"/hello/:name"</span><span class="p">)</span> <span class="p">{</span> <span class="n">ctx</span> <span class="p">-></span> <span class="n">ctx</span><span class="p">.</span><span class="n">result</span><span class="p">(</span><span class="s">"Hello: "</span> <span class="p">+</span> <span class="n">ctx</span><span class="p">.</span><span class="n">pathParam</span><span class="p">(</span><span class="s">"name"</span><span class="p">))</span> <span class="p">}</span> |
Destinos do Handler aceitam como parâmetros wildcard, que são chamados de path-parameters:
- Java
- Kotlin
1 2 3 4 |
<span class="n">app</span><span class="o">.</span><span class="na">get</span><span class="o">(</span><span class="err">"</span><span class="o">/</span><span class="n">hello</span><span class="o">/*,</span> <span class="n">ctx</span> <span class="o">-></span> <span class="o">{</span> <span class="c1">// capture all request to sub-paths of /hello/</span> <span class="o">});</span> |
1 2 3 4 |
<span class="n">app</span><span class="p">.</span><span class="k">get</span><span class="p">(</span><span class="s">"/hello/*"</span><span class="p">)</span> <span class="p">{</span> <span class="n">ctx</span> <span class="p">-></span> <span class="c1">// capture all request to sub-paths of /hello/ </span><span class="p">}</span> |
After Handlers
After-handlers executam depois de cada request (mesmo que ocorra uma exceção)
- Java
- Kotlin
1 2 3 4 5 6 7 |
<span class="n">app</span><span class="o">.</span><span class="na">after</span><span class="o">(</span><span class="n">ctx</span> <span class="o">-></span> <span class="o">{</span> <span class="c1">// run after all requests</span> <span class="o">});</span> <span class="n">app</span><span class="o">.</span><span class="na">after</span><span class="o">(</span><span class="s">"/path/*"</span><span class="o">,</span> <span class="n">ctx</span> <span class="o">-></span> <span class="o">{</span> <span class="c1">// runs after request to /path/*</span> <span class="o">});</span> |
1 2 3 4 5 6 7 |
<span class="n">app</span><span class="p">.</span><span class="n">after</span> <span class="p">{</span> <span class="n">ctx</span> <span class="p">-></span> <span class="c1">// run after all requests </span><span class="p">}</span> <span class="n">app</span><span class="p">.</span><span class="n">after</span><span class="p">(</span><span class="s">"/path/*"</span><span class="p">)</span> <span class="p">{</span> <span class="n">ctx</span> <span class="p">-></span> <span class="c1">// runs after request to /path/* </span><span class="p">}</span> |
Contex
O objeto Context fornece tudo que você precisa para a manipulação de um http-request. Ele contém o servlet-request e o servlet-response, e um grupo de getters e setters. Os getters operam na maioria das vezes no request-object, enquanto os setters operam exclusivamente no response object.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
<span class="n">ctx</span><span class="o">.</span><span class="na">appAttribute</span><span class="o">(</span><span class="kd">class</span><span class="err">)</span> <span class="err">//</span> <span class="nc">get</span> <span class="n">an</span> <span class="n">attribute</span> <span class="n">set</span> <span class="n">on</span> <span class="n">the</span> <span class="n">app</span> <span class="n">ctx</span><span class="o">.</span><span class="na">register</span><span class="o">(</span><span class="kd">class</span><span class="err">,</span> <span class="nc">object</span><span class="o">)</span> <span class="c1">// register an extension on the context</span> <span class="n">ctx</span><span class="o">.</span><span class="na">use</span><span class="o">(</span><span class="kd">class</span><span class="err">)</span> <span class="err">//</span> <span class="nc">use</span> <span class="n">an</span> <span class="n">extension</span> <span class="n">on</span> <span class="n">the</span> <span class="n">context</span> <span class="n">ctx</span><span class="o">.</span><span class="na">cookieStore</span><span class="o">(</span><span class="n">key</span><span class="o">)</span> <span class="c1">// get cookie store value</span> <span class="n">ctx</span><span class="o">.</span><span class="na">cookieStore</span><span class="o">(</span><span class="n">key</span><span class="o">,</span> <span class="n">value</span><span class="o">)</span> <span class="c1">// set a cookie store value</span> <span class="n">ctx</span><span class="o">.</span><span class="na">clearCookieStore</span><span class="o">()</span> <span class="c1">// clear the cookie store</span> <span class="n">ctx</span><span class="o">.</span><span class="na">matchedPath</span><span class="o">()</span> <span class="c1">// path that was used to match request (also includes before/after paths)</span> <span class="n">ctx</span><span class="o">.</span><span class="na">endpointHandlerPath</span><span class="o">()</span> <span class="c1">// endpoint path that was used to match request (null in before, available in after)</span> <span class="c1">// Request methods</span> <span class="n">ctx</span><span class="o">.</span><span class="na">body</span><span class="o">()</span> <span class="c1">// get body as string</span> <span class="n">ctx</span><span class="o">.</span><span class="na">bodyAsBytes</span><span class="o">()</span> <span class="c1">// get body as bytes</span> <span class="n">ctx</span><span class="o">.</span><span class="na">bodyAsClass</span><span class="o">(</span><span class="kd">class</span><span class="err">)</span> <span class="err">//</span> <span class="nc">get</span> <span class="n">body</span> <span class="n">as</span> <span class="kd">class</span> <span class="nc">ctx</span><span class="o">.</span><span class="na">bodyValidator</span><span class="o">(</span><span class="kd">class</span><span class="err">)</span> <span class="err">//</span> <span class="nc">get</span> <span class="n">typed</span> <span class="n">validator</span> <span class="k">for</span> <span class="n">body</span> <span class="n">ctx</span><span class="o">.</span><span class="na">uploadedFile</span><span class="o">(</span><span class="n">name</span><span class="o">)</span> <span class="c1">// get uploaded file by name</span> <span class="n">ctx</span><span class="o">.</span><span class="na">uploadedFiles</span><span class="o">(</span><span class="n">name</span><span class="o">)</span> <span class="c1">// get uploaded file(s) by name</span> <span class="n">ctx</span><span class="o">.</span><span class="na">formParam</span><span class="o">(</span><span class="n">key</span><span class="o">)</span> <span class="c1">// get form parameter</span> <span class="n">ctx</span><span class="o">.</span><span class="na">formParam</span><span class="o">(</span><span class="n">key</span><span class="o">,</span> <span class="k">default</span><span class="o">)</span> <span class="c1">// get form parameter (or default value)</span> <span class="n">ctx</span><span class="o">.</span><span class="na">formParam</span><span class="o">(</span><span class="n">key</span><span class="o">,</span> <span class="kd">class</span><span class="err">)</span> <span class="err">//</span> <span class="nc">get</span> <span class="n">form</span> <span class="n">parameter</span> <span class="n">as</span> <span class="kd">class</span> <span class="nc">ctx</span><span class="o">.</span><span class="na">formParam</span><span class="o">(</span><span class="n">key</span><span class="o">,</span> <span class="kd">class</span><span class="err">,</span> <span class="nc">default</span><span class="o">)</span> <span class="c1">// get form parameter (or default value) as class</span> <span class="n">ctx</span><span class="o">.</span><span class="na">formParams</span><span class="o">(</span><span class="n">key</span><span class="o">)</span> <span class="c1">// get form parameters (multiple)</span> <span class="n">ctx</span><span class="o">.</span><span class="na">formParamMap</span><span class="o">()</span> <span class="c1">// get form parameter map</span> <span class="n">ctx</span><span class="o">.</span><span class="na">pathParam</span><span class="o">(</span><span class="n">key</span><span class="o">)</span> <span class="c1">// get path parameter</span> <span class="n">ctx</span><span class="o">.</span><span class="na">pathParam</span><span class="o">(</span><span class="n">key</span><span class="o">,</span> <span class="kd">class</span><span class="err">)</span> <span class="err">//</span> <span class="nc">get</span> <span class="n">path</span> <span class="n">as</span> <span class="kd">class</span> <span class="nc">ctx</span><span class="o">.</span><span class="na">pathParamMap</span><span class="o">()</span> <span class="c1">// get path parameter map</span> <span class="n">ctx</span><span class="o">.</span><span class="na">basicAuthCredentials</span><span class="o">()</span> <span class="c1">// get basic auth credentials (username/pwd)</span> <span class="n">ctx</span><span class="o">.</span><span class="na">attribute</span><span class="o">(</span><span class="n">key</span><span class="o">)</span> <span class="c1">// get request attribute</span> <span class="n">ctx</span><span class="o">.</span><span class="na">attributeMap</span><span class="o">()</span> <span class="c1">// get request attribute map</span> <span class="n">ctx</span><span class="o">.</span><span class="na">contentLength</span><span class="o">()</span> <span class="c1">// get request content length</span> <span class="n">ctx</span><span class="o">.</span><span class="na">contentType</span><span class="o">()</span> <span class="c1">// get request content type</span> <span class="n">ctx</span><span class="o">.</span><span class="na">cookie</span><span class="o">(</span><span class="n">name</span><span class="o">)</span> <span class="c1">// get request cookie</span> <span class="n">ctx</span><span class="o">.</span><span class="na">cookieMap</span><span class="o">()</span> <span class="c1">// get request cookie map</span> <span class="n">ctx</span><span class="o">.</span><span class="na">header</span><span class="o">(</span><span class="n">header</span><span class="o">)</span> <span class="c1">// get request header</span> <span class="n">ctx</span><span class="o">.</span><span class="na">headerMap</span><span class="o">()</span> <span class="c1">// get request header map</span> <span class="n">ctx</span><span class="o">.</span><span class="na">host</span><span class="o">()</span> <span class="c1">// get request host</span> <span class="n">ctx</span><span class="o">.</span><span class="na">ip</span><span class="o">()</span> <span class="c1">// get request host</span> <span class="n">ctx</span><span class="o">.</span><span class="na">isMultipart</span><span class="o">()</span> <span class="c1">// check if request is multipart</span> <span class="n">ctx</span><span class="o">.</span><span class="na">isMultipartFormData</span><span class="o">()</span> <span class="c1">// check if request is multipart/form data</span> <span class="n">ctx</span><span class="o">.</span><span class="na">method</span><span class="o">()</span> <span class="c1">// get request method</span> <span class="n">ctx</span><span class="o">.</span><span class="na">path</span><span class="o">()</span> <span class="c1">// get request path</span> <span class="n">ctx</span><span class="o">.</span><span class="na">port</span><span class="o">()</span> <span class="c1">// get request port</span> <span class="n">ctx</span><span class="o">.</span><span class="na">protocol</span><span class="o">()</span> <span class="c1">// get request protocol</span> <span class="n">ctx</span><span class="o">.</span><span class="na">queryParam</span><span class="o">(</span><span class="n">key</span><span class="o">)</span> <span class="c1">// get query parameter</span> <span class="n">ctx</span><span class="o">.</span><span class="na">queryParam</span><span class="o">(</span><span class="n">key</span><span class="o">,</span> <span class="k">default</span><span class="o">)</span> <span class="c1">// get query parameter (or default value)</span> <span class="n">ctx</span><span class="o">.</span><span class="na">queryParam</span><span class="o">(</span><span class="n">key</span><span class="o">,</span> <span class="kd">class</span><span class="err">)</span> <span class="err">//</span> <span class="nc">get</span> <span class="n">query</span> <span class="n">parameter</span> <span class="n">as</span> <span class="kd">class</span> <span class="nc">ctx</span><span class="o">.</span><span class="na">queryParam</span><span class="o">(</span><span class="n">key</span><span class="o">,</span> <span class="kd">class</span><span class="err">,</span> <span class="nc">default</span><span class="o">)</span> <span class="c1">// get query parameter (or default value) as class</span> <span class="n">ctx</span><span class="o">.</span><span class="na">queryParams</span><span class="o">(</span><span class="n">key</span><span class="o">)</span> <span class="c1">// get query parameters (multiple)</span> <span class="n">ctx</span><span class="o">.</span><span class="na">queryParamMap</span><span class="o">()</span> <span class="c1">// get query parameter map</span> <span class="n">ctx</span><span class="o">.</span><span class="na">queryString</span><span class="o">()</span> <span class="c1">// get query string</span> <span class="n">ctx</span><span class="o">.</span><span class="na">scheme</span><span class="o">()</span> <span class="c1">// get request scheme</span> <span class="n">ctx</span><span class="o">.</span><span class="na">sessionAttribute</span><span class="o">(</span><span class="n">key</span><span class="o">,</span> <span class="n">value</span><span class="o">)</span> <span class="c1">// set session attribute (server side attribute)</span> <span class="n">ctx</span><span class="o">.</span><span class="na">sessionAttribute</span><span class="o">(</span><span class="n">key</span><span class="o">)</span> <span class="c1">// get session attribute</span> <span class="n">ctx</span><span class="o">.</span><span class="na">sessionAttributeMap</span><span class="o">()</span> <span class="c1">// get attribute map</span> <span class="n">ctx</span><span class="o">.</span><span class="na">url</span><span class="o">()</span> <span class="c1">// get request url</span> <span class="n">ctx</span><span class="o">.</span><span class="na">fullUrl</span><span class="o">()</span> <span class="c1">// get request url + query param</span> <span class="n">ctx</span><span class="o">.</span><span class="na">contextPath</span><span class="o">()</span> <span class="c1">// get request context path</span> <span class="n">ctx</span><span class="o">.</span><span class="na">userAgent</span><span class="o">()</span> <span class="c1">// get request user agent</span> <span class="c1">// Response methods</span> <span class="n">ctx</span><span class="o">.</span><span class="na">result</span><span class="o">(</span><span class="n">resultString</span><span class="o">)</span> <span class="c1">// set a string result that will be sent to the client</span> <span class="n">ctx</span><span class="o">.</span><span class="na">resultString</span><span class="o">()</span> <span class="c1">// get the string result that will be sent to the client</span> <span class="n">ctx</span><span class="o">.</span><span class="na">result</span><span class="o">(</span><span class="n">resultStream</span><span class="o">)</span> <span class="c1">// set a stream result that will be sent to the client</span> <span class="n">ctx</span><span class="o">.</span><span class="na">resultStream</span><span class="o">()</span> <span class="c1">// get the stream that will be sent to the client</span> <span class="n">ctx</span><span class="o">.</span><span class="na">result</span><span class="o">(</span><span class="n">future</span><span class="o">)</span> <span class="c1">// set a future result that will be sent to the client (async)</span> <span class="n">ctx</span><span class="o">.</span><span class="na">resultFuture</span><span class="o">()</span> <span class="c1">// get the future result that will be sent to the client</span> <span class="n">ctx</span><span class="o">.</span><span class="na">contentType</span><span class="o">(</span><span class="n">contentType</span><span class="o">)</span> <span class="c1">// set the response content type</span> <span class="n">ctx</span><span class="o">.</span><span class="na">header</span><span class="o">(</span><span class="n">name</span><span class="o">,</span> <span class="n">value</span><span class="o">)</span> <span class="c1">// set a response header</span> <span class="n">ctx</span><span class="o">.</span><span class="na">redirect</span><span class="o">(</span><span class="n">location</span><span class="o">)</span> <span class="c1">// send a redirect response to location</span> <span class="n">ctx</span><span class="o">.</span><span class="na">redirect</span><span class="o">(</span><span class="n">location</span><span class="o">,</span> <span class="n">httpStatusCode</span><span class="o">)</span> <span class="c1">// send a redirect response to location with status code</span> <span class="n">ctx</span><span class="o">.</span><span class="na">status</span><span class="o">(</span><span class="n">statusCode</span><span class="o">)</span> <span class="c1">// set response status</span> <span class="n">ctx</span><span class="o">.</span><span class="na">status</span><span class="o">()</span> <span class="c1">// get response status</span> <span class="n">ctx</span><span class="o">.</span><span class="na">cookie</span><span class="o">(</span><span class="n">name</span><span class="o">,</span> <span class="n">value</span><span class="o">)</span> <span class="c1">// set cookie by name and value</span> <span class="n">ctx</span><span class="o">.</span><span class="na">cookie</span><span class="o">(</span><span class="n">cookie</span><span class="o">)</span> <span class="c1">// set cookie</span> <span class="n">ctx</span><span class="o">.</span><span class="na">removeCookie</span><span class="o">(</span><span class="n">name</span><span class="o">,</span> <span class="n">path</span><span class="o">)</span> <span class="c1">// remove a cookie</span> <span class="n">ctx</span><span class="o">.</span><span class="na">html</span><span class="o">(</span><span class="n">html</span><span class="o">)</span> <span class="c1">// call result(string).contentType("text/html")</span> <span class="n">ctx</span><span class="o">.</span><span class="na">json</span><span class="o">(</span><span class="n">obj</span><span class="o">)</span> <span class="c1">// call result(JavalinJson.toJson(obj)).contentType("application/json")</span> <span class="n">ctx</span><span class="o">.</span><span class="na">json</span><span class="o">(</span><span class="n">future</span><span class="o">)</span> <span class="c1">// call result(future(JavalinJson.toJson(obj))).contentType("application/json")</span> <span class="n">ctx</span><span class="o">.</span><span class="na">render</span><span class="o">(</span><span class="n">filePath</span><span class="o">,</span> <span class="n">model</span><span class="o">)</span> <span class="c1">// call html(JavalinRenderer.render(filePath, model)</span> |
https://javalin.io/documentation
❤️ Curso Java Para Iniciantes
👍 Curso de Desenvolvimento Web Completo
#java #javalin
Deixe um comentário