Why is my method called twice in jersey?

I have HomeService.java and when I go to the url /test

it prints HomeService::test

once, but when I go to the url /play

it prints twice HomeService::play

. How can I get the method /play

just one time?

The URLs I am accessing are

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

      

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

<?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 you need more information, let me know.

Edit: So, I ran tcpdump and this was the output:

# ./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

      

+3


source to share


1 answer


This is because the client has requested it twice. You can expect this behavior on media (audio / video) requests. Most media players will check if the server supports range requests so that it can be more efficiently buffered across multiple HTTP connections. If you look closer to the request headers, you can see Range

and If-Range

. If your server supported it, the client made multiple range requests requesting smaller chunks of the media, starting and ending in the specified range. In addition, if the client quickly jumps to a certain point (for example, after 1 minute), the media player can simply abort the request and send a new request with a request for a range starting from that position.

You cannot stop customer requests multiple times. If your service doesn't support range requests, you are probably better off doing this. Servletcontainer The default built-in servlet supports it. So if you put the media in public web content and allow the client to request it directly and not through the web service, then it will worry about it.



Please note that this issue is completely unrelated to Jersey, although I am under the impression that this is the wrong tool for the particular media streaming job.

+4


source







All Articles