是否可以将来自WebServlet的请求转发到其他助手servlet(javax.ws.rs.core.Application) [英] Is it possible to forward a Request from a WebServlet to rest helper servlet (javax.ws.rs.core.Application)

查看:191
本文介绍了是否可以将来自WebServlet的请求转发到其他助手servlet(javax.ws.rs.core.Application)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题: 我有一个WebFilter,它将有效的URL转发到代理Servlet或处理为管理员管理网页的Servlet,以监视最近的请求和更多请求.

Problem: I have a WebFilter which forwards valid urls to either a proxy servlet or a servlet which handles a webpage for the admin to monitor recent requests and more.

管理servlet是支持将ajax请求转发到REST服务的(在从controlpannel.jsp呈现的网页上的login.jsp之后),但是显然其余的服务与WebFilter和WebServlets具有不同的上下文?

The admin servlet is suppost to forward ajax requests to a REST service (after login.jsp from the webpage rendered by controlpannel.jsp ) but apparently the rest service has a different context as the WebFilter and WebServlets ?

问题: 那么,是否有可能从我的WebServlet转发到其余的辅助Servlet(及其资源类)?

Question: So is it at all possible to forward from my WebServlet to the rest helper servlet (and its resource classes) ?

更具体的信息:

这是我使用转发的方式:

This is how I use forwarding:

ServletContext sc = request.getServletContext();
RequestDispatcher dispatcher = sc.getRequestDispatcher(forwardURI);
dispatcher.forward(request, response);

我试图转发到这个uri:

I tried to forward to this uri:

forwardURI = /REST/proxy_client/newer_than

我的休息助手servlet:

My rest helper servlet:

@Stateless
@ApplicationPath("/REST")
public class RestService extends Application {
@Override
public Set<Class<?>> getClasses() {
    final Set<Class<?>> restResourceClasses = new HashSet<Class<?>>();
    restResourceClasses.add(ProxyClientResource.class);
    return restResourceClasses;
}

}

这个资源类别:

@Path("/proxy_client")
@Stateless
public class ProxyClientResource {
@EJB
private ProxyClientBean proxyClientBean;

@GET
@Produces("application/json")
@Path("/newer_than")
public String getNumberOfEntriesNewerThanTimestamp(@QueryParam("timestamp") Expression<Timestamp> indexTimestamp,
        @QueryParam("numberOfclients") Integer numberOfclients) {
    List<ProxyClient> pageData = proxyClientBean.getElementsNewerThan(numberOfclients, indexTimestamp);
    return convertToJSONstring(pageData);
}

解决方案尝试:

我发现了有关如何操作的问题从servlet调用其余的Web服务,但它们使用客户端且不进行转发.

I found this question about how to call a rest web service from a servlet, but they use a client and no forwarding.

编辑:

我遇到一个配置问题(可能仍然有一个),所以现在当我尝试转发到我的rest helper servlet(扩展javax.ws.rs.core.Application的那个)时,我会收到此错误:

I had a configuration problem (might still have one), so now when I try to forward to my rest helper servlet (the one extending javax.ws.rs.core.Application) I get this error:

RestServlet is currently unavailable 

(在web.xml中,我称为Servlet RestServlet)

(in the web.xml I call the Servlet RestServlet)

直接访问REST API时,我得到:

when accessing the REST api directly I get:

HTTP Status 500 - Authenticator.invoke() failed

但是我不知道这意味着什么.

but I can't find out what this means.

我将尝试在web.xml子类中使用配置替换Applicaton的子类,并且@ApplicationPath似乎对我不起作用.同样,当我尝试获取其余的ServletsContext时,我得到一个错误,即未指定任何类,这是在使用web.xml配置时执行的操作.

I will try repacing the subclass of Applicaton with a config in web.xml subclassing and @ApplicationPath dont seem to work for me. Also when I try to get the rest ServletsContext I get an error that no class has been specified, which is something you do when using the web.xml config.

Edit3:

我正在将应用程序部署在HCP上,并且存在潜在的问题,因为我什至无法访问我的REST服务,我

I'm deploying my application on HCP and with the underlying problem beeing that I cant even access my REST service I found this SAP discussion. When I get my REST service working without forwarding I will report back here.

这实际上回答了Edit3的问题 我必须在项目库中添加球衣1.19.1(而不是2.x,因为即时消息使用的Java EE6仅支持DWP 3.0,而不是要求的3.1),否则我会说我没有指定servlet类(但是当我试图添加javax.ws.rs.core.Application,即使我已经看到此配置,它也会告诉我这不是Servlet类.

This actually answers the question from Edit3 I had to add jersey 1.19.1 (not 2.x because im using Java EE6 which only supports up to DWP 3.0 not 3.1 as required) to by projects libraries otherwise It would say that I didn't specify a servlet class (but when I tried to add javax.ws.rs.core.Application it would tell me this is no Servlet class even though I have seen this configuration).

推荐答案

我的真正问题是SAP Hana Cloud Platform上Java ee6容器的javax.ws.rs.core.Application由于某种未知原因而无法正常工作.

My real problem was that the javax.ws.rs.core.Application from the Java ee6 container on SAP Hana Cloud Platform did not work for a unkown reason.

解决方案是下载jersey-bundle-1.19.1.jar并将其添加到WEB-INF/lib和项目库中.

The solution was to download and add the jersey-bundle-1.19.1.jar to WEB-INF/lib and the projects libraries.

将来自原始servlet的请求转发到其余服务完全没有问题! .如果在您的情况下不起作用,则很可能是您的设置或像我这样的一些意外原因.

There is no problem at all to forward a request from a vanilla servlet to the rest service! If it does not work in your case its most likely your setup or some unexpected reason like it was in my case.

这篇关于是否可以将来自WebServlet的请求转发到其他助手servlet(javax.ws.rs.core.Application)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆