无法使用 RESTEasy 在 JAX-RS 中找到路径资源

Could not find resource for path in JAX-RS with RESTEasy

我创建了一个原型为 maven-archetype-webapp 的 maven 项目,然后为了将我的应用程序公开为 rest webservice,我正在使用 RestEasy,但是在 rest 应用程序中实现之后我无法访问 URL 由我创建。 请指导我做错了什么。感谢阅读问题。

我的 UserManagementController 如下:

package com.restimplement.btl.pl.controller;

import javax.ws.rs.DefaultValue;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Response;

import com.restimplement.btl.pl.dao.UserDao;

@Path(value="/UserManagement")
public class UserManagementController {

    @GET
    @Path("/{pathParameter}")
    public Response getAllUsers( @PathParam("pathParameter") String pathParameter,
            @DefaultValue("Nothing to say") @QueryParam("queryParameter") String queryParameter)
    {
        UserDao userdao = new UserDao();
        System.out.println("able to visit  user add");
        String response = "details from: "+userdao.findObject();
        return Response.status(200).entity(response).build();
    }

}

我的MyRestWS.java如下:

package com.restimplement.btl.pl.controller;

import java.util.HashSet;
import java.util.Set;
import javax.ws.rs.core.Application;

public class MyRestWS extends Application{

    private Set<Object> singletons = new HashSet<Object>();

    public MyRestWS() {
        singletons.add(new UserManagementController());
    }
}

我的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>BTL_PL</display-name>

    <servlet-mapping>
        <servlet-name>resteasy-servlet</servlet-name>
        <url-pattern>/btlservice/*</url-pattern>
    </servlet-mapping>

    <!-- this should be the same URL pattern as the servlet-mapping property -->
    <context-param>
        <param-name>resteasy.servlet.mapping.prefix</param-name>
        <param-value>/btlservice</param-value>
    </context-param>

    <servlet>
        <servlet-name>resteasy-servlet</servlet-name>
        <servlet-class>
            org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher
        </servlet-class>
        <init-param>
            <param-name>javax.ws.rs.Application</param-name>
            <param-value>com.restimplement.btl.pl.controller.MyRestWS</param-value>
        </init-param>
    </servlet>

</web-app>

应用程序已成功部署在 JBoss-wildfly 8 上,但是在访问 URL 时:http://localhost:8080/BTL_PL/btlservice/UserManagement/getUser;我在 jboss 控制台上遇到以下错误:

14:19:33,240 WARN  [org.jboss.resteasy.core.ExceptionHandler] (default task-10)
failed to execute: javax.ws.rs.NotFoundException: Could not find resource for fu
ll path: http://localhost:8080/BTL_PL/btlservice/UserManagement/getUser
        at org.jboss.resteasy.core.registry.ClassNode.match(ClassNode.java:73) [
resteasy-jaxrs-3.0.6.Final.jar:]
        at org.jboss.resteasy.core.registry.RootClassNode.match(RootClassNode.ja
va:48) [resteasy-jaxrs-3.0.6.Final.jar:]
        at org.jboss.resteasy.core.ResourceMethodRegistry.getResourceInvoker(Res
ourceMethodRegistry.java:444) [resteasy-jaxrs-3.0.6.Final.jar:]
        at org.jboss.resteasy.core.SynchronousDispatcher.getInvoker(SynchronousD
ispatcher.java:234) [resteasy-jaxrs-3.0.6.Final.jar:]
        at org.jboss.resteasy.core.SynchronousDispatcher.invoke(SynchronousDispa
tcher.java:171) [resteasy-jaxrs-3.0.6.Final.jar:]
        at org.jboss.resteasy.plugins.server.servlet.ServletContainerDispatcher.
service(ServletContainerDispatcher.java:220) [resteasy-jaxrs-3.0.6.Final.jar:]
        at org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher.servi
ce(HttpServletDispatcher.java:56) [resteasy-jaxrs-3.0.6.Final.jar:]
        at org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher.servi
ce(HttpServletDispatcher.java:51) [resteasy-jaxrs-3.0.6.Final.jar:]
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:790) [jboss-s
ervlet-api_3.1_spec-1.0.0.Final.jar:1.0.0.Final]
        at io.undertow.servlet.handlers.ServletHandler.handleRequest(ServletHand
ler.java:85) [undertow-servlet-1.0.0.Final.jar:1.0.0.Final]
        at io.undertow.servlet.handlers.security.ServletSecurityRoleHandler.hand
leRequest(ServletSecurityRoleHandler.java:61) [undertow-servlet-1.0.0.Final.jar:
1.0.0.Final]
        at io.undertow.servlet.handlers.ServletDispatchingHandler.handleRequest(
ServletDispatchingHandler.java:36) [undertow-servlet-1.0.0.Final.jar:1.0.0.Final
]
        at org.wildfly.extension.undertow.security.SecurityContextAssociationHan
dler.handleRequest(SecurityContextAssociationHandler.java:78)
        at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateH
andler.java:25) [undertow-core-1.0.0.Final.jar:1.0.0.Final]
        at io.undertow.servlet.handlers.security.SSLInformationAssociationHandle
r.handleRequest(SSLInformationAssociationHandler.java:113) [undertow-servlet-1.0
.0.Final.jar:1.0.0.Final]
        at io.undertow.security.handlers.AuthenticationCallHandler.handleRequest
(AuthenticationCallHandler.java:52) [undertow-core-1.0.0.Final.jar:1.0.0.Final]
        at io.undertow.security.handlers.AbstractConfidentialityHandler.handleRe
quest(AbstractConfidentialityHandler.java:45) [undertow-core-1.0.0.Final.jar:1.0
.0.Final]
        at io.undertow.servlet.handlers.security.ServletConfidentialityConstrain
tHandler.handleRequest(ServletConfidentialityConstraintHandler.java:61) [underto
w-servlet-1.0.0.Final.jar:1.0.0.Final]
        at io.undertow.servlet.handlers.security.CachedAuthenticatedSessionHandl
er.handleRequest(CachedAuthenticatedSessionHandler.java:70) [undertow-servlet-1.
0.0.Final.jar:1.0.0.Final]
        at io.undertow.security.handlers.SecurityInitialHandler.handleRequest(Se
curityInitialHandler.java:76) [undertow-core-1.0.0.Final.jar:1.0.0.Final]
        at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateH
andler.java:25) [undertow-core-1.0.0.Final.jar:1.0.0.Final]
        at org.wildfly.extension.undertow.security.jacc.JACCContextIdHandler.han
dleRequest(JACCContextIdHandler.java:61)
        at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateH
andler.java:25) [undertow-core-1.0.0.Final.jar:1.0.0.Final]
        at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateH
andler.java:25) [undertow-core-1.0.0.Final.jar:1.0.0.Final]
        at io.undertow.servlet.handlers.ServletInitialHandler.handleFirstRequest
(ServletInitialHandler.java:240) [undertow-servlet-1.0.0.Final.jar:1.0.0.Final]
        at io.undertow.servlet.handlers.ServletInitialHandler.dispatchRequest(Se
rvletInitialHandler.java:227) [undertow-servlet-1.0.0.Final.jar:1.0.0.Final]
        at io.undertow.servlet.handlers.ServletInitialHandler.access[=13=]0(Servlet
InitialHandler.java:73) [undertow-servlet-1.0.0.Final.jar:1.0.0.Final]
        at io.undertow.servlet.handlers.ServletInitialHandler.handleRequest(Se
rvletInitialHandler.java:146) [undertow-servlet-1.0.0.Final.jar:1.0.0.Final]
        at io.undertow.server.Connectors.executeRootHandler(Connectors.java:168)
 [undertow-core-1.0.0.Final.jar:1.0.0.Final]
        at io.undertow.server.HttpServerExchange.run(HttpServerExchange.java:6
87) [undertow-core-1.0.0.Final.jar:1.0.0.Final]
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.
java:1145) [rt.jar:1.7.0_79]
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor
.java:615) [rt.jar:1.7.0_79]
        at java.lang.Thread.run(Thread.java:745) [rt.jar:1.7.0_79]

如果我需要将其他内容添加到查询中以获取更多信息,请告诉我。另外,如果我尝试访问错误的 URL.

在 servlet 3.x 环境中不需要 web.xml

在 Servlet 3.x 环境中(如 WildFly 8.x),web.xml 部署描述符不是强制性的,在简单的应用程序中您可能不需要它。

您可以删除它并在 pom.xml 文件中将 maven-war-plugin to ignore the missing web.xml file by setting failOnMissingWebXml 配置 属性 设置为 false

<plugins>
    ...
    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-war-plugin</artifactId>
        <version>3.0.0</version>
        <configuration>
            <failOnMissingWebXml>false</failOnMissingWebXml>
        </configuration>
    </plugin>
    ...
</plugins>

正在修复您的 Application 子class

将您的 Application subclass 更改为:

@ApplicationPath("btlservice")
public class MyRestWS extends Application {

    @Override
    public Set<Class<?>> getClasses() {
        HashSet<Class<?>> set = new HashSet<Class<?>>();
        set.add(UserManagementController.class);
        return set;
    }
}

getClasses() method returns a set of root resource, provider and feature class 是的。资源 class 实例的默认 life-cycle 是 per-request。提供商(直接注册或通过功能注册)的默认 life-cycle 是单例。

@ApplicationPath 注解可用于注解自定义 Application subclass 并为所有 JAX-RS 在应用程序中配置的资源。使用它,您无需在 web.xml 部署描述符中声明 servlet。

有关 Application class 的更多详细信息,此 可能很有见地。

正在请求您的端点

最后,请求您的端点应在以下 URL:

中可用
http://[server]:[port]/[context]/btlservice/UserManagement/[path-parameter]