RESTEASY003210:找不到完整路径的资源:Tomcat

RESTEASY003210: Could not find resource for full path : Tomcat

我收到以下错误:

这是堆栈跟踪:

javax.ws.rs.NotFoundException: RESTEASY003210: Could not find resource for full path: http://localhost:9050/scenarioplanner/api/models/
    at org.jboss.resteasy.core.registry.ClassNode.match(ClassNode.java:75)
    at org.jboss.resteasy.core.registry.RootClassNode.match(RootClassNode.java:48)
    at org.jboss.resteasy.core.ResourceMethodRegistry.getResourceInvoker(ResourceMethodRegistry.java:445)
    at org.jboss.resteasy.core.SynchronousDispatcher.getInvoker(SynchronousDispatcher.java:257)
    at org.jboss.resteasy.core.SynchronousDispatcher.invoke(SynchronousDispatcher.java:194)
    at org.jboss.resteasy.plugins.server.servlet.ServletContainerDispatcher.service(ServletContainerDispatcher.java:221)
    at org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher.service(HttpServletDispatcher.java:56)
    at org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher.service(HttpServletDispatcher.java:51)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:292)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:240)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:212)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:141)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
    at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:616)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:528)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1099)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:670)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1520)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1476)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Thread.java:745)

这是我的web.xml

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

</webapp>

这是我的pom.xml

<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/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>us.deloitteinnovation.dmaanalytics.nissan.backend</groupId>
    <artifactId>scenarioplanner</artifactId>
    <packaging>war</packaging>
    <version>1.0.0-SNAPSHOT</version>
    <name>Scenario Planner Maven Webapp</name>
    <url>http://maven.apache.org</url>



    <dependencies>

     <dependency>
         <groupId>org.jboss.resteasy</groupId>
            <artifactId>resteasy-servlet-initializer</artifactId>
            <version>3.0.12.Final</version>
        </dependency>

<!-- https://mvnrepository.com/artifact/org.jboss.resteasy/resteasy-jaxb-provider -->
<dependency>
    <groupId>org.jboss.resteasy</groupId>
    <artifactId>resteasy-jaxb-provider</artifactId>
    <version>3.0.12.Final</version>
</dependency>

<!-- https://mvnrepository.com/artifact/org.jboss.resteasy/resteasy-jaxrs -->
<dependency>
    <groupId>org.jboss.resteasy</groupId>
    <artifactId>resteasy-jaxrs</artifactId>
    <version>3.0.12.Final</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.jboss.resteasy/resteasy-validator-provider-11 -->
<dependency>
    <groupId>org.jboss.resteasy</groupId>
    <artifactId>resteasy-validator-provider-11</artifactId>
    <version>3.0.12.Final</version>
</dependency>


<dependency>
        <groupId>org.jboss.resteasy</groupId>
        <artifactId>resteasy-jackson-provider</artifactId>
        <version>3.0.12.Final</version>
    </dependency> 


<dependency>
        <groupId>net.sf.scannotation</groupId>
        <artifactId>scannotation</artifactId>
        <version>1.0.2</version>
    </dependency>

<!-- https://mvnrepository.com/artifact/javax.ws.rs/javax.ws.rs-api -->
<dependency>
    <groupId>javax.ws.rs</groupId>
    <artifactId>javax.ws.rs-api</artifactId>
    <version>2.1-m01</version>
</dependency>

    <!-- https://mvnrepository.com/artifact/org.jasypt/jasypt -->
<dependency>
    <groupId>org.jasypt</groupId>
    <artifactId>jasypt</artifactId>
    <version>1.9.2</version>
</dependency>

    </dependencies>

    <build>
        <finalName>scenarioplanner</finalName>
        <plugins>
            <plugin>
             <groupId>org.apache.maven.plugins</groupId>
              <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>

        </plugins>

    </build>

</project> 

我在这里声明了一个空应用程序class:

public class ScenarioPlannerApp  extends Application{}

我正在使用以下简单的 Web 服务 URL http://localhost:9050/scenarioplanner/api/models/:

资源代码是:

@Path("")
public class ModelController 
{

    @GET    
    @Path("/models/")
    @Produces(MediaType.APPLICATION_JSON)


    public Response getModels() throws Exception {

        ModelService modelService = null;
        List<CarModelData> models;

            models = modelService.getModelDetails();

        logger.debug("Webservice Name [/models/] execution completed with HTTP status OK [200]");
        //return Responsestatus(Status.OK).entity(models).build();
        return Response     
        .status(Status.OK)

        .entity(models)
        .build(); 

    }

我已经阅读了很多关于堆栈溢出的答案,但无法解决这个错误。 我花了一整天的时间试图调试这个错误,但徒劳无功!

任何帮助将不胜感激!!

这个问题已经被我解决了。 这是我需要做的:

@Path("")
public class ModelController 
{

    @GET    
    @Path("/models/")
    @Produces(MediaType.APPLICATION_JSON)


    public Response getModels() throws Exception {

  return Response     
        .status(Status.OK)

        .entity(models)
        .build(); 

}

在上面的代码中,

我不得不change the path @Path("") to @Path("/api")

就是这样!!

其他一切都很好!

修复应用程序路径

注释扩展Application with @ApplicationPath的class:

@ApplicationPath("/api")
public class ScenarioPlannerApp extends Application {
    ...
}

修复资源路径

在这种情况下用@Path using /models as value. The getModels() method doesn't need a @Path注解ModelControllerclass:

@Path("/models")
public class ModelController {

    @GET    
    @Produces(MediaType.APPLICATION_JSON)
    public Response getModels() throws Exception {
        ...
    }
}

重要的是要记住 资源 class 上的 @Path 注释表示 资源

创建资源方法

您很可能需要向 ModelController 资源 class 添加更多方法。请参阅以下示例:

要拥有处理 /api/models 上的 POST 请求的方法,您可以使用以下方法:

@POST
@Consumes(MediaType.APPLICATION_JSON)
public Response createModel(Model model) throws Exception {
    ...
}

要使用一种方法来处理 /api/models/{id} 上的 GET 请求,您可以使用以下方法:

@GET
@Path("/{id}")
@Produces(MediaType.APPLICATION_JSON)
public Response getModel(@PathParam("id") Long id) throws Exception {
    ...
}

并且请务必记住,资源 class 的 资源方法 上的 @Path 注释指定了 子资源 的资源。

对于那些正在将他们的 Resteasy 版本从 2.X 迁移到 3.X 或 4.X 的人。

JAX-RS 2.0 的路径匹配算法发生了变化,变得非常严格。

为了继续使用现有方法的 @Path,您必须启用 resteasy.wider.request.matching Resteasy 配置。

web.xml

<context-param>
    <param-name>resteasy.wider.request.matching</param-name>
    <param-value>true</param-value>
</context-param>

参考:Resteasy Migration Guide Section 55.3