JAX-RS 资源未转发到 JSP 文件

问题描述 投票:0回答:1

我在创建 Web 服务以重定向到 JSP 文件时遇到问题。运行创建的 Web 服务的 url 时出现以下错误:

ERROR [stderr] (default task-2) java.lang.IllegalArgumentException: UT010023: Request HttpServletRequestImpl [ GET /WSService/ws/showView ] was not original or a wrapper.

下面我详细介绍我的代码:

web.xml
文件:

<?xml version="1.0" encoding="UTF-8"?>
<web-app>
    <display-name>WSService</display-name>
    <welcome-file-list>
        <welcome-file>index.html</welcome-file>
        <welcome-file>index.jsp</welcome-file>
        <welcome-file>index.htm</welcome-file>
        <welcome-file>default.html</welcome-file>
        <welcome-file>default.jsp</welcome-file>
        <welcome-file>default.htm</welcome-file>
    </welcome-file-list>
    <servlet>
        <servlet-name>Resteasy</servlet-name>
        <servlet-class>org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>Resteasy</servlet-name>
        <url-pattern>/showView/*</url-pattern>
    </servlet-mapping>
    <context-param>
        <param-name>javax.ws.rs.Application</param-name>
        <param-value>login.WSService</param-value>
    </context-param>
    <listener>
        <listener-class>org.jboss.resteasy.plugins.server.servlet.ResteasyBootstrap</listener-class>
    </listener>
</web-app>

这是

WSService
class
:

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;

@Path("/showView")
public class WSService {

    @GET
    @Path("/redirect")
    @Produces("text/html")
    public void redirect(@Context HttpServletResponse response, @Context HttpServletRequest request) {
        try {
            String data = "someData";
            request.setAttribute("data", data);
            request.getRequestDispatcher("view/view.jsp").forward(request, response);
        } catch (ServletException | IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}

我将JSP文件放在下一个路径中:

/src/main/webapp/view/view.jsp
jax-rs resteasy illegalargumentexception
1个回答
0
投票

这看起来不是正确的方法。您通常会发送重定向响应。比如:

@Path("/showView")
public class WSService {

    @GET
    @Path("/redirect")
    @Produces("text/html")
    public Response redirect(@Conect UriInfo uriInfo) {
        return Response.status(Status.MOVED_PERMANENTLY).location(uriInfo.getBaseUriBuilder().path("/view/view.jsp")).build();
    }

}

在 RESTEasy 以及可能的其他实现中,

HttpServletResponse
HttpServletRequest
肯定是被包装的。

© www.soinside.com 2019 - 2024. All rights reserved.