gpt4 book ai didi

java - Websphere App 8 : DataContentHandler requires String object, 被赋予类型为 com.sun.jersey.api.view.Viewable 类的对象

转载 作者:行者123 更新时间:2023-12-01 13:23:51 27 4
gpt4 key购买 nike

我有以下代码

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();
}
}

应用程序结构是

webapp
->jsp
->WEB-INF

我得到的异常(exception)是

Error 500: javax.servlet.ServletException: java.io.IOException: "text/html" DataContentHandler requires String object, was given object of type class com.sun.jersey.api.view.Viewable

但这似乎与我能找到的所有示例相匹配。

最佳答案

这段代码对我有用:

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

服务器是 Glassfish 3.1.1 和 Jersey 1.17

也许 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的路径;)

关于java - Websphere App 8 : DataContentHandler requires String object, 被赋予类型为 com.sun.jersey.api.view.Viewable 类的对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21891920/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com