Endpoint Rest with Jersey shows no expected result

Asked

Viewed 41 times

-1

I created a Javaee project directly in Intellij and added the Javaee 8 dependencies via Maven. The structure of the project is in this link.

web xml.:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0">
</web-app>

Beans.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans
    xmlns="http://xmlns.jcp.org/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee 
                      http://xmlns.jcp.org/xml/ns/javaee/beans_1_1.xsd"
    bean-discovery-mode="all">
</beans>

Jerseyconfig:

@ApplicationPath("resources")
public class JerseyConfig extends Application {

//    @Override
//    public Set<Class<?>> getClasses()
//    {
//        Set<Class<?>> yourResources = new HashSet<Class<?>>();
//        yourResources.add(MySimpleRest.class);
//        return yourResources;
//    }


}

Mysimplerest :

@Path("/info")
public class MySimpleRest {

    @GET
    public String get(){
        return "Hello Word";
    }
}

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>JavaEE</groupId>
    <artifactId>JavaEE</artifactId>
    <version>1.0-SNAPSHOT</version>
    <!--<packaging>war</packaging>-->

    <dependencies>
        <dependency>
            <groupId>javax</groupId>
            <artifactId>javaee-api</artifactId>
            <version>8.0</version>
            <scope>provided</scope>
        </dependency>
    </dependencies>
</project>

I’m trying to access the url http://localhost:9090/resources/info and give me 404. What am I doing wrong?

1 answer

1


I believe the problem lies in the class of inicalization, I propose the following changes:

First add these (Jersey) dependencies to your pom.xml:

<dependency>
    <groupId>javax.ws.rs</groupId>
    <artifactId>javax.ws.rs-api</artifactId>
    <version>2.1</version>
</dependency>
<dependency>
    <groupId>org.glassfish.jersey.containers</groupId>
    <artifactId>jersey-container-servlet</artifactId>
    <version>2.27</version>
</dependency>
<!-- Isso é necessário se for usar a ultima versao do Jersey -->
<dependency>
    <groupId>org.glassfish.jersey.inject</groupId>
    <artifactId>jersey-hk2</artifactId>
    <version>2.26</version>
</dependency>

Jerseyconfig:

@ApplicationPath("resources")
public class JerseyConfig extends ResourceConfig {

    public JerseyConfig() {
        //Scaneia Dinamicamente pelos Controllers (classes com @Path)
        packages("br.com.javaee.controllers");
    }
}

Mysimplerest :

@Path("/info")
public class MySimpleRest {

    @GET
    @Produces(MediaType.TEXT_PLAIN)
    public Response get() {
        return Response.ok("Hello Word").build();
    }
}

According to that link and that link the Jersey does not work on the application server Jboss, only the Restlet. When testing using it I actually got errors but when switching to the Tomcat or Glassfish worked properly.

  • Which way to import the Resourceconfig? For me, it is not shown to import.

  • import org.glassfish.jersey.server.ResourceConfig, I checked here and you don’t have the jersey dependencies I’ll add this to my answer.

  • I added the dependencies and it still doesn’t work. I tried with "Packages" and Register. I put a debug in the Jerseyconfig constructor, however, it did not pass there. I need to declare something in web.xml?

  • the method MyApplication in Jerseyconfig is the method builder, I misnamed so it should be public JerseyConfig() { }

  • Yes, I’m on. I changed it to the right one, but it still doesn’t work...

  • is sure that the url http://localhost:9090/resources/info is correct? the application server did not put a path with the application name? something like: http://localhost:9090/myApplication/resources/info

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.