File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ package com .zetcode ;
2+
3+ import io .undertow .Handlers ;
4+ import io .undertow .Undertow ;
5+ import io .undertow .server .HttpHandler ;
6+ import io .undertow .server .HttpServerExchange ;
7+ import io .undertow .util .Headers ;
8+ import io .undertow .util .PathTemplateMatch ;
9+
10+ class ItemHandler implements HttpHandler {
11+
12+ @ Override
13+ public void handleRequest (HttpServerExchange exchange ) {
14+ exchange .getResponseHeaders ().put (Headers .CONTENT_TYPE , "text/plain" );
15+
16+ // Method 1
17+ // PathTemplateMatch pathMatch = exchange.getAttachment(PathTemplateMatch.ATTACHMENT_KEY);
18+ // String itemId = pathMatch.getParameters().get("itemId");
19+
20+ // Method 2
21+ String itemId = exchange .getQueryParameters ().get ("itemId" ).getFirst ();
22+
23+ var msg = String .format ("Received Item Id %s" , itemId );
24+
25+ exchange .getResponseSender ().send (msg );
26+ }
27+ }
28+
29+ public class UndertowPathParam {
30+
31+ public static void main (String [] args ) {
32+
33+ Undertow server = Undertow .builder ()
34+ .addHttpListener (8080 , "0.0.0.0" )
35+ .setHandler (Handlers .pathTemplate ()
36+ .add ("/item/{itemId}" , new ItemHandler ())
37+ )
38+ .build ();
39+ server .start ();
40+ }
41+ }
You can’t perform that action at this time.
0 commit comments