How do I do the equivalent of this @Path
param using query parameters?
@Path("/history/{startDate: [0-9][0-9][0-9][0-9]-[0-1][0-9]-[0-3][0-9]}/{endDate: [0-9][0-9][0-9][0-9]-[0-1][0-9]-[0-3][0-9]}")
Which would match this URI:
/history/1914-01-20/2014-01-20/
But now I need to match this:
/history/123456?end=2024-01-20&start=2014-11-12
This code works, but there is no validation against the start
param and end
param:
@Path("/history")
public Response getHistory( @QueryParam("start") String start,
@QueryParam("end") String end){
/*** foo ***/
}
Wich version of JAX-RS (or Jersey) are you using?
From version 2 you can use Bean Validation ( https://weblogs.java.net/blog/bhaktimehta/archive/2013/10/30/jax-rs-and-bean-validation )
Or a bit more complex in the case of your example, Entities Providers like
https://blogs.oracle.com/arungupta/entry/jax_rs_custom_entity_providers
But is for version JAX-RS 2.
Since I was told there is no "matching" for query parameters, I found two other ways to handle this:
Method #1: Using Regular Expressions
//in my includes
import java.util.regex.Matcher;
import java.util.regex.Pattern;
//In my @GET endpoint
Pattern startPattern = Pattern.compile("[0-9][0-9][0-9][0-9]-[0-1][0-9]-[0-3][0-9]");
Matcher startMatcher = startPattern.matcher(assigned);
if (startMatcher.find()) {
System.out.println("Matched!");
} else {
System.out.println("Did not match!");
}
Method #2: Require a certain data type in your implementation
I can't really codify this as it's specific to your implementation, but in my implementation, I required a TIMESTAMPTZ for my database row. Jax-RS then throws an error when the QueryParam
doesn't match during an SQL query.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.