bigelite
bigelite

Reputation: 29

restful with maven has 404 not found error

i've tried very simple restful tutorial with javax.ws and i have problem with maven dependency. web.xml file as comming below:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee   http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1">
<servlet>
    <servlet-name>jersey-servlet</servlet-name>
    <servlet-class>
        com.sun.jersey.spi.container.servlet.ServletContainer
    </servlet-class>
    <init-param>
        <param-name>com.sun.jersey.config.property.packages</param-name>
        <param-value>service</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>jersey-servlet</servlet-name>
    <url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>

and maven dependency like this:

<?xml version="1.0" encoding="UTF-8"?>
<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>groupId</groupId>
<artifactId>untitled40</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
    <dependency>
        <groupId>javax.ws.rs</groupId>
        <artifactId>javax.ws.rs-api</artifactId>
        <version>2.0</version>
    </dependency>
    <dependency>
        <groupId>com.sun.jersey</groupId>
        <artifactId>jersey-server</artifactId>
        <version>1.2</version>
    </dependency>
</dependencies>
</project>

and Class is:

package service;

import javax.ws.rs.*;
@Path("hesab")
public class App {

@Path("textc")
@GET
@Produces("text/plain")
public String recit(){
    return "got it";
}
}

and the problem is when i add JAR files into lib folder in WEB-INF it will work and connect to my service, but when i try to add maven dependency and delete lib folder, it doesn't work! could you please tell me what the problem is?

Upvotes: 0

Views: 1223

Answers (1)

diego medeiros
diego medeiros

Reputation: 11

i did have the same problem.

change your javax dependency for this one

<dependency>
    <groupId>org.glassfish.jersey.core</groupId>
    <artifactId>jersey-server</artifactId>
    <version>2.6</version>
</dependency>
<dependency>
    <groupId>org.glassfish.jersey.containers</groupId>
    <artifactId>jersey-container-servlet</artifactId>
    <version>2.6</version>
</dependency>
<dependency>
    <groupId>com.fasterxml.jackson.jaxrs</groupId>
    <artifactId>jackson-jaxrs-json-provider</artifactId>
    <version>2.4.1</version>
</dependency>

and create this class

package ***.services;

import java.util.HashSet;
import java.util.Set;

import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;

import ***UsuarioResource;

/**
 * The Class PortalAppConfiguration.
 */
@ApplicationPath("api")
public class ApiConfiguration extends Application {

     @Override
        public Set<Class<?>> getClasses()
        {
            final Set<Class<?>> classes = new HashSet<>();
            classes.add(UsuarioResource.class);//your resource

            return classes;
        }

}

let your web.xml like this, because in this new implementation, it is not used

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee   http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1">

</web-app>

Upvotes: 1

Related Questions