Java RESTFULL using Jersey

0

Well, I'm trying to develop a Java project with a Web Service REST, I'm new to this Web Service business, so I'm kind of lost. I can not access my service by the URL of the browser, when I enter the address of the Service, the browser responds with the error "404". Please help me, I have already imported the jersey libs and etc ... After several searches, I have not found where I am going wrong. Below is my code for the web.xml and Service class. Thank you!

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 class 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;
    }
}
    
asked by anonymous 19.02.2017 / 07:58

1 answer

2

You are mixing Jersey 1.x and 2.x version 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

In this 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 the 404 error continues to occur and you are using Tomcat, right-click it, click clear and restart the server.

    
19.02.2017 / 14:54