我正在做一个关于使用泽西暂停asynch请求的小测试,但我得到一个错误。让我告诉你我想要做什么,以及服务器启动和运行的方式只是这个请求失败了:
Path("/json/metallica")
public class JSONService {
@GET
@Path("/test")
@Produces(MediaType.APPLICATION_JSON)
public void getAsynchHealthyTracks(@Suspended AsyncResponse asyncResponse){
HealthService.getInstance().getHealththyTracks(asyncResponse);
}
}
当我在tomcat上从我的本地主机调用它时,我收到以下错误:
HTTP Status 500 - javax.ws.rs.ProcessingException: Attempt to suspend a connection of an asynchronous request failed in the underlying container.
type Exception report
message javax.ws.rs.ProcessingException: Attempt to suspend a connection of an asynchronous request failed in the underlying container.
description The server encountered an internal error that prevented it from fulfilling this request.
exception
javax.servlet.ServletException: javax.ws.rs.ProcessingException: Attempt to suspend a connection of an asynchronous request failed in the underlying container.
org.glassfish.jersey.servlet.WebComponent.service(WebComponent.java:391)
org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:381)
org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:344)
org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:221)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:51)
root cause
javax.ws.rs.ProcessingException: Attempt to suspend a connection of an asynchronous request failed in the underlying container.
org.glassfish.jersey.server.model.ResourceMethodInvoker.apply(ResourceMethodInvoker.java:312)
org.glassfish.jersey.server.model.ResourceMethodInvoker.apply(ResourceMethodInvoker.java:103)
org.glassfish.jersey.server.ServerRuntime$1.run(ServerRuntime.java:271)
org.glassfish.jersey.internal.Errors$1.call(Errors.java:271)
org.glassfish.jersey.internal.Errors$1.call(Errors.java:267)
org.glassfish.jersey.internal.Errors.process(Errors.java:315)
org.glassfish.jersey.internal.Errors.process(Errors.java:297)
org.glassfish.jersey.internal.Errors.process(Errors.java:267)
org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:297)
org.glassfish.jersey.server.ServerRuntime.process(ServerRuntime.java:254)
org.glassfish.jersey.server.ApplicationHandler.handle(ApplicationHandler.java:1028)
org.glassfish.jersey.servlet.WebComponent.service(WebComponent.java:372)
org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:381)
org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:344)
org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:221)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:51)
我的web.xml文件中已经有<async-supported>true</async-supported>
,这是web.xml文件的内容:
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<display-name>Archetype Created Web Application</display-name>
<servlet>
<servlet-name>jersey-servlet</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>com.example.ema,com.fasterxml.jackson.jaxrs.json</param-value>
<async-supported>true</async-supported>
<load-on-startup>1</load-on-startup>
</init-param>
<init-param>
<param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
<param-value>true</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>jersey-servlet</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
</web-app>
健康服务本身只是构建一个pojo对象,应该将其作为json发回。无论如何,这是服务:
public class HealthService {
private static HealthService instance = null;
protected HealthService() {
// Exists only to defeat instantiation.
}
public static HealthService getInstance() {
if(instance == null) {
instance = new HealthService();
}
return instance;
}
public void getHealthyTracks(AsyncResponse asyncResponse){
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
Track track = new Track();
track.setSinger("micheal jackson");
track.setTitle("thriller");
asyncResponse.resume(track);
}
}
我提出了一个破发点,甚至没有达到最初的球衣要求。服务器停止了。我的最终目标是睡几秒钟然后发回回复。
更新:这是现在的web.xml照片:
答案 0 :(得分:0)
我想出了这个问题。标签“true”不应该放在init params部分。它应该放在web.xml文件中的servlet标记内。我的web.xml现在看起来像这样:
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:web="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<display-name>Archetype Created Web Application</display-name>
<servlet>
<servlet-name>jersey-servlet</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<async-supported>true</async-supported>
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>com.example.ema,com.fasterxml.jackson.jaxrs.json</param-value>
<load-on-startup>1</load-on-startup>
</init-param>
<init-param>
<param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
<param-value>true</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>jersey-servlet</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
</web-app>
答案 1 :(得分:0)
需要对web.xml内的Servlet配置进行小的更改
<servlet>
<servlet-name>jersey-servlet</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>com.javapapers.webservices.rest.jersey</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
<async-supported>true</async-supported>
</servlet>
观察“异步支持”元素的位置/嵌套,它已作为子元素定位在“ servlet”元素下。