Could anyone tell me how to call post request from simple terminal client?
@POST
@Path("insertt")
public void insert(@QueryParam("id") Integer id,
@QueryParam("name") String name,
@QueryParam("lastname") String lastname,
@QueryParam("adress") String adress,
@QueryParam("city") String city ) {
Customer cust = new Customer();
cust.setCustomerId(id);
cust.setName(name);
cust.setLastNAme(lastname);
cust.setAddressline1(adress);
cust.setCity(city);
customerDAO.add( cust );
}
In client I do:
Client c = ClientBuilder.newClient();
WebTarget resource = c.target("http://localhost:8080/WebService/webresources/generic/insertt?id=100&name=test&lastname=test&adress=test&city=test");
//resource.request().post(); // This does not work
Because you're trying to send POST data @QueryParam
will not work because post data will be sent as request body and not as query param (that means not appended in the URL as you did). So you have to change your resource method as follows:
@POST
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
@Path("insertt")
public void insert(@FormParam("id") Integer id,
@FormParam("name") String name,
@FormParam("lastname") String lastname,
@FormParam("adress") String adress,
@FormParam("city") String city ) {
Customer cust = new Customer();
cust.setCustomerId(id);
...
customerDAO.add( cust );
}
And change your client as follows:
Client client = ClientBuilder.newClient();
WebTarget target = client.target("http://localhost:8080/WebService/webresources/generic").path("insertt");
Form form = new Form().param("id", "100").param("name", "test").param("lastname", "test").param("address", "test").param("city", "test");
Response response = target.request().post(Entity.form(form));
This example is just simulating an HTML form submission. If you want to send data as XML or JSON or any other form, you have to look at the JAX-RS documentation. There are lots of resources on the net; here are some example sites:
NOTE: The example is tested with Jersey 2.23 and Wildfly 8.2.1