DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Because the DevOps movement has redefined engineering responsibilities, SREs now have to become stewards of observability strategy.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Related

  • Resolving Parameter Sensitivity With Parameter Sensitive Plan Optimization in SQL Server 2022
  • Comparing Managed Postgres Options on The Azure Marketplace
  • Useful System Table Queries in Relational Databases
  • Introducing Graph Concepts in Java With Eclipse JNoSQL

Trending

  • Code Reviews: Building an AI-Powered GitHub Integration
  • Apple and Anthropic Partner on AI-Powered Vibe-Coding Tool – Public Release TBD
  • The End of “Good Enough Agile”
  • Agile’s Quarter-Century Crisis
  1. DZone
  2. Data Engineering
  3. Databases
  4. Query Parameters and Path Parameters in Undertow

Query Parameters and Path Parameters in Undertow

Take a look at how to use query and path parameters in Undertow. In addition to some examples, there are some best practices to keep in mind as well.

By 
Bill O'Neil user avatar
Bill O'Neil
·
Jun. 09, 17 · Tutorial
Likes (5)
Comment
Save
Tweet
Share
16.1K Views

Join the DZone community and get the full member experience.

Join For Free

Query parameters and path parameters are fairly straightforward in Undertow. There is some slight boilerplate because, technically, query and path params can have multiple values. We are using some helper functions (PathParameters.java and QueryParameters.java).

Path Parameter and Query Parameter RoutingHandler

Fairly standard path param syntax. Undertow uses {param}, though you may be used to :param from other frameworks.

private static final HttpHandler ROUTES = new RoutingHandler()
    .get("/hello", ParametersServer::queryParam)
    .get("/hello/{name}/{num}", ParametersServer::pathParam)
    .get("/users/{userId}", ParametersServer::obfuscatedIdRoute)
;


View on GitHub

Path Parameter and Query Parameter HttpHandlers

These should be straightforward. Once again, we are using the following helpers — PathParameters.java and QueryParameters.java

private static void queryParam(HttpServerExchange exchange) {
    String name = Exchange.queryParams().queryParam(exchange, "name").orElse("world");
    int num = Exchange.queryParams().queryParamAsInteger(exchange, "num").orElse(1);
    Exchange.body().sendText(exchange, "Hello " + name + Strings.repeat("!", num));
}

private static void pathParam(HttpServerExchange exchange) {
    String name = Exchange.pathParams().pathParam(exchange, "name").orElse("world");
    int num = Exchange.pathParams().pathParamAsInteger(exchange, "num").orElse(1);
    Exchange.body().sendText(exchange, "Hello " + name + Strings.repeat("!", num));
}


View on GitHub

Server

public static void main(String[] args) {
    // Just some examples for obfuscated parameters.
    LongStream.range(100_000_000, 100_000_003).forEach( id -> {
       log.debug("id: " + id + " hashed: " + HashIds.encode(id));
    });
    SimpleServer server = SimpleServer.simpleServer(ROUTES);
    server.start();
}


View on GitHub

Output

curl localhost:8080/hello
Hello world!
curl localhost:8080/hello?name=bill
Hello bill!
curl 'localhost:8080/hello?name=bill&num=5'
Hello bill!!!!!
curl 'localhost:8080/hello/bill/5'
Hello bill!!!!!


Best Practices

Although it's not very hard to call params in this way, we recommend extracting the logic out even further. Take a look at Request Utilities. Here, you define the string literals and defaults once and only once. Everywhere else in the code that references this param is called just like UserRequests.email(exchange). This way, if you change the param name or even the type of param, it updates all of your code at once. Sharing defaults is also incredibly useful. Another feature of this is that you can cache values on the HttpServerExchange so you can use expensive calls in multiple routes with the cached values.

Obfuscating Sequential Ids

Sometimes you may want to obfuscate sequential ids to reduce the possibility of giving away some information. A quick example can be found at Obfuscating and Shortening Sequential ids with HashIds.

Database

Published at DZone with permission of Bill O'Neil. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Resolving Parameter Sensitivity With Parameter Sensitive Plan Optimization in SQL Server 2022
  • Comparing Managed Postgres Options on The Azure Marketplace
  • Useful System Table Queries in Relational Databases
  • Introducing Graph Concepts in Java With Eclipse JNoSQL

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!