1

im trying to learn how to make a Rest API on java and im having issues while testing it.

I get 404 not Found when i try to make a get on localhost:8080/restService/prueba same for localhost:8080/restService/vendedores

I tried a thousand way to make the URI, this is just the last way i tried it.

To deploy the api y packaged as a war the java project and paste the .war on the webapps directory of my tomcat installation and the run the startup.bat of tomcat

This is muy 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>restService</display-name>
<servlet>
<servlet-name>Jersey REST Service</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
 <!-- Register resources and providers under com.vogella.jersey.first package. -->
<init-param>
    <param-name>jersey.config.server.provider.packages</param-name>
    <param-value>com.arquimeda.capacitacion</param-value>
</init-param>
<init-param>
<param-name>jersey.config.server.provider.classnames</param-name>
<param-value>org.glassfish.jersey.jackson.JacksonFeature</param-value>
</init-param>
    <load-on-startup>1</load-on-startup>
 </servlet>
 <servlet-mapping>
<servlet-name>Jersey REST Service</servlet-name>
<url-pattern>/restService/*</url-pattern>
</servlet-mapping>
</web-app> 

This are my dependcies

 <dependency>
  <groupId>org.glassfish.jersey.core</groupId>
  <artifactId>jersey-server</artifactId>
  <version>2.30.1</version>
 </dependency>
 <dependency>
  <groupId>org.glassfish.jersey.containers</groupId>
  <artifactId>jersey-container-servlet</artifactId>
  <version>2.30.1</version>
 </dependency>
 <dependency>
  <groupId>org.glassfish.jersey.inject</groupId>
  <artifactId>jersey-hk2</artifactId>
  <version>2.30.1</version>
 </dependency>
 <dependency>
  <groupId>org.glassfish.jersey.media</groupId>
  <artifactId>jersey-media-json-jackson</artifactId>
  <version>2.30.1</version>
 </dependency>
</dependencies>

And this is my code

package com.arquimeda.capacitacion;

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

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


@Path("/restService")
public class RestService {

    @GET
    @Path("/vendedor/{vend_id}")
    @Produces(MediaType.APPLICATION_JSON)
    public Vendedor  getVendedorPorId(@PathParam("vend_id")int id) throws Exception {
        VendedorDAO vendedorDAO = new VendedorDAO();
        System.out.println("hola");
        return vendedorDAO.getVendedorPorId(id); 
    }

    @GET
    @Path("/vendedores")
    @Produces("application/json ")
    public List<Vendedor>  getVendedores() throws Exception {
        /*VendedorDAO vendedorDAO = new VendedorDAO();
        System.out.println("hola");
        return vendedorDAO.getVendedores(); 
        */
        Vendedor vend1 = new Vendedor(5,"juan",1,null,70);
        Vendedor vend2 = new Vendedor(5,"juan",1,null,70);
        List<Vendedor> lista = new ArrayList<Vendedor>();
        lista.add(vend1);
        lista.add(vend2);
        return lista;
    }

    @GET
    @Path("/prueba")
    @Produces(MediaType.TEXT_PLAIN)
    public String getPrueba(){
        return "hola";
    }
}
Steffen Harbich
  • 2,639
  • 2
  • 37
  • 71

2 Answers2

0

Use glassfish server rather than tomcat. i think problem is occur because you mention servlet class is glassfish and used a tomcat and please check http://localhost:8080/manager/text/list that is deployed or not you also need to create a user for login. go to the TOMCAT_HOME/conf/tomcat-users.xml and add that.. and for glassfish it may helpful to you. Where would i put .war file in glassfish server?

Thanks

0

Try the url as below. since the path is also configured as restService

localhost:8080/restService/restService/vendedores
Sam
  • 143
  • 1
  • 8