简体   繁体   English

为什么我的球衣方法会被叫两次?

[英]Why does my method in jersey get called twice?

I have HomeService.java and when I go to the /test url it prints HomeService::test once, but when I go to the /play url it prints HomeService::play twice. 我有HomeService.java,当我去/test url时它打印HomeService::test一次,但是当我去/play url时它会打印HomeService::play两次。 How can I get my /play method to only be called once? 如何让我/play方法只调用一次?

The urls I go to are 我去的网址是

http://127.0.0.1:8080/Home/rest/main/test
http://127.0.0.1:8080/Home/rest/main/play

HomeService.java: HomeService.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 在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>

If more information is need let me know. 如果需要更多信息,请告诉我。

Edit: So I ran tcpdump and this was the output: 编辑:所以我运行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

and then again 再一次

........GET /Home/rest/main/play HTTP/1.1
........GET /Home/rest/main/play HTTP/1.1

It's because the client requested it twice. 这是因为客户要求了两次。 You may expect this behavior on media (audio/video) requests. 您可能期望在媒体(音频/视频)请求上出现此行为。 Most media players would test if the server supports range requests, so that it could be more efficiently buffered via multiple HTTP connections. 大多数媒体播放器会测试服务器是否支持范围请求,以便通过多个HTTP连接更有效地缓冲它。 If you look closer at the request headers, you may see Range and If-Range headers. 如果仔细查看请求标头,可能会看到RangeIf-Range标头。 If your server had supported it, then the client would send multiple range requests requesting smaller chunks of the media file starting and ending at the specified range. 如果您的服务器支持它,则客户端将发送多个范围请求,请求在指定范围内开始和结束的较小媒体文件块。 Also, if the client fast-forwards to a specific moment (eg 1 minute later), then the media player could just abort the request and send a new request requesting the range starting at exactly that position. 此外,如果客户端快进到特定时刻(例如1分钟后),则媒体播放器可以中止该请求并发送新请求,请求从该位置开始的范围。

You can't stop clients from requesting it multiple times. 您无法阻止客户多次请求它。 If your service doesn't support range requests, you'd probably better make it one. 如果您的服务不支持范围请求,您可能最好将其设置为一个。 The servletcontainer's builtin default servlet supports it. servletcontainer的内置默认servlet支持它。 So if you put the media file in public web content and let the client request it directly instead of via a web service, then it'll worry about that. 因此,如果您将媒体文件放在公共Web内容中并让客户端直接请求它而不是通过Web服务,那么它会担心这一点。

Note that this problem is completely unrelated to Jersey, although I have somewhat the impression that it's the wrong tool for the particular job of media streaming. 请注意,这个问题与Jersey完全无关,尽管我有点认为这是媒体流特定工作的错误工具。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM