I am making the following curl request successfully to my API:
curl -v -X GET -H "Content-Type: application/json" -d {'"query":"some text","mode":"0"'} http://host.domain.abc.com:23423/api/start-trial-api/
I would like to know how can i make this request from inside JAVA code. I have tried searching through Google and stack overflow for the solution. All i have found is how to send data through a query string or how to send JSON data through a POST request.
Thanks
Spring's RESTTemplate is also useful for sending all REST requests i.e. GET , PUT , POST , DELETE
By using Spring REST template, You can pass JSON request with POST like below,
You can pass JSON representation serialized into java Object using JSON serializer such as jackson
Using below code you should be able to invoke any rest API.
Make a class called RestClient.java which will have method for get and post
invoke the get and post method
Make your own Request and response class.
for json to java and java to json use below class
I wrote the RestClient.java class , to reuse the get and post methods. similarly you can write other methods like put and delete...
Hope it will help you.
You could use the Jersey client library, if your project is a Maven one just include in your pom.xml the jersey-client and jersey-json artifacts from the com.sun.jersey group id. To connect to a web service you need a WebResource object:
To make a call sending a payload you can model the payload as a POJO, i.e.
and then call the call using the resource object:
where ResultType is the Java mapped return type of the called service, in case it's a JSON object, otherwise you can remove the accept call and just put String.class as the get parameter and assign the return value to a plain string.