What exactly is an HTTP Entity?

后端 未结 10 707
盖世英雄少女心
盖世英雄少女心 2020-11-29 16:31

Would someone please describe to me what exactly an HTTP entity is?

I am reading the HTTPClient documentation, but I do not really understand what t

10条回答
  •  一生所求
    2020-11-29 16:59

    HttpEntity is what you are going to pass in Request(with header) and what you are getting in Response. For Get Request we are passing simple String

     HttpHeaders headers = new HttpHeaders();
     headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
     HttpEntity entity = new HttpEntity(headers);
    

    For Post We are going to pass complete Entity Class

    public String createProducts(@RequestBody Product product) {
        HttpHeaders headers = new HttpHeaders();
        headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
        HttpEntity entity = new HttpEntity(product,headers);
    
        return restTemplate.exchange(
                 "http://localhost:8080/products", HttpMethod.POST, entity, String.class
               ).getBody();
    }
    

提交回复
热议问题