Why does SparkJava not process the second request on the same connection?

358 Views Asked by At

I have written a small server with a REST-API using SparkJava. I try to query the REST-API with an Apache Httpclient. With this client, I open a connection and send a first request to the server and receive a response. Then I reuse the same connection to send a second request to the server. The request is transmitted but the server does not process it. Does anyone know, what I am doing wrong?

Here a minimal working example:

Maven dependencies:

        <dependency>
            <groupId>com.sparkjava</groupId>
            <artifactId>spark-core</artifactId>
            <version>2.9.3</version>
        </dependency>
        <dependency>
            <groupId>org.apache.httpcomponents.client5</groupId>
            <artifactId>httpclient5</artifactId>
            <version>5.0.3</version>
        </dependency>

Server class:

package minimal;

import spark.Spark;

public class Server {

  public static void main(String[] args) {
    Spark.post("/a", (req, resp) -> {
          resp.status(204);
          return "";
        });
    Spark.post("/b", (req, resp) -> {
          resp.status(204);
          return "";
        });
    Spark.before((req, res) -> {
          System.out.println("Before: Request from " + req.ip() + " received " + req.pathInfo());
        });
    Spark.after((req, res) -> {
          System.out.println("After: Request from " + req.ip() + " received " + req.pathInfo());
        });
  }
}

Client class:

package minimal;

import java.io.IOException;

import org.apache.hc.client5.http.classic.methods.HttpPost;
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
import org.apache.hc.client5.http.impl.classic.CloseableHttpResponse;
import org.apache.hc.client5.http.impl.classic.HttpClients;

public class Client {

  public static void main(String[] args) throws IOException {
    try (CloseableHttpClient httpclient = HttpClients.createDefault()) {
      HttpPost httpPost1 = new HttpPost("http://localhost:4567/a");
      try (CloseableHttpResponse response1 = httpclient.execute(httpPost1)) {
        System.out.println(response1.getCode() + " " + response1.getReasonPhrase());
      }

      HttpPost httpPost2 = new HttpPost("http://localhost:4567/b");
      try (CloseableHttpResponse response2 = httpclient.execute(httpPost2)) {
        System.out.println(response2.getCode() + " " + response2.getReasonPhrase());
      }
    }
  }
}

The server output on the console:

Before: Request from 127.0.0.1 received /a
After: Request from 127.0.0.1 received /a

Here the shortened output of a tcpdump:

14:52:15.210468 IP localhost.44020 > localhost.4567:
POST /a HTTP/1.1
Accept-Encoding: gzip, x-gzip, deflate
Host: localhost:4567
Connection: keep-alive
User-Agent: Apache-HttpClient/5.0.3 (Java/1.8.0_282)

14:52:15.271563 IP localhost.4567 > localhost.44020:
HTTP/1.1 204 No Content
Date: Tue, 27 Apr 2021 12:52:15 GMT
Content-Type: text/html;charset=utf-8
Server: Jetty(9.4.26.v20200117)

14:52:15.277376 IP localhost.44020 > localhost.4567:
POST /b HTTP/1.1
Accept-Encoding: gzip, x-gzip, deflate
Host: localhost:4567
Connection: keep-alive
User-Agent: Apache-HttpClient/5.0.3 (Java/1.8.0_282)

Thereafter no response of the Server was recorded anymore.

2

There are 2 best solutions below

5
On BEST ANSWER

The reason for the missing processing of the SparkJava server was the following additional maven dependency I had in the project:

        <dependency>
            <groupId>org.apache.spark</groupId>
            <artifactId>spark-core_2.11</artifactId>
            <version>2.4.0.7.1.1.0-565</version>
        </dependency>

After removing this dependency, the SparkJava server works as expected.

6
On

Here's the client sample please try it out and see if it works for you. I tested it and it worked fine.

import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;

import java.io.IOException;

public class T1 {

   static void runPost(CloseableHttpClient c,String s)
    {
            HttpPost httpPost1 = new HttpPost(s);
            try(CloseableHttpResponse response1 = c.execute(httpPost1)) {
                System.out.println(Thread.currentThread().getName() + ": " +
                         response1.getStatusLine().getStatusCode() + " " +
                         response1.getStatusLine().getReasonPhrase());
            } catch (Exception e) {
                e.printStackTrace();
            }
    }

    public static void main(String[] args) throws IOException {

        try(CloseableHttpClient httpclient = HttpClients.createDefault()) {
            T1.runPost(httpclient, "http://localhost:4567/a");
            T1.runPost(httpclient, "http://localhost:4567/b");
        }
        System.exit(0);
    }
}