Home > Net >  Simple web rest api in Java with eclipse and tomcat
Simple web rest api in Java with eclipse and tomcat

Time:11-26

I followed a tutorial and the project don't work. When I deploy the war in tomcat and access the address http://localhost:8080/queroquero/rest/vendedor/get I have 404 status, but if I access http://localhost:8080/queroquero/ I get a "Hello world".

@ApplicationPath("rest")
public class Main extends Application{ }
---------------------------------------------
@Path("vendedor")
public class VendedorResource {

    @GET
    @Path("get")
    @Produces(MediaType.APPLICATION_JSON)
    public Response get() {
        String texto = new String("It's working!!!");
        return Response.ok(texto).build();
    }
    
}

My POM.xml dependencies:

<dependencies>
    <dependency>
        <groupId>jakarta.platform</groupId>
        <artifactId>jakarta.jakartaee-api</artifactId>
        <version>8.0.0</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>jakarta.xml.bind</groupId>
        <artifactId>jakarta.xml.bind-api</artifactId>
        <version>2.3.3</version>
    </dependency>
    <dependency>
        <groupId>com.google.code.gson</groupId>
        <artifactId>gson</artifactId>
        <version>2.7</version>
    </dependency>
  </dependencies>

My web.xml:

<web-app>
  <display-name>Archetype Created Web Application</display-name>
  <!-- Auto scan REST service -->
</web-app>

I'm already spent hours and cannot find what is wrong or missing.

CodePudding user response:

Probably you need to improve your @Path definition?: as far as I know you should put:

@Path("/rest")
@Path("/venderdor")
@Path("/get")

CodePudding user response:

So guys, The problems was a conflit genereted by the maven imports. As far a change the libraries looking for compability I got the results. The main change was to change the Jakarta implementation of JAX for Jersey implementation. If anyone wants to know how I did it, just check the complete code in github (Rest code). Unfortunately I did not have time to discover what is the correct versions of jakarta implementation, which of them to match. I will share here the dependencies of pom.xml:

<dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.glassfish.jersey</groupId>
                <artifactId>jersey-bom</artifactId>
                <version>${jersey.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <dependencies>
        <dependency>
            <groupId>org.glassfish.jersey.containers</groupId>
            <artifactId>jersey-container-servlet-core</artifactId>
            <!-- use the following artifactId if you don't need servlet 2.x compatibility -->
            <!-- artifactId>jersey-container-servlet</artifactId -->
        </dependency>
        <dependency>
            <groupId>org.glassfish.jersey.inject</groupId>
            <artifactId>jersey-hk2</artifactId>
        </dependency>
        
        <dependency>
            <groupId>com.google.code.gson</groupId>
            <artifactId>gson</artifactId>
            <version>2.7</version>
        </dependency>
        
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.27</version>
        </dependency>
    </dependencies>
  • Related