How do I submit a post request with an empty body with a Jersey 2 client?
final MyClass result = ClientBuilder.newClient() .target("http://localhost:8080") .path("path") .queryParam("key", "value") .request(APPLICATION_JSON) .post(What to fill in here if the body should be left empty??, MyClass.class);
Update: this works:
final MyClass result = ClientBuilder .newBuilder().register(JacksonFeature).build() .target("http://localhost:8080") .path("path") .queryParam("key", "value") .request(APPLICATION_JSON) .post(null, MyClass.class);
Jersey is an open source framework for developing RESTFul Web Services. It also has great inbuilt client capabilities. In this quick tutorial, we will explore the creation of JAX-RS client using Jersey 2.
Jersey is a REST-client, featuring full JAX-RS implementation, neat fluent API and a powerfull filter stack. Apache Http Client is a HTTP-client, perfect in managing low-level details like timeouts, complex proxy routes and connection polling. They act on a different levels of your protocol stack.
Jersey ClientBuilder. JAX-RS Client API is a designed to allow fluent programming model. To create jersey client follow these steps – Use ClientBuilder. newClient() static method.
I can't find this in the doc's anywhere, but I believe you can use null
to get an empty body:
final MyClass result = ClientBuilder.newClient() .target("http://localhost:8080") .path("path") .queryParam("key", "value") .request(APPLICATION_JSON) .post(Entity.json(null), MyClass.class)
I found that this worked for me:
Response r = client .target(url) .path(path) .queryParam(name, value) .request() .put(Entity.json(""));
Pass an empty string, not a null value.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With