JAX-RS @PathParam. Reading Path Parameters.

In this blog post I am going to share with you how to read the PathParam value which was included in the request URI.

And before you continue further I would like to mention that I have recorded a video lesson on how to use the @PathParam annotation and made it part of my video course: REST API with Java JAX-RS. Create and Deploy to Amazon Cloud.

Let’s assume an HTTP GET request was send to our RESTful Web Service endpoint to get a specific user profile. For our web service to be able to read from database a specific user profile, our script will need to know the unique identified of that user, so most linked the URI of our Web Service Endpoint that matches the get user profile functionality will look something like this:

/api/users/{profileId}

where the {profileId} will contain the actual user id identifier which can be used to retrieve the record from database. For example:

/api/users/r4ghtaf43c3n

To be able to read the value of {profileId} from request URI we will need to use the @PathParam annotation. Below is a little code example that demonstrates how to do it.

JAX-RS @PathParam example. Reading Single Path Parameter.

    @GET
    @Path("/{profileId}")
    @Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
    public UserProfile getProfile(@PathParam("profileId") String profileId)  {

     // We can how use the profileId value to retrieve the record
     UserService userService = new UserService()
     UserProfile userProfile = userService.getUserProfile(profileId);

     return userProfile;
 }

Reading Multiple @PathParam Values.

Often there are more than one @PathParam value that we need to read. For example, user application requested to return a specific message published by specific user. So the request URI will contain multiple path parameters:

/api/users/{profileId}/messages/{messageId}

where {profileId} will be replaced with with the user profile identifier and {messageId} will be replaced with message identifier. For example:

/api/users/r4ghtaf43c3n/messages/e1hfgryetfho

Code example:

   
    @GET
    @Path("/{profileId}/messages/{messageId}")
    @Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
     public Message getUserMessage(@PathParam("profileId") String profileId, @PathParam("messageId") String messageId)  {

     // We can how use the profileId value to retrieve the record
     UserService userService = new UserService()
     Message message = userService.getUserMessage(profileId, messageId);

     return message;
 }

Java Web Services. Video Course.

Learn how to design,create , consume and secure SOAP and REST web services from scratch in easy steps.

Java
icon

Java Web Services Part 2. Video Course.

Master advanced web services concepts and implement them in easy steps

Java Web Services Part 2
icon

REST Java Web Services. Video Course.

A guide to understanding, accessing, and writing a REST Java web service using Apache and Java EE.

Java Web Services Part 2
icon

Leave a Reply

Your email address will not be published. Required fields are marked *