JAVA 利用org.springframework.http 发送HTTP 请求

            RestTemplate client = new RestTemplate();
            HttpHeaders headers = new HttpHeaders();
            headers.setContentType(MediaType.APPLICATION_JSON_UTF8);
            HttpEntity<MultiValueMap<String, String>> requestEntity = new HttpEntity(requestBody, headers);
            ResponseEntity<Object> exchange = null;
            try{
                exchange = client.exchange(host+url, method, requestEntity, Object.class);
                System.out.println( "--------------requestBody-------------");         
                System.out.println(JSON.toJSONString(requestBody));             
                System.out.println( "-------------responseBody-------------");           
                System.out.println(JSON.toJSONString(exchange.getBody()));  
            }catch (HttpClientErrorException e){
                System.out.println( "--------------requestBody-------------");              
                System.out.println(JSON.toJSONString(requestBody));            
                System.out.println(e.getMessage());
                System.out.println(e.getStatusText());
                System.out.println( "-------------responseBody-------------");           
                System.out.println( e.getResponseBodyAsString());                
                e.printStackTrace();
            }catch (Exception e){
                System.out.println(e.getMessage());               
            }

 

requestBody传进来Object 

依赖包为

import org.springframework.web.client.HttpClientErrorException;
import org.springframework.web.client.RestTemplate;
import org.springframework.http.*;
import com.alibaba.fastjson.JSON;

 

 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Boot 提供了一种简单的方式来发送 HTTP 请求,通常使用它的内置 `RestTemplate` 或者第三方库如 `HttpURLConnection`、`HttpClient` 或 `Retrofit`。如果你想通过命令行的方式模拟一个 `curl` 请求,可以利用 Spring Boot 的 `RestTemplate` 来执行。 以下是一个简单的示例,展示如何使用 `RestTemplate` 发送 GET 请求: ```java import org.springframework.http.HttpEntity; import org.springframework.http.ResponseEntity; import org.springframework.web.client.RestTemplate; public class CurlExample { public static void main(String[] args) { RestTemplate restTemplate = new RestTemplate(); String apiUrl = "https://api.example.com/data"; try { // 发送 GET 请求 ResponseEntity<String> response = restTemplate.getForEntity(apiUrl, String.class); // 打印响应状态码和内容 System.out.println("Status Code: " + response.getStatusCode()); System.out.println("Response Body: " + response.getBody()); } catch (Exception e) { e.printStackTrace(); } } } ``` 如果你想通过 `curl` 命令模拟这个请求,你可以将上述代码中 API URL 替换为你的目标地址,并在命令行运行类似下面的命令: ```bash curl -X GET "https://api.example.com/data" --header "accept: application/json" ``` 这里 `-X GET` 指定了请求方法,`--header "accept: application/json"` 设置了接受的响应类型。如果 Spring Boot 中处理的是 JSON 数据,你可能需要匹配相应的头信息。 相关问题-- 1. Spring Boot 中如何使用 `RestTemplate` 发送其他HTTP方法(如POST、PUT)? 2. 如何在Spring Boot应用中设置自定义的请求头或认证信息? 3. 如果API返回的是XML或HTML格式,`RestTemplate` 需要做哪些调整?

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值