如何在TomEE +中配置JAX-RS基本路径? [英] How can I configure the JAX-RS base path in TomEE+?
问题描述
我有一个带有一些JAX-RS服务的WAR,已部署到TomEE Plus中。给定以 @Path( myservice)
注释的服务,TomEE +会将其发布到 localhost:8080 / mywebapp / myservice
I have a WAR with some JAX-RS services, deployed into TomEE Plus. Given a service annotated with @Path("myservice")
, TomEE+ publishes it to localhost:8080/mywebapp/myservice
.
但是,这也使得访问 localhost:8080 / mywebapp / index.jsp
的JSP变得不可能-JAXRSInInterceptor抱怨找不到根资源匹配请求路径,相对路径:/index.jsp
。
However, that also makes accessing a JSP at localhost:8080/mywebapp/index.jsp
impossible - JAXRSInInterceptor complains that No root resource matching request path has been found, Relative Path: /index.jsp
.
所以我想要为所有服务配置路径前缀 api
,将 myservice
URL更改为本地主机:8080 / mywebapp / api / myservice
。如果我自己配置了CXF(带有或不带有Spring),这样做将是微不足道的,因为我可以简单地更改CXF Servlet的URL模式-但是我依赖于默认设置,除了注释。那么在这种情况下我该怎么做呢?
So I would like to configure a path prefix api
to all services, which changes the myservice
URL to localhost:8080/mywebapp/api/myservice
. Doing so would be trivial if I had configured CXF on my own (with or without Spring), because I could simply change the URL pattern of the CXF Servlet - but I am relying on the default settings where I don't configure anything besides the annotations. So how do I do that in this case?
请注意,我不想更改 @Path
包括前缀的注释,因为不能解决JSP的问题。
Note that I don't want to alter the @Path
annotations to include the prefix, because that does not fix the issue with the JSP.
推荐答案
创建 javax.ws.rs.core.Application 并用进行注释 @ApplicationPath ,其中值将为 api
在您的情况下:
Create an extension of javax.ws.rs.core.Application and annotate it with @ApplicationPath where value would be api
in your case:
@ApplicationPath("/api")
public class MyApplication extends Application {
@Override
public Set<Class<?>> getClasses() {
final Set<Class<?>> classes = new HashSet<Class<?>>();
// register root resource
classes.add(MyServiceResource.class);
return classes;
}
}
这样,Servlet 3容器将找到您的应用程序并将您的资源映射到 / mywebapp / api / myservice
,同时使Web资源(.jsp)在 / mywebapp
可用。
This way a Servlet 3 container would find your application and map your resource to /mywebapp/api/myservice
while making your web resources (.jsp) available at /mywebapp
.
这篇关于如何在TomEE +中配置JAX-RS基本路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!