我有 HomeService.java,当我转到/test
url 时它打印HomeService::test
一次,但是当我转到/play
url 时它打印HomeService::play
两次。我怎样才能让我的/play
方法只被调用一次?
我去的网址是
http://127.0.0.1:8080/Home/rest/main/test
http://127.0.0.1:8080/Home/rest/main/play
家庭服务.java:
@Path("/main")
public class HomeService
{
@GET
@Path("/test")
@Produces("text/plain")
public String test()
{
System.out.println("HomeService::test");
return "Running...";
}
@GET
@Path("/play")
@Produces("video/mpeg")
public StreamingOutput play()
{
System.out.println("HomeService::play");
return new StreamingOutput()
{
@Override
public void write(java.io.OutputStream outputStream) {}
}
}
Web.xml
<?xml version="1.0" encoding="UTF-8"?><web-app id="WebApp_ID" version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>Home</display-name>
<servlet>
<servlet-name>jersey-servlet</servlet-name>
<servlet-class>
com.sun.jersey.spi.container.servlet.ServletContainer
</servlet-class>
<init-param>
<param-name>com.sun.jersey.config.property.packages</param-name>
<param-value>com.home</param-value>
</init-param>
<init-param>
<param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
<param-value>true</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>jersey-servlet</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
</web-app>
如果需要更多信息,请告诉我。
编辑:所以我运行了 tcpdump,这是输出:
# ./tcpdump -s 128 -A -v -i any port 8080|grep 'play'
tcpdump: listening on any, link-type LINUX_SLL (Linux cooked), capture size 128 bytes
...u...uGET /Home/rest/main/play HTTP/1.1
.. ... .GET /Home/rest/main/play HTTP/1.1
然后再一次
........GET /Home/rest/main/play HTTP/1.1
........GET /Home/rest/main/play HTTP/1.1