How to pass post json Object Data to an api using java/Spring/rest

This question already has an answer here:

  • How to POST JSON data with Curl from Terminal/Commandline to Test Spring REST? 18 answers

  • 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, "{nt"name":"hotel california", nt"createdAt":1505727060471, nt"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();
    

    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.


    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.

  • Create controller that can response to your url.
  • Inform your controller method data is in Json format.
  • Inform your controller method it needs to wait for "POST" request.
  • 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.

    链接地址: http://www.djcxy.com/p/8546.html

    上一篇: 验证请求时,Laravel中的REST API

    下一篇: 如何将post json Object Data传递给使用java / Spring / rest的api