Problem in the Java API Matrix example on the documentation

Hello,

I´m trying to run this matrix example provided on the documentation in Java (here):

Client client = ClientBuilder.newClient();
Entity<String> payload = Entity.json({"locations":[[9.70093,48.477473],[9.207916,49.153868],[37.573242,55.801281],[115.663757,38.106467]]});
Response response = client.target("https://api.openrouteservice.org/v2/matrix/driving-car")
  .request()
  .header("Authorization", "api_key")
  .header("Accept", "application/json, application/geo+json, application/gpx+xml, img/png; charset=utf-8")
  .header("Content-Type", "application/json; charset=utf-8")
  .post(payload);

System.out.println("status: " + response.getStatus());
System.out.println("headers: " + response.getHeaders());
System.out.println("body:" + response.readEntity(String.class));

However I’m getting an error on this line: Entity<String> payload = Entity.json({"locations":[[9.70093,48.477473],[9.207916,49.153868],[37.573242,55.801281],[115.663757,38.106467]]});

The error is this one:
Multiple markers at this line
- Syntax error, insert “;” to complete
LocalVariableDeclarationStatement
- The method json(T) in the type Entity is not applicable for the
arguments ()
- Syntax error on tokens, delete these tokens
- Syntax error, insert “)” to complete MethodInvocation

Since this is a straight up copy from the documentation and I’m relatively new to this matter, does anyone know how to solve it ?

Thanks

Please use proper (code) formatting, i.e. 3 backticks, it really hurts my brain to look at it otherwise.

The code on our documentation is auto-generated, we don’t write any of it. Maybe some of the backend folks will point out the mistake and we can override the code piece.

Hi,

without checking properly, just an idea:
The example is a <String> type Entity. But what is passed is a String followed by an array of floats/doubles. I think you need to either change the type or make the whole statement a String.

Hi @Pedro_Pereira,

First of all, please don’t ever expose your api_key or other people might use it!

I just tested this out shortly and @HendrikLeuschner is right.

You need to pass:

        Entity<String> payload = Entity.json(
                "{\"locations\":[[9.70093,48.477473],[9.207916,49.153868],[37.573242,55.801281],[115.663757,38.106467]]}"
        );

and everything will work without a problem.

Please read 403 Forbidden - Daily quota reached or API key unauthorized
for further info on the escape strings.

Best regards

Thank you all for your answers. The problem is solved following the solution provided by @amandus .