1

Related to this post, JAX-RS Post multiple objects

If my resource endpoint takes in List<String>

@POST @Path("test") @Consumes(MediaType.APPLICATION_JSON) public void test(List<String> jsonStrings)

How to call this resource end point using curl (command line)

curl -X POST -H "Content-Type: application/json" http://localhost:port/path/to/endpoint/test -d '{} {}'

The above command does not work

Community
  • 1
  • 1
Gnana
  • 614
  • 1
  • 7
  • 18

1 Answers1

0

'{} {}' isn't valid JSON. A Java mapping terms, a java.util.List would be a JSON array ([]). So a valid input would be -d "[ \"hello\", \"world\" ]". Since your list is just a of a simple type String, you don't need any curly brackets {}, and those signify JSON objects.

This is just simple Strings. If you want an array of object, then you would need something like "[{\"word\" : \"hello\"},{\"word\": \"world\"}]", where the list List type would be something like

public class Hello {
    private String word;
    public String getWord() { return word; }
    public void setWord(String word) { this.word = word; }
}

So you would use List<Hello>. Also you need to make sure you have a JSON provider registered with the application (for POJO mapping). Some providers will auto-register, others won't If you need help with this, then please provide the JAX-RS implementation you are using, and whether or not you are using Maven.

Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720