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
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Building a High-Throughput Distributed Sequence Generator Using the Hi-Lo Algorithm
  • When Snowflake Lies to You: Understanding False Failures in dbt Pipelines
  • Master-Class: Understanding Database Replication (Single, Multi, and Leaderless)
  • Liquibase: Database Change Management and Automated Deployments

Trending

  • Prompt Injection Is Real, So I Built a Python Firewall for LLM Pipelines
  • Advanced Error Handling and Retry Patterns in Enterprise REST Integrations
  • Good Data, Bad Metric: A Mutation Testing Pattern for Analytics Engineering
  • Liquid Glass, Material 3, and a Lot of Plumbing
  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 user avatar
Abhishek Gupta
DZone Core CORE ·
Jun. 08, 17 · Tutorial
Likes (3)
Comment
Save
Tweet
Share
24.7K Views

Join the DZone community and get the full member experience.

Join For Free

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

  • Building a High-Throughput Distributed Sequence Generator Using the Hi-Lo Algorithm
  • When Snowflake Lies to You: Understanding False Failures in dbt Pipelines
  • Master-Class: Understanding Database Replication (Single, Multi, and Leaderless)
  • Liquibase: Database Change Management and Automated Deployments

Partner Resources

×

Comments

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

  • 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