0

I'm trying to use Jersey to develop a REST web service. My requirement is for me to be able to access the web service and return data, based on the passed PatParam parameters. My web service so far is as follows:

@GET
@Produces(MediaType.APPLICATION_JSON)
@Consumes({ "application/xml", "application/json", "application/x-www-form-urlencoded" })
@Path("/1.0/people{extension:(.json)*}/{personId:([^/]+?)?}{entityExtension:(.json)*}")
public String getLocation(@PathParam("extension") String extension, @PathParam("personId") String personId,@PathParam("entityExtension") String entityExtension)
{
    if ((personId==null ||personId.equals("")) && (extension == null || extension.equals("")))
        return "No Id, and no extension";
    else
        return "personId= "+personId + ", extension= " + extension+", entityExtension= "+entityExtension;
}

With the above code in mind, what I'm trying to achieve is the following:

http://localhost:8080/Jersey/RestService/1.0/people.json

Should return the list of all people in the "json" format (Hence the .json extension)

Now, I want to be able to get the information for a particular person by simply putting the person's ID, and return the person's information in json/xml based on the extension:

http://localhost:8080/Jersey/RestService/1.0/people/Mouhammed89.json

With the above URL, I should be returning the information for the personId: Mouhammed89, and returning the information in the json format.

I know that my problem is with the regular expressions that I'm using, so I would really appreciate the help in creating them.

Mouhammed Soueidane
  • 1,056
  • 2
  • 24
  • 42

1 Answers1

1

IMHO container (List) and item (Person) URL should be handled separately. And you don't require explicit .json prefix for returning JSON response, only the annotation at the API level should suffice.

@GET
@Produces(MediaType.APPLICATION_JSON)
@Path("/1.0/people") //no need for .json suffix as API only return JSON format
public List<String> allLocations(..){ //jackson etc. will convert return type to json string 
..
}

@GET
@Produces(MediaType.APPLICATION_JSON)
@Path("/1.0/people/{personId: \\d+}")//digits only
public String getLocation(..){ //jackson etc. will convert return type to json string 
..
}
harsh
  • 7,502
  • 3
  • 31
  • 32
  • Thanks Harsh. The reason why I'm relying on the PathParameter to decide which format to return is that I'm expecting some clients to send me the request without sending me an "Accept" in the header, in which case I do not know which format to return to them. This is why we have decided to include the extension in the URL. – Mouhammed Soueidane Nov 18 '13 at 11:30
  • 2
    Ok, I think this should help you on mapping extensions to `media-type` http://zcox.wordpress.com/2009/08/11/uri-extensions-in-jersey/ – harsh Nov 18 '13 at 12:02
  • Thanks Harsh, that was brilliant. I ended up doing two different web service methods: One to get the list of all people, and another one to get me the information for a specific person. I have also implemented the custom PackagesResourceConfig which has helped me pass the extension I want in the URL itself, and Jersey ends up doing the XML/JSON mapping for me =) – Mouhammed Soueidane Nov 19 '13 at 14:01