JAVA EXAMPLE PROGRAMS

JAVA EXAMPLE PROGRAMS

Publish Your Article Here

JAX-RS @Path annotation example


This page gives you an example using JAX-RS @Path annotation, how to bind a URI pattern to your java method.

In the previous examples we have given details of application setup, dependencies, web.xml file configurations: If you want to know about these configuration, please refer these:

Restful web services using RESTEasy hello world example.
Restful web services using Jersey hello world example.

Simple URI matching:

Look at the below class to apply @Path annotation to your method:

package com.java2novice.restful;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.core.Response;

@Path("/message")
public class MessageReceiveService {

	@GET
	public Response pingMe(){
	
		String defaultResp = "Hi... How are you?";
		return Response.status(200).entity(defaultResp).build();
	}
	
	@GET
	@Path("/birthday")
	public Response printBdayMessage(){
		
		String bDayMessage = "Happy Birthday";
		return Response.status(200).entity(bDayMessage).build();
	}
}

In the above example, if you use "/message" URI pattern, pingMe() method will be invoked, and you will get "i... How are you?" as a response.

If you use "/message/birthday" URI pattern, then printBdayMessage() method will be invoked and you will get "Happy Birthday" as a response.

Dynamic URI with parameter matching:

In your @Path annotation URI value, anything between "{" and "}" braces can be dynamic. This value can be mapped to method input variable using @PathParam annotation. Look at below example:

package com.java2novice.restful;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.core.Response;

@Path("/message")
public class MessageReceiveService {

	@GET
	@Path("{custMessage}")
	public Response printCustomMessage(@PathParam("custMessage") String message){
		
		return Response.status(200).entity(message).build();
	}
}

In the above example, if you use "/message/java2novice" as an URI, then you will get response as "java2novice". If you use "/message/do-u-like-it" as an URI, then you will get response as "do-u-like-it".

In the next page you will see an example on how to use @Path annotation with regular expression matching.

<< Previous Program | Next Program >>

Restful Web Services Examples

  1. Restful web services using RESTEasy hello world example.
  2. Restful web services using Jersey hello world example.
  3. JAX-RS @Path annotation example
  4. JAX-RS @Path annotation with regular expression match example
  5. JAX-RS @PathParam annotation example
  6. JAX-RS @QueryParam and @DefaultValue annotations example
  7. JAX-RS @MatrixParam annotation example
  8. How to read multiple values of a query paramter in JAX-RS restful web services?
  9. How to pass header parameters as method inputs in JAX-RS restful web services?
  10. How to read header parameters in JAX-RS restful web services?
  11. JAX-RS @FormParam annotation example
  12. How to upload file using Jersey restful web services?
  13. How to download file using java restful web services?
  14. XML based Restful web service with RESTEasy and JAXB.
  15. XML based Restful web service with Jersey and JAXB.
  16. Json based Restful web service with RESTEasy, Jettison and JAXB
  17. Json based Restful web service with RESTEasy and Jackson
  18. Json based Restful web service with Jersey and Jackson
  19. How to input json request with Jersey and Jackson?
  20. Java client for restful web service using java.net package
  21. Java client for restful web service using Jersey API
  22. Java restful webservices with HTTP basic authentication.
Knowledge Centre
Where can we use serialization?
Whenever an object has to sent over the network, those objects should be serialized. Also if the state of an object is to be saved, objects need to be serilazed.
Famous Quotations
The very best thing you can do for the whole world is to make the most of yourself.
-- Wallace Wattles

About Author

I'm Nataraja Gootooru, programmer by profession and passionate about technologies. All examples given here are as simple as possible to help beginners. The source code is compiled and tested in my dev environment.

If you come across any mistakes or bugs, please email me to [email protected].

Most Visited Pages

Other Interesting Sites

Reference: Java™ Platform Standard Ed. 7 - API Specification | Java™ Platform Standard Ed. 8 - API Specification | Java is registered trademark of Oracle.
Privacy Policy | Copyright © 2022 by Nataraja Gootooru. All Rights Reserved.