Files
PixelDocs/assets/flash_core-concepts_request-handler.md.fjZWLpOw.js
2025-02-27 21:46:56 +00:00

13 lines
7.8 KiB
JavaScript
Raw Permalink Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import{_ as s,c as t,a0 as a,o as i}from"./chunks/framework.p2VkXzrt.js";const k=JSON.parse('{"title":"⚙️ Request Handler","description":"","frontmatter":{"banner_title":"Flash - Request Handler","banner_description":"Learn how to create and manage Request Handlers in Flash.","head":[["meta",{"name":"twitter:image","content":"/assets/banner-cards/flash-core-concepts-request-handler.png"}],["meta",{"name":"twitter:image:src","content":"https://docs.pixel-services.com/assets/banner-cards/flash-core-concepts-request-handler.png"}],["meta",{"name":"twitter:card","content":"summary_large_image"}],["meta",{"name":"twitter:image:height","content":"1280"}],["meta",{"name":"twitter:image:width","content":"669"}],["meta",{"name":"twitter:description","content":""}]]},"headers":[],"relativePath":"flash/core-concepts/request-handler.md","filePath":"flash/core-concepts/request-handler.md"}'),n={name:"flash/core-concepts/request-handler.md"};function d(r,e,h,l,o,c){return i(),t("div",null,e[0]||(e[0]=[a(`<h1 id="⚙️-request-handler" tabindex="-1">⚙️ Request Handler <a class="header-anchor" href="#⚙️-request-handler" aria-label="Permalink to &quot;⚙️ Request Handler&quot;"></a></h1><p>In this section, we illustrate the powerful concept of <code>RequestHandler</code> in Flash, which are used to handle incoming requests and generate responses. RequestHandler classes provide the most control over the request lifecycle and allow you to use routing, expected operators and HDI&#39;s to create custom logic for handling requests.</p><h2 id="creating-a-request-handler" tabindex="-1">Creating a Request Handler <a class="header-anchor" href="#creating-a-request-handler" aria-label="Permalink to &quot;Creating a Request Handler&quot;"></a></h2><p>To create a custom request handler, you need to extend the <code>RequestHandler</code> class and annotate the class with the <code>RouteInfo</code> annotation, specifying the HTTP method that the handler will respond to and the relative path that the handler will be registered to. After that, you must override the <code>handle</code> method; The <code>handle</code> method is where you define the logic for processing the request and generating the response. The <code>req</code> (request) and <code>res</code> (response) objects are available in the handler to access the request data and send the response back to the client.</p><p>You must call the super constructor with the <code>req</code> and <code>res</code> objects to initialize the handler.</p><div class="language-java vp-adaptive-theme"><button title="Copy Code" class="copy"></button><span class="lang">java</span><pre class="shiki shiki-themes github-light github-dark vp-code" tabindex="0"><code><span class="line highlighted"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">@</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">RouteInfo</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">endpoint</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&quot;/hello&quot;</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">, </span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">method</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> =</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> HttpMethod.GET)</span></span>
<span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">public</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> class</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> MyHandler</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> extends</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> RequestHandler</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> {</span></span>
<span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> public</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> MyHandler</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(Request </span><span style="--shiki-light:#E36209;--shiki-dark:#FFAB70;">req</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">, Response </span><span style="--shiki-light:#E36209;--shiki-dark:#FFAB70;">res</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">) {</span></span>
<span class="line highlighted"><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> super</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(req, res);</span></span>
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> }</span></span>
<span class="line"></span>
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> @</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">Override</span></span>
<span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> public</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> Object </span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">handle</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">() {</span></span>
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> String response </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> &quot;Hello, world!&quot;</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">;</span></span>
<span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> return</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> response;</span></span>
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> }</span></span>
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">}</span></span></code></pre></div><div class="warning custom-block"><p class="custom-block-title">WARNING</p><p>Any logic that needs to be executed before the request handler is registered must be done within the constructor.</p></div><h2 id="request-handler-methods" tabindex="-1">Request Handler methods <a class="header-anchor" href="#request-handler-methods" aria-label="Permalink to &quot;Request Handler methods&quot;"></a></h2><p>The <code>RequestHandler</code> class provides several methods that can be used to interact with the request and response objects easily and safely. Following are listed the methods available in the <code>RequestHandler</code> class, with a brief description of their purpose:</p><table tabindex="0"><thead><tr><th>Method</th><th>Params</th><th>Description</th></tr></thead><tbody><tr><td><code>getRequestBody()</code></td><td><code>none</code></td><td>Returns a <code>JSONObject</code> representation of the request body.</td></tr><tr><td><code>getSpecification()</code></td><td><code>none</code></td><td>Returns an instance of <code>HandlerSpecification</code> containing all sorts of information about the handler.</td></tr><tr><td><code>expectedRequestParameter()</code></td><td><code>String name, description</code></td><td>Returns an instance of <code>ExpectedRequestParameter</code> for the specified parameter name.</td></tr><tr><td><code>expectedBodyField()</code></td><td><code>String name, description</code></td><td>Returns an instance of <code>ExpectedBodyField</code> for the specified field name.</td></tr><tr><td><code>expectedBodyFile()</code></td><td><code>String name, description</code></td><td>Returns an instance of <code>ExpectedBodyFile</code> for the specified file name.</td></tr></tbody></table><p>(More on the <code>ExpectedRequestParameter</code>, <code>ExpectedBodyField</code>, and <code>ExpectedBodyFile</code> classes in the next section).</p>`,11)]))}const u=s(n,[["render",d]]);export{k as __pageData,u as default};