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
Newsletter
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Should Developers Switch From SQLite to DuckDB?
  • Adding Graph Retrieval to a RAG Pipeline With Python
  • PostgreSQL vs Oracle: When Higher Cost Means Better Performance
  • Catching Query Regressions Before Your Users Do

Trending

  • Idempotency Under Concurrency: Why Your Application Check Isn't the Last Line of Defense
  • Build Software Faster With Three Simple Principles
  • Mistaking Code Production for Engineering Progress: AI Productivity Myths Part 1
  • Detection and Response Did Its Job. Now Someone Has to Actually Fix It.

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

  1. DZone
  2. Data Engineering
  3. Databases
  4. Validating JAX-RS Query Parameters

Validating JAX-RS Query Parameters

This quick and simple guide will walk you through the steps to validate JAX-RS query parameters using filters, including their creation and enforcement.

By 
Abhishek Gupta
DZone Core CORE ·
Jun. 08, 17 · Tutorial
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
24.9K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

It’s easy to validate parameters in JAX-RS using filters – ContainerRequestFilter to be specific. There are other options at your disposal, e.g. using (CDI or EJB) interceptors, or injecting (HttpServletRequest using @Context)

Scenario: Validate Query Parameter Passed in by the Caller

Steps

  • Implement filter
  • Extracts query parameter from ContainerRequestContext
  • Performs the validation – aborts the request with an appropriate response status (and the error message)
@Provider
@QueryParamValidator
public class JAXRSReqFilter implements ContainerRequestFilter {

    @Override
    public void filter(ContainerRequestContext requestContext) throws IOException {

        MultivaluedMap < String, String > queryParameters = requestContext.getUriInfo().getQueryParameters();

        String queryParam = queryParameters.keySet().stream().findFirst().get();
        System.out.println("Query param - " + queryParam);

        if (!queryParam.equals("p")) {
            requestContext.abortWith(Response
                .status(Response.Status.BAD_REQUEST)
                .entity("Invalid Query Param " + queryParam)
                .build());
        }
    }
}


Enforce Filter

  • Use @NameBinding to decorate custom annotation
  • Use the custom annotation on the JAX-RS method
@NameBinding
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE, ElementType.METHOD})
public @interface QueryParamValidator {
    
}


@Path("test")
public class TestResource {
    
    @GET
    @QueryParamValidator
    public String test(@QueryParam("p") String p){
        return "Param "+ p + " on " + new Date();
    }
}


Further Reading

  • My eBook: REST assured with JAX-RS
  • This JAX-RS 2.0 article in the Java Magazine

Cheers!

Database

Published at DZone with permission of Abhishek Gupta. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Should Developers Switch From SQLite to DuckDB?
  • Adding Graph Retrieval to a RAG Pipeline With Python
  • PostgreSQL vs Oracle: When Higher Cost Means Better Performance
  • Catching Query Regressions Before Your Users Do

Partner Resources

×

Comments

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

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 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook