我是在 Java 中指定资源路由的新手,并且在指定路由时遇到问题。到目前为止,我有一个类简单地扩展了 Application
,以及一个对输入使用react的类。这些类的路由要求是什么?我下面的代码不起作用,我试图找出原因。我尝试过寻找这些资源,但运气不佳。
- 我可以仅使用
/
作为 ApplicationPath 吗?这个类所做的只是扩展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 {
}
每个类都需要有一个声明的@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 的简短版本是"is"。
但是:行为取决于实现。请参阅https://stackoverflow.com/a/16747253/1063501以获得详尽的解释。
对于#2,是的,您通常需要为您想要的每个端点指定一个@Path
。事实上,它是 @Stateless
并不重要,因为您需要一种方法来解决它。
关于java - @Path 注解的要求?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45426387/