Java RESTFULL using Jersey

Asked

Viewed 245 times

0

Guys, I’m trying to develop a project in Java with a Web Service REST, I’m new in this Web Service business, so I’m kind of lost. I cannot access my service through the browser URL, when I enter the Service address, the browser responds with the error "404". Please help me, I’ve imported the libs from Jersey and so on ... After several searches, I haven’t found where I’m going wrong. Below follows my web.xml code and Service class. Thanks in advance!

My 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" 
         xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 
         id="WebApp_ID" 
         version="2.5">

  <!-- COMO DEFAULT, O NOME DO MEU PROJETO, ESSE NOME IRÁ APARECER NA URL -->
  <display-name>Cast_Frotas</display-name> 

  <!-- MAPEANDO O SERVLET-->
  <servlet> 
    <servlet-name>Jersey RESTfull</servlet-name>
    <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>

    <init-param>
      <param-name>com.sun.jersey.config.property.packages</param-name>
      <param-value>br.com.Cast_frotas.service</param-value>
    </init-param> 

    <load-on-startup>1</load-on-startup>
  </servlet>


  <servlet-mapping>
    <servlet-name>Jersey RESTfull</servlet-name>
    <url-pattern>/rest/*</url-pattern>
  </servlet-mapping>
</web-app>

My kind of service

package br.com.Cast_frotas.service;

import java.util.ArrayList;
import java.util.List;

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

import br.com.Cast_frotas.controle.CtrlCurso;
import br.com.Cast_frotas.model.Curso;

@Path("/curso")
public class CursoService {

    @GET
    @Path("/teste")
    @Produces(MediaType.TEXT_HTML)
    public String teste(){
        String txt = "<html>" + "<title>" + "TESTE Serviço" + "</title>"
                + "<body>" + "<h1>" + "Acessei o serviço" + "</h1>"
                + "</body>" + "</html>";
        return txt;
    }

    @POST
    @Path("/insert")
    public void inserirCurso(Curso c){
        new CtrlCurso().insert(c);
    }

    @GET
    @Path("/select")
    @Produces(MediaType.APPLICATION_JSON)
    public List<Curso> getListAll(){
        List<Curso> lista_cursos = new ArrayList<Curso>();

        lista_cursos = new CtrlCurso().getList();

        return lista_cursos;
    }
}

1 answer

2


You’re mixing Jersey versions 1.x and 2.x settings in your web.xml. The following property belongs to version 1.x:

com.sun.jersey.config.property.packages

The corresponding property in version 2.x is:

jersey.config.server.provider.packages

That way, your web.xml would look like this:

?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" 
         xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 
         id="WebApp_ID" 
         version="2.5">

  <!-- COMO DEFAULT, O NOME DO MEU PROJETO, ESSE NOME IRÁ APARECER NA URL -->
  <display-name>Cast_Frotas</display-name> 

  <!-- MAPEANDO O SERVLET-->
  <servlet> 
    <servlet-name>Jersey RESTfull</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.Cast_frotas.service</param-value>
    </init-param> 

    <load-on-startup>1</load-on-startup>
  </servlet>


  <servlet-mapping>
    <servlet-name>Jersey RESTfull</servlet-name>
    <url-pattern>/rest/*</url-pattern>
  </servlet-mapping>
</web-app>

If error 404 continues to occur and you are using server Tomcat, right click on it, click on clear and restart the server.

  • Thanks for the answer, Felipe! I did this, but keep giving the 404 error, am I typing the wrong url? I start the Tomcat Server, go to the browser and type: localhost:8080/ + name of my project/ + Rest / + course/ + test/... It would be like this?

  • @Matheus Minguini Try to remove the bar at the end so localhost:8080/ + name of my project/ + Rest / + course/ + test

  • Felipe, it worked! Thank you very much! Very successful for you!

Browser other questions tagged

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