2

Problem Statement

I am new to Spring/Rest Applications and I have data in an Object.

Now,I need to pass this data to an API.

Below is the sample curl Attached for a single record-

curl --request POST \
  --url http://eventapi-dev.wynk.in/tv/events/v1/event \
  --header 'cache-control: no-cache' \
  --header 'content-type: application/json' \
  --header 'postman-token: 67f73c14-791f-62fe-2b5a-179ba04f67ba' \
  --data '{"name":"hotel california", "createdAt":1505727060471, "steamUrl":"https://www.youtube.com/watch?v=lHje9w7Ev4U"}'

The response I got after Hitting curl url in Terminal is Ok

Can I anyone guide me how to write the Code in Java.

Jalaj Chawla
  • 189
  • 1
  • 3
  • 18
  • 1
    Simple java? familiarize yourself with java.net.* APIs. – Optional Sep 28 '17 at 03:34
  • Please read [Why is “Can someone help me?” not an actual question?](https://meta.stackoverflow.com/questions/284236/why-is-can-someone-help-me-not-an-actual-question) before attempting to ask more questions. –  Sep 28 '17 at 04:03

3 Answers3

3

You can using okhttp (https://github.com/square/okhttp) to call this api. Example:

OkHttpClient client = new OkHttpClient();

MediaType mediaType = MediaType.parse("application/json");
RequestBody body = RequestBody.create(mediaType, "{\n\t\"name\":\"hotel california\", \n\t\"createdAt\":1505727060471, \n\t\"steamUrl\":\"https://www.youtube.com/watch?v=lHje9w7Ev4U\"\n}");
Request request = new Request.Builder()
  .url("http://eventapi-dev.wynk.in/tv/events/v1/event")
  .post(body)
  .addHeader("content-type", "application/json")
  .addHeader("cache-control", "no-cache")
  .addHeader("postman-token", "08af0720-79cc-ff3d-2a7d-f208202e5ec0")
  .build();

Response response = client.newCall(request).execute();
  • @Tran Duc Hung How to pass my object to this. – Jalaj Chawla Sep 28 '17 at 03:42
  • 2
    @JalajChawla: I guess answer is pretty clear that you need to pass your JSON on line - `RequestBody body = RequestBody.create(..., ...)` or your question something else ? Alternately, you can use a JSON API like Jackson etc to represent your POJOS as JSON objects and then convert to strings etc. – Sabir Khan Sep 28 '17 at 04:19
  • 1
    @JalajChawla u can use gson – Trần Đức Hùng Sep 28 '17 at 04:27
2

You have to use something similar as described here maily bu using HttpURLConnection & URL .

There you notice for post scenario that JSON data is passed as String

Then you can follow this question also to know few more answers using that API.

You can also use Apache HttpClient and browse examples on their site.

Apache HttpClient examples are here too.

I am not sure if I should just copy - paste relevant code samples from those websites to this answer ( for the sake of completeness ) but idea is very simply that you have to find an API that helps you in building and executing a REST request.

Another API is listed in answer by - Trần Đức Hùng and so we have numerous other Java APIs available in the market.

Sabir Khan
  • 9,826
  • 7
  • 45
  • 98
-2

Url will be your end-point. It means you need to write a controller that can response your request, inside of your request there is some headers as you see. Those headers are for logging in and telling spring that you are sending json text. Also if you check your request it is "POST" so you also need to inform your controller method about this. It is good practice to catch all data with a model.

So your task should be like this.

  1. Create controller that can response to your url.
  2. Inform your controller method data is in Json format.
  3. Inform your controller method it needs to wait for "POST" request.
  4. Parse data to a model.

Let's try to do with code.

@RequestMapping(value = events.EVENT, method = RequestMethod.POST, consumes = {
        MediaType.APPLICATION_JSON_VALUE }, produces = { MediaType.APPLICATION_JSON_VALUE })
public Event eventAction(@RequestBody Event event){

}

In your case you need to define what is event. Class should be like this.

public class Quota implements Serializable{
 private String name;
 private Date createAt;
 private String url;

// create getter setter
}

That is all now you are able to response this request. Inside of your controller method you can do your business logic.

Sahin Yanlık
  • 1,171
  • 2
  • 11
  • 21