Wednesday 30 November 2022

Using Jersey in a Spring boot application

In this post, I am going to explain how to integrate spring and Jersy in a rest application.

 

Jersey implements JAX-RS specification. JAX-RS document the Java APIs for restful web services.

 

Follow below step-by-step procedure to build the working application.

 

Step 1: Create new maven project ‘spring-jersey-hello-world’.

 

Step 2: Update pom.xml with maven dependencies.

 

pom.xml

<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>com.sample.app</groupId>
	<artifactId>spring-jersey-hello-world</artifactId>
	<version>1.0.0</version>

	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.7.5</version>
	</parent>

	<properties>
		<swagger.version>2.1.6</swagger.version>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<!-- <version.java>17</version.java> -->
	</properties>

	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-jersey</artifactId>
		</dependency>

	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>
</project>

 

Step 3: Define HelloEndpoint interface.

 

HelloEndpoint.java
package com.sample.app.rs;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

@Path("/hello")
public interface HelloEndpoint {

	@GET
	@Produces(MediaType.TEXT_PLAIN)
	public String sayHello();
}

 

Step 4: Define HelloEndpointImpl class.

 

HelloEndpointImpl.java

package com.sample.app.rs.impl;

import org.springframework.stereotype.Service;

import com.sample.app.rs.HelloEndpoint;

@Service
public class HelloEndpointImpl implements HelloEndpoint{

	public String sayHello() {
		return "Hello World";
	}
}

 

Step 5: Define JerseyConfig class.

 

JerseyConfig.java
package com.sample.app.config;

import org.glassfish.jersey.server.ResourceConfig;
import org.springframework.context.annotation.Configuration;

import com.sample.app.rs.impl.HelloEndpointImpl;

@Configuration
public class JerseyConfig extends ResourceConfig {
	public JerseyConfig() {
		register(HelloEndpointImpl.class);
	}
}

 

Step 6: Define main application class.

 

App.java
package com.sample.app;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class App {

    public static void main(String[] args) {
        SpringApplication.run(App.class, args);
    }
}

Total project structure looks like below.


 

Generate the artifact

Navigate to the folder where pom.xml file is located and execute the command ‘mvn package’. Upon command execution, you can see the ‘spring-jersey-hello-world-1.0.0.jar’ file in target folder.

$ ls ./target/
classes
generated-sources
generated-test-sources
maven-archiver
maven-status
spring-jersey-hello-world-1.0.0.jar
spring-jersey-hello-world-1.0.0.jar.original
test-classes

Execute below command to run the application.

java -jar ./target/spring-jersey-hello-world-1.0.0.jar

Open the url ‘http://localhost:8080/hello’ in browser, you will see below kind of screen.

 


You can download this application from this link.

 

Previous                                                 Next                                                 Home

1 comment: