RestTemplate请求

复制代码
package com.istrong.guarantee.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.web.client.RestTemplate;


/**
 * @program: sm_guarantee_platform
 * @ClassName AppConfig
 * @description:注册bean、系统配置注解开启
 * @author: 黄涛
 * @create: 2025-01-10 16:03
 * @Version 1.0
 **/
@Configuration
public class AppConfig {

    /**
     * 注册restTemplate的bean
     * @return
     */
    @Bean
    public RestTemplate restTemplate() {
        return new RestTemplate();
    }
    
}
复制代码
复制代码
package com.istrong.guarantee.component;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.springframework.http.*;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import java.util.Map;
import java.util.stream.Collectors;

/**
 * @program: sm_guarantee_platform
 * @ClassName ThirdPartyService
 * @description:HTTP访问的模板类
 * @author: 黄涛
 * @create: 2025-01-10 15:41
 * @Version 1.0
 **/
@Component
public final class RestTemplateService {

    private final RestTemplate restTemplate;
    public RestTemplateService(RestTemplate restTemplate){
        this.restTemplate = restTemplate;
    }


    /**
     * post,application/json
     * @param url
     * @param param
     * @return
     */
    public final Map post(String url, Map<String, Object> param) {
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        HttpEntity<Map<String, Object>> entity = new HttpEntity<>(param, headers);
        ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.POST, entity, String.class);
        return JSONObject.parseObject(response.getBody());
    }


    /**
     * get,application/x-www-form-urlencoded
     * @param url
     * @return
     */
    public final Map get(String url) {
        ResponseEntity<String> response = restTemplate.getForEntity(url, String.class);
        return JSONObject.parseObject(response.getBody());
    }


    /**
     * get,application/x-www-form-urlencoded
     * @param url
     * @param map
     * @return
     */
    public final Map get(String url,Map<String,String> map) {
        url += "?" + map.entrySet().stream().map(entry -> entry.getKey() + "=" + entry.getValue()).collect(Collectors.joining("&"));
        ResponseEntity<String> responseEntity = restTemplate.getForEntity(url, String.class);
        Map ret_map = JSON.parseObject(responseEntity.getBody());
        return ret_map;
    }
}
复制代码

 

posted on   五官一体即忢  阅读(3)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· DeepSeek 开源周回顾「GitHub 热点速览」
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

导航

统计

点击右上角即可分享
微信分享提示