Hi,
I have an issue with the path template for the built in handler
"Handlers.routing", when execute this implementation:
public class UndertowOnePathVariableApplication {
private static final Logger LOGGER =
LoggerFactory.getLogger(UndertowOnePathVariableApplication.class);
public static void main(final String[] args) {
Undertow.builder()
.addHttpListener(8080, "localhost")
.setHandler(
Handlers.routing()
.get("/api/v1/orders/", (exchange) -> {
LOGGER.info("The path template defined is
'/api/v1/orders/'");
LOGGER.info("The request URI '{}' and URL '{}'",
exchange.getRequestURI(), exchange.getRequestURL());
exchange.getResponseHeaders().put(Headers.CONTENT_TYPE,
"text/plain");
exchange.setStatusCode(StatusCodes.OK)
.getResponseSender().send("All Orders");
})
.get("/api/v1/orders/{orderId}", (exchange) -> {
LOGGER.info("The path template defined is
'/api/v1/orders/{orderId}'");
LOGGER.info("The request URI '{}' and URL '{}'",
exchange.getRequestURI(), exchange.getRequestURL());
exchange.getResponseHeaders().put(Headers.CONTENT_TYPE,
"text/plain");
exchange.setStatusCode(StatusCodes.OK)
.getResponseSender().send(
"Order '" +
exchange.getQueryParameters().get("orderId").getLast() + "'");
}))
.build()
.start();
}
}
I get this responses:
boggard@xiuhcoatl:~$ curl -v 'http://localhost:8080/api/v1/orders'
* Trying 127.0.0.1...
* TCP_NODELAY set
* Connected to localhost (127.0.0.1) port 8080 (#0)
GET /api/v1/orders HTTP/1.1
Host: localhost:8080
User-Agent: curl/7.56.0
Accept: */*
< HTTP/1.1 404 Not Found
< Connection: keep-alive
< Content-Length: 0
< Date: Sun, 21 Jan 2018 02:01:53 GMT
<
* Connection #0 to host localhost left intact
boggard@xiuhcoatl:~$ curl -v 'http://localhost:8080/api/v1/orders/'
* Trying 127.0.0.1...
* TCP_NODELAY set
* Connected to localhost (127.0.0.1) port 8080 (#0)
GET /api/v1/orders/ HTTP/1.1
Host: localhost:8080
User-Agent: curl/7.56.0
Accept: */*
< HTTP/1.1 200 OK
< Connection: keep-alive
< Content-Type: text/plain
< Content-Length: 10
< Date: Sun, 21 Jan 2018 02:02:45 GMT
<
* Connection #0 to host localhost left intact
All Orders
boggard@xiuhcoatl:~$ curl -v 'http://localhost:8080/api/v1/orders/12345'
* Trying 127.0.0.1...
* TCP_NODELAY set
* Connected to localhost (127.0.0.1) port 8080 (#0)
GET /api/v1/orders/12345 HTTP/1.1
Host: localhost:8080
User-Agent: curl/7.56.0
Accept: */*
< HTTP/1.1 200 OK
< Connection: keep-alive
< Content-Type: text/plain
< Content-Length: 13
< Date: Sun, 21 Jan 2018 02:03:56 GMT
<
* Connection #0 to host localhost left intact
Order '12345'
That I thing is correct because I just define two paths: '.../orders/' and
'.../orders/{orderId}'. And the path '.../orders', that is not defined,
respond 404. But when use this implementation:
public class UndertowTwoPathVariablesApplication {
private static final Logger LOGGER =
LoggerFactory.getLogger(UndertowTwoPathVariablesApplication.class);
public static void main(final String[] args) {
Undertow.builder()
.addHttpListener(8080, "localhost")
.setHandler(
Handlers.routing()
.get("/api/v1/orders/{orderId}/items/", (exchange) -> {
LOGGER.info("The path template defined is
'/api/v1/orders/{orderId}/items/'");
LOGGER.info("The request URI '{}' and URL '{}'",
exchange.getRequestURI(), exchange.getRequestURL());
exchange.getResponseHeaders().put(Headers.CONTENT_TYPE,
"text/plain");
exchange.setStatusCode(StatusCodes.OK)
.getResponseSender().send(
"Order '" +
exchange.getQueryParameters().get("orderId").getLast() + "'");
})
.get("/api/v1/orders/{orderId}/items/{itemId}", (exchange) -> {
LOGGER.info("The path template defined is
'/api/v1/orders/{orderId}/items/{itemId}'");
LOGGER.info("The request URI '{}' and URL '{}'",
exchange.getRequestURI(), exchange.getRequestURL());
exchange.getResponseHeaders().put(Headers.CONTENT_TYPE,
"text/plain");
exchange.setStatusCode(StatusCodes.OK)
.getResponseSender().send(
"Order '" +
exchange.getQueryParameters().get("orderId").getLast() +
"' Item '" +
exchange.getQueryParameters().get("itemId").getLast() + "'");
}))
.build()
.start();
}
}
I get this responses:
boggard@xiuhcoatl:~$ curl -v 'http://localhost:8080/api/v1/orders/12345/items'
* Trying 127.0.0.1...
* TCP_NODELAY set
* Connected to localhost (127.0.0.1) port 8080 (#0)
GET /api/v1/orders/12345/items HTTP/1.1
Host: localhost:8080
User-Agent: curl/7.56.0
Accept: */*
< HTTP/1.1 200 OK
< Connection: keep-alive
< Content-Type: text/plain
< Content-Length: 13
< Date: Sun, 21 Jan 2018 02:08:45 GMT
<
* Connection #0 to host localhost left intact
Order '12345'
boggard@xiuhcoatl:~$ curl -v 'http://localhost:8080/api/v1/orders/12345/items/'
* Trying 127.0.0.1...
* TCP_NODELAY set
* Connected to localhost (127.0.0.1) port 8080 (#0)
GET /api/v1/orders/12345/items/ HTTP/1.1
Host: localhost:8080
User-Agent: curl/7.56.0
Accept: */*
< HTTP/1.1 200 OK
< Connection: keep-alive
< Content-Type: text/plain
< Content-Length: 21
< Date: Sun, 21 Jan 2018 02:09:11 GMT
<
* Connection #0 to host localhost left intact
Order '12345' Item ''
boggard@xiuhcoatl:~$ curl -v
'http://localhost:8080/api/v1/orders/12345/items/09876'
* Trying 127.0.0.1...
* TCP_NODELAY set
* Connected to localhost (127.0.0.1) port 8080 (#0)
GET /api/v1/orders/12345/items/09876 HTTP/1.1
Host: localhost:8080
User-Agent: curl/7.56.0
Accept: */*
< HTTP/1.1 200 OK
< Connection: keep-alive
< Content-Type: text/plain
< Content-Length: 26
< Date: Sun, 21 Jan 2018 02:09:30 GMT
<
* Connection #0 to host localhost left intact
Order '12345' Item '09876'
That I thing is incorrect because there are just two paths again:
'.../orders/{orderId}/items/' and '.../orders/{orderId}/items/{itemId}'
but the curl with the exact URI use the second path template. Is this an issue or what I
am doing wrong?
Best Regards
Fernando Cruz