📘 Premium Read: Access my best content on Medium member-only articles — deep dives into Java, Spring Boot, Microservices, backend architecture, interview preparation, career advice, and industry-standard best practices.
✅ Some premium posts are free to read — no account needed. Follow me on Medium to stay updated and support my writing.
🎓 Top 10 Udemy Courses (Huge Discount): Explore My Udemy Courses — Learn through real-time, project-based development.
▶️ Subscribe to My YouTube Channel (172K+ subscribers): Java Guides on YouTube
Video
What is JAX-RS?
What is RESTEasy?
Tools and Technologies used
- JDK 1.8 or later
- Maven 3.5+
- Eclipse IDE
- JAX-RS 2.0 +
- RESTEasy - 3.9.3.Final
- Tomcat 8.5+
Development Steps
- Create a Maven Web project in Eclipse IDE
- Add maven dependencies
- Project Structure
- Create a HelloWorld model class
- Create HelloWorldResource class
- Create Application Class
- RESTEasy Client for REST API
1. Create a Maven Web project in Eclipse IDE
https://www.javaguides.net/2018/11/how-to-create-web-project-using-maven-in-eclipse.html
2. Add maven dependencies
<project
xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>net.javaguides</groupId>
<artifactId>resteasy-hello-world-example</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>resteasy-hello-world-example Maven Webapp</name>
<url>http://maven.apache.org</url>
<properties>
<resteasy.version>3.9.3.Final</resteasy.version>
</properties>
<dependencies>
<!-- Set up RESTEasy -->
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-jaxrs</artifactId>
<version>${resteasy.version}</version>
</dependency>
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-servlet-initializer</artifactId>
<version>${resteasy.version}</version>
</dependency>
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-jackson-provider</artifactId>
<version>${resteasy.version}</version>
</dependency>
<!-- RESTEasy Client Dependency -->
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-client</artifactId>
<version>${resteasy.version}</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<finalName>resteasy-hello-world-example</finalName>
</build>
</project>
3. Project Structure
Refer below screenshot for project structure and packaging structure:4. Create a HelloWorld model class
package net.javaguides.resteasy.model;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement
public class HelloWorld {
private String message;
public HelloWorld() {}
public HelloWorld(String message) {
super();
this.message = message;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}
4. Create HelloWorldResource class
package net.javaguides.resteasy.resource;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import net.javaguides.resteasy.model.HelloWorld;
/**
* Hello World rest api using resteasy
*
* @author Ramesh Fadatare
*
*/
@Path("hello")
public class HelloWorldResource {
@GET
@Produces(MediaType.APPLICATION_JSON)
public Response helloWorld() {
HelloWorld helloWorld = new HelloWorld("Hello World !");
return Response.ok(helloWorld).build();
}
}
- The @Path annotation specifies the URL to which the resource responds.
- The @GET annotation indicates that the annotated method responds to HTTP GET requests.
- With the @Produces annotation, we define that the method produces JSON.
6. Create Application Class
package net.javaguides.resteasy.resource;
import java.util.HashSet;
import java.util.Set;
import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;
@ApplicationPath("/restapi")
public class RestEasyServices extends Application {
private Set < Object > singletons = new HashSet < Object > ();
public RestEasyServices() {
singletons.add(new HelloWorldResource());
}
@Override
public Set < Object > getSingletons() {
return singletons;
}
}
7. RESTEasy Client for REST API
package net.javaguides.resteasy;
import org.jboss.resteasy.client.jaxrs.ResteasyClient;
import org.jboss.resteasy.client.jaxrs.ResteasyClientBuilder;
import org.jboss.resteasy.client.jaxrs.ResteasyWebTarget;
import org.junit.Test;
public class HelloWorldResourceTest {
private static final String FULL_PATH = "http://localhost:8080/resteasy-hello-world-example/restapi/hello";
@Test
public void testHelloWorld() {
final ResteasyClient client = new ResteasyClientBuilder().build();
final ResteasyWebTarget target = client.target(FULL_PATH);
String response = target.request().get(String.class);
System.out.println(response);
}
}
Comments
Post a Comment
Leave Comment