Websphere App 8:DataContentHandler需要String对象,给出了类com.sun.jersey.api.view.Viewable类型的对象

时间:2014-02-19 20:39:26

标签: java jax-rs websphere-8 apache-wink

我有以下代码

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.xml.rpc.ServiceException;
...
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.sun.jersey.api.view.Viewable;
@Path("/test")
public class TestResource {
@GET
public Response getMessage( @Context final HttpServletRequest request,
        @Context final HttpServletResponse response) throws MalformedURLException, RemoteException, ServiceException, StdAddFault{
        ...
        //Works fine
//      return Response.ok(ret.toString()).type(MediaType.TEXT_PLAIN).build();
        //Throws Exception
    return Response.ok(new Viewable("/app/jsp/test.jsp")).type(MediaType.TEXT_HTML).build();
}
}

App结构

webapp
->jsp
->WEB-INF

我得到的例外是

  

错误500:javax.servlet.ServletException:java.io.IOException:“text / html”DataContentHandler需要String对象,给出了类com.sun.jersey.api.view.Viewable

类型的对象

但这似乎与我能找到的所有例子相符。

1 个答案:

答案 0 :(得分:1)

此代码适用于我:

import com.sun.jersey.api.view.Viewable;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import java.net.MalformedURLException;

@Path("/test")
public class TestResource {
    @GET
    public Response getMessage( @Context final HttpServletRequest request,
                                @Context final HttpServletResponse response) throws MalformedURLException{
        return Response.ok(new Viewable("/dummy.jsp")).type(MediaType.TEXT_HTML).build();
    }
}

在src / main / webapp / WEB-INF / jsp / dummy.jsp中使用此内容

Dummy response

服务器是带有Jersey 1.17的Glassfish 3.1.1

也许是一个糟糕的web.xml配置或可查看路径? 我的JSPTemplatesBasePath

<init-param>
    <param-name>com.sun.jersey.config.property.JSPTemplatesBasePath</param-name>
    <param-value>/WEB-INF/jsp</param-value>
</init-param>

[编辑] 我的web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
    version="3.0">
    <session-config>
        <tracking-mode>COOKIE</tracking-mode>
    </session-config>
    <filter>
        <filter-name>jersey</filter-name>
        <filter-class>com.sun.jersey.spi.container.servlet.ServletContainer</filter-class>
        <init-param>
            <param-name>com.sun.jersey.config.property.packages</param-name>
            <param-value>com.ezakus.</param-value>
        </init-param>
        <init-param>
            <param-name>com.sun.jersey.spi.container.ContainerResponseFilters</param-name>
            <param-value>com.ezakus.api.web.security.ResponseCorsFilter</param-value>
        </init-param>
        <init-param>
            <param-name>com.sun.jersey.config.property.JSPTemplatesBasePath</param-name>
            <param-value>/WEB-INF/jsp</param-value>
        </init-param>
        <init-param>
            <param-name>com.sun.jersey.config.property.WebPageContentRegex</param-name>
            <param-value>/(resources|js|css|img)/.*</param-value>
        </init-param>
        <init-param>
            <param-name>com.sun.jersey.config.feature.DisableWADL</param-name>
            <param-value>true</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>jersey</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    <error-page>
        <error-code>500</error-code>
        <location>/WEB-INF/jsp/500.jsp</location>
    </error-page>
    <error-page>
        <error-code>503</error-code>
        <location>/WEB-INF/jsp/503.jsp</location>
    </error-page>
    <error-page>
        <error-code>404</error-code>
        <location>/WEB-INF/jsp/404.jsp</location>
    </error-page>
    <error-page>
        <error-code>400</error-code>
        <location>/WEB-INF/jsp/400.jsp</location>
    </error-page>
</web-app>

在Viewable中,您必须在没有JSPTemplatesBasePath的情况下放置路径;)