Webservice Jersey REST Not Found

Asked

Viewed 593 times

2

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

inserir a descrição da imagem aqui

My POM.XML

<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>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 Class of Resource

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";
    }
}

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’s - Resources). Meanwhile the name of package where the class is ProdutoResource is br.com.webservice.resource (without’s').

So, either you change your name package for 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>

A remark:

You don’t need that stretch on your web.xml, 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>

Starting with version 2.0 of Jersey, Jersey "automatically recognizes" Jackson when it encounters the jar jersey-media-json-jackson (that you added to your pom.xml).

  • Perfect, worked.

Browser other questions tagged

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