@Path注解要求?

@Path Annotation requirements?

我是在 Java 中指定资源路由的新手,我在指定路由时遇到了问题。到目前为止,我有一个 Class 可以简单地扩展 Application,还有一个 class 可以对输入做出反应。这些 classes 的路由要求是什么?我下面的代码不起作用,我试图找出原因。我试图找到这些资源的来源,但运气不佳。

  1. 我可以只使用 / 作为 ApplicationPath 吗? class 所做的只是扩展 Application 以便它可以找到路线。

示例:

package com.sentiment360.helloworld;

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

/**
 * JAXActivator is an arbitrary name, what is important is that javax.ws.rs.core.Application is extended
 * and the @ApplicationPath annotation is used with a "rest" path.  Without this the rest routes linked to
 * from index.html would not be found.
 */
@ApplicationPath("/")
public class JAXActivator extends Application {
}

是否每个 class 都需要有一个声明的@Path(或者它们都可以是@Stateless)?

@Path("/helloservice")
public class HelloService {

    private static Logger _logger;

    public HelloService(){
        _logger = Logger.getLogger(HelloService.class.getName());
    }

    private Connection conn() throws SQLException  {...}
}

#1 的简短版本是。

但是:行为取决于实现。详见 。

至于 #2,是的,您通常需要为您想要的每个端点指定一个 @Path。它是 @Stateless 的事实无关紧要,因为您需要一种方法来解决它。