Spring Boot: Setup Jersey Configuration and Resource/Endpoint

How-to: Spring Boot – Setup Jersey Configuration and Resource/Endpoint

These instructions assume you have a base Spring Boot project setup that runs the web server correctly. If you do not, then you can follow the instructions in a previous blog post to see how to do that.

Setting up a base project with JAX-RS and Spring Boot in IntelliJ

When you setup your Spring Boot application, whatever package your main class is created in, its sub packages will automatically be scanned for annotations that signify Spring Beans. So if my main class is in the package “com.familyassist” then all classes and sub packages in that package will be scanned on startup.

Let’s first setup your Jersey Resource Config. Create the following class in a sub package of your Spring Boot application. This class will load on startup to tell Jersey what packages to scan for REST endpoints. In the code, specify with a call to packages() the package name of where you will create your endpoints. I used “com.familyassist.messaging.resources”.

JerseyResourceConfig.java
package com.familyassist.config;

import org.glassfish.jersey.server.ResourceConfig;
import org.springframework.stereotype.Component;

@Component
public class JerseyResourceConfig extends ResourceConfig {

    public JerseyResourceConfig() {
        /* Set package to scan for application Resources/Endpoints */
        packages( "com.familyassist.messaging.resources;");
    }
}

Next, create your first endpoint class.

MessagingResource.java
package com.familyassist.messaging.resource;

import org.springframework.stereotype.Component;
import javax.ws.rs.GET;
import javax.ws.rs.Path;

@Component
@Path("/messages")
public class MessagingResource {
    @GET
    public String retrieveMessages(){
        return "fake message here";
    }
}

This is all that is needed to publish your first endpoint. Let’s test it out. Once you have saved it and restarted, test out the endpoint by hitting the URL in your browser. http://localhost:8080/messages

Congrats! Your Spring Boot application is now ready to serve REST resources!

Next : Setup Swagger to document your endpoints

Leave a Reply