Vinay B.N.
Vinay B.N.

Reputation: 25

Spring boot jersey error

I generated a project on start.spring.io with the following project dependencies:

  1. Jersey (JAX-RS)
  2. JPA
  3. PostgreSQL
  4. Web

When I try to access localhost:8080/homeroom/webapi/test I get a page with the following error:

Whitelabel Error Page

This application has no explicit mapping for /error, so you are seeing this as a fallback.

I get this error no matter what url I try to access. In my console info I can see it print Mapping servlet: 'jerseyServlet' to [/webapi/*] so I know my config class is being registered. When I change @ApplicationPath("/webapi") to @ApplicationPath("/"), a GET on localhost:8080/homeroom/test or localhost:8080/homeroom/ returns a blank page instead, with no text or error.

Why can't I access my resource?

This is my pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>

    <groupId>com.homeroomed</groupId>
    <artifactId>homeroom</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>HomeRoom</name>
    <description>HomeRoom REST API</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.2.5.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <java.version>1.8</java.version>
    </properties>

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

        <dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <version>9.4-1201-jdbc41</version>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

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


</project>

I'm trying to do a descriptor-less deploy so I have:

@Component
@ApplicationPath("/webapi")
public class MyJerseyConfig extends ResourceConfig{
    public MyJerseyConfig() {
//      String packageName =TestJerseyResource.class.getPackage().getName();
//      this.packages(packageName);
        this.register(TestJerseyResource.class);
//      System.out.println("The package is:"+packageName);
    }

}

And I have the following resource:

@Component
@Path("/test")
public class TestJerseyResource {

    @GET
    @Produces(MediaType.APPLICATION_JSON)
    public String getTest()
    {
        return "Hi!";
    }
}

I'm running the project from:

@SpringBootApplication
public class HomeRoomApplication {

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

UPDATE:

So 2 things:

I had to use spring annotations @Controller and @RequestMapping instead of Jersey's @PATH and @GET, and had to GET /webapi/test instead of /homeroom/webapi/test

Upvotes: 0

Views: 1174

Answers (2)

ColinMc
ColinMc

Reputation: 1238

The reason you can't access your resource from looking at the information you provided is you there is no /homeroom anywhere I saw in your code.

This is a valid URL for your project: http://localhost:8080/webapi/test

If you wanted homeroom to be in the URL you could change the application path value to homeroom instead of webapi.

Upvotes: 1

jchampemont
jchampemont

Reputation: 2773

Well, quoting from @ApplicationPath JavaDoc:

May only be applied to a subclass of Application.

https://docs.oracle.com/javaee/6/api/javax/ws/rs/ApplicationPath.html

That might be part of the problem...

Upvotes: 0

Related Questions