WebService Jersey REST Not Found

2

I have the following problem: when I run my jersey server and type the url mapped in web.xml the following error appears.

MyPOM.XML

<projectxmlns="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>br.com.webservice</groupId>
    <artifactId>Jersey</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>


    <build>
        <sourceDirectory>src</sourceDirectory>
        <plugins>
            <plugin>
                <artifactId>maven-war-plugin</artifactId>
                <version>2.3</version>
                <configuration>
                    <warSourceDirectory>WebContent</warSourceDirectory>
                    <failOnMissingWebXml>false</failOnMissingWebXml>
                </configuration>
            </plugin>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <source>1.7</source>
                    <target>1.7</target>
                </configuration>
            </plugin>
        </plugins>
    </build>

    <!-- Vesão do Jersey -->
    <properties>
        <jersey.version>2.26-b08</jersey.version>
    </properties>

    <dependencies>
        <!-- Hibernate -->
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-entitymanager</artifactId>
            <version>5.0.1.Final</version>
        </dependency>
        <!-- Mysql -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.38</version>
        </dependency>
        <!-- Api do Servidor -->
        <dependency>
            <groupId>org.glassfish.jersey.core</groupId>
            <artifactId>jersey-server</artifactId>
            <version>${jersey.version}</version>
        </dependency>
        <dependency>
            <groupId>org.glassfish.jersey.containers</groupId>
            <artifactId>jersey-container-servlet</artifactId>
            <version>${jersey.version}</version>
        </dependency>
        <dependency>
            <groupId>org.glassfish.jersey.media</groupId>
            <artifactId>jersey-media-json-jackson</artifactId>
            <version>${jersey.version}</version>
        </dependency>
        <dependency>
            <groupId>org.glassfish.jersey.inject</groupId>
            <artifactId>jersey-hk2</artifactId>
            <version>${jersey.version}</version>
        </dependency>
    </dependencies>



</project>

WEB.XML

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://java.sun.com/xml/ns/javaee"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
    id="WebApp_ID" version="3.0">
    <display-name>Jersey</display-name>

    <servlet>
        <!-- Servlet Jersey -->
        <servlet-name>Servlet Produtos</servlet-name>
        <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>

        <init-param>
            <param-name>jersey.config.server.provider.packages</param-name>
            <param-value>br.com.webservice.resources</param-value>
        </init-param>

        <!-- Define Conversões para o fromato JSON -->
        <init-param>
            <param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
            <param-value>true</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <!-- Mapeia um servlet  -->
    <servlet-mapping>
        <servlet-name>Servlet Produtos</servlet-name>
        <url-pattern>/v1/*</url-pattern>
    </servlet-mapping>


</web-app>

My Resource class

package br.com.webservice.resource;

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

import br.com.webservice.domain.Produto;
import br.com.webservice.service.ProdutoService;

@Path("produto")
public class ProdutoResource {

    @GET
    @Produces(MediaType.APPLICATION_JSON + ";charset=utf-8")
    public String teste(){
        return "teste";
    }
}
    
asked by anonymous 16.12.2017 / 21:42

1 answer

0

The problem is here:

<init-param>
    <param-name>jersey.config.server.provider.packages</param-name>
    <param-value>br.com.webservice.resources</param-value>
</init-param>

You asked Jersey to scan the package br.com.webservice.resources (note the 's' - resource s ). In the meantime the name of the package where class ProdutoResource is br.com.webservice.resource (without the 's').

Then you either rename your package to br.com.webservice.resources , or you change your mapping to:

<init-param>
    <param-name>jersey.config.server.provider.packages</param-name>
    <param-value>br.com.webservice.resource</param-value>
</init-param>

One note:

You do not need this snippet in your web.xml , you can remove it:

<!-- Define Conversões para o fromato JSON -->
<init-param>
    <param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
    <param-value>true</param-value>
</init-param>

From Jersey version 2.0, Jersey "recognizes" Jackson automatically by finding the jersey-media-json-jackson jar (which you added to your pom.xml ).

    
17.12.2017 / 12:31