NC65采用Restful调用第三方接口示例

本文介绍了一个使用SpringBoot的RESTful客户端工具类,通过Java代码实现POST请求发送JSON数据到指定URL,自动处理HTTP头部信息,如Content-Type和Accept,同时使用UUID生成唯一标识符作为认证令牌。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

import java.util.UUID;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.web.client.RestTemplate;


public class RestClientUtils {

  /**

    *url :接口地址

    * jsonObj : json字符串

   **/

    public static String doPost(String url, String jsonObj) throws Exception {
        RestTemplate template = new RestTemplate();

        HttpHeaders headers = new HttpHeaders();

       // 设置请求参数类型为 application/json; charset=UTF-8
        MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");
        headers.setContentType(type);
        headers.add("Accept", MediaType.APPLICATION_JSON.toString());

        HttpEntity<String> formEntity = new HttpEntity<String>(jsonObj.toString(), headers);
        headers.add("X-Auth-Token", UUID.randomUUID().toString());
         url="";
        String result = template.postForObject(url, formEntity, String.class);
        return result;
    }

}
 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值