users@jersey.java.net

Re: [Jersey] mapping dynamic paths in Jersey

From: Martin Matula <martin.matula_at_oracle.com>
Date: Wed, 27 Oct 2010 15:00:53 +0200

Hi,
You can do something like this:

   @Path("{paths: .+"}")
   @GET
   public ... get(@PathParam("paths") List<PathSegment>) {
   }

Or:

   @Path("{a}/{b}/{paths: .+"}")
   @GET
   public ... get(@PathParam("paths") List<PathSegment>) {
   }

Or you can use sub-resource locators. E.g. this is a simplified
example of how you could expose a directory structure:

import com.sun.jersey.api.NotFoundException;
import java.io.File;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

@Path("/files")
public class FileResource {
     private File file = null;

     public FileResource() {
         this(new File("."));
     }

     public FileResource(File file) {
         this.file = file;
     }

     /** Sub-resource locator */
     @Path("{fileName}")
     public FileResource getDir(@PathParam("fileName") String file) {
         File child = new File(this.file, file);
         if (!child.exists()) {
             throw new NotFoundException();
         }
         return new FileResource(child);
     }

     @GET
     @Produces(MediaType.TEXT_PLAIN)
     public String get() {
         StringBuilder sb = new StringBuilder();
         if (file.isDirectory()) {
             sb.append("Directory: ");
         } else {
             sb.append("File: ");
         }
         sb.append(file.getAbsolutePath()).append("\n\n");
         for (String f : file.list()) {
             sb.append(f).append("\n");
         }
         return sb.toString();
     }
}

Regards,
Martin

On Oct 27, 2010, at 1:48 PM, mxn wrote:

>
> Hi,
>
> How can I map a dynamic path in Jersey like:
> /tree/parent1/parent2/parent3/node1 where all these are node ids? I
> need to
> provide the client with data about a node at a specific path in the
> tree.
>
> Thanks.
> --
> View this message in context: http://jersey.576304.n2.nabble.com/mapping-dynamic-paths-in-Jersey-tp5678235p5678235.html
> Sent from the Jersey mailing list archive at Nabble.com.
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
> For additional commands, e-mail: users-help_at_jersey.dev.java.net
>