与maven一起休息有404找不到错误

时间:2017-08-28 11:24:17

标签: java rest maven tomcat8

我已经用javax.ws尝试了非常简单的restful教程,我对maven依赖有问题。 web.xml文件如下所示:

<?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>
像这样的

和maven依赖:

<?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>

和Class是:

package service;

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

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

问题是,当我将JAR文件添加到WEB-INF的lib文件夹中时,它将工作并连接到我的服务,但是当我尝试添加maven依赖项并删除lib文件夹时,它不起作用!你能告诉我这是什么问题吗?

1 个答案:

答案 0 :(得分:0)

我确实遇到了同样的问题。

更改这个

的javax依赖项
<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>

并创建此类

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;
        }

}

让你的web.xml像这样,因为在这个新的实现中,它没有被使用

<?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>