Skip to content
Advertisement

Jax-rs annotation Path is not work in java spring boot?

I have spring boot application with starter version 2.1.16, and spring-boot-starter-web dependency. So, i want use javax.ws.rs-api library, and add dependency:

 <dependency>
     <groupId>javax.ws.rs</groupId>
     <artifactId>javax.ws.rs-api</artifactId>
     <version>2.1.1</version>
 </dependency>

So when i create controller, and add @Path, @Get – i don’t get answer from the server (404 not found). How makes it work?

package com.example.test;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.ext.Provider;

@RestController
public class MyController {

    //Doesn't work (404 not found
    @GET
    @Path("/my_test")
    public String check() {
        return "hi!";
    }

    //Work
    @RequestMapping("/my_test2")
    public String check2() {
        return "hi2!";
    }
}

Advertisement

Answer

Add the following dependency in your pom.xml

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

if you use spring boot starter version 2.1.16, you will normally have a parent pom which will also define the default version for the above dependency.

Then replace @RestController with @Path("/") from package javax.ws.rs.Path

Now it should be working.

Keep in mind your embedded server now, will be jersey instead of default tomcat.

Edit: Also as found from the author of the question another change is needed to register a ResourceConfig. This is also described here in the official documentation.

Advertisement