Jersey sandbox match path or empty lines

I have a problem with Jersey @Path where I want to implement a sandbox environment for my system. Basically disable or enable sandbox mode at this URL, which might look like this:

Sandbox

GET: ../ MyProject / sandbox / data

official site

GET: ../ MyProject / data p>

However, I want to go where to use the regular expression for Path associated with my project root class.

@Path("/{mode:sandbox|}") public class JerseyResource{ boolean isSandbox = false; public JerseyResource(@PathParam("mode") String mode) { if(mode.equals("sandbox")) isSandbox = true; } @GET @Path("data") @Produces(MediaType.TEXT_PLAIN) public Response data() { if(isSandbox) return Response.ok("Sandbox is on").build(); return Response.ok("Sandbox is off").build(); } } 

It works great to try "GET: ../ MyProject / sandbox / data" and it returns "Sandbox enabled." But when I do "GET: ../ MyProject / data", it just returns me a 404 page that was not found.

Is there a way to use an empty string for the path URL as an argument in Jersey, as well as a fixed string?

+4
source share
2 answers

I found a solution by editing the web.xml file!

 <servlet-mapping> <servlet-name>myProject</servlet-name> <url-pattern>/sandbox/*</url-pattern> <url-pattern>/*</url-pattern> </servlet-mapping> 

This allows me to have multiple URLs pointing to the same project and write in code:

 @Path("") public class JerseyResource{ boolean isSandbox = false; public JerseyResource(@Context HttpServletRequest req) { if(req.getRequestURI().startsWith("/nexus/sandbox")) isSandbox = true; } 

This works fine for me, and it allows you to have an empty Path class, which also allows subparty.

0
source

Try using:

 @Path("{mode:(/sandbox)?}") 

You also need to change the mode comparison to:

 if (mode.endsWith("sandbox")) isSandbox = true; 

and your data resource:

 @Path("/data") 
0
source

Source: https://habr.com/ru/post/1435502/


All Articles