RETemplate 封装

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.thunisoft.t3.sscyr.template.impl.GetRETemplateImpl;
import com.thunisoft.t3.sscyr.template.impl.PatchRETemplateImpl;
import com.thunisoft.t3.sscyr.template.impl.PostRETemplateImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service
public final class RETemplate {

    @Autowired
    private PostRETemplateImpl postService;

    @Autowired
    private GetRETemplateImpl getService;

    @Autowired
    private PatchRETemplateImpl patchService;

    private RETemplate () {
    }

    public String Post (String paramType, String url, Object param, Map<String, String> headers) {
        String response = null;
        switch (paramType) {
            case ParamTypeConst.STRING:
                response = postService.postTemplate(url, param.toString (), headers);
                break;
            case ParamTypeConst.Map:
                response = postService.postTemplate(url, JSONObject.parseObject (JSONObject.toJSONString (param), HashMap.class), headers);
                break;
            case ParamTypeConst.JSON:
                response = postService.postTemplate(url, JSONObject.parseObject (JSONObject.toJSONString (param)), headers);
                break;
            case ParamTypeConst.XML:
                response = postService.postTemplate(url, param.toString ());
                break;
            case ParamTypeConst.JSONSTRING:
                response = postService.postTemplate(url, JSON.toJSONString (param), headers);
                break;
            case ParamTypeConst.LIST:
                response = postService.postTemplate(url, (List) param, headers);
                break;
            default:
                break;
        }
        return response;
    }

    public String Get (String paramType, String url, Object param, Map<String, String> headers) {
        String response = null;
        switch (paramType) {
            case ParamTypeConst.NULL:
                response = getService.getTemplate(url, headers);
                break;
            case ParamTypeConst.Map:
                response = getService.getTemplate(url, JSONObject.parseObject (JSONObject.toJSONString (param), Map.class), headers);
                break;
            default:
                break;
        }
        return response;
    }

    public String Patch (String paramType, String url, Object param, Map<String, String> headers) {
        String response = null;
        switch (paramType) {
            case ParamTypeConst.STRING:
                response = patchService.patchTemplate(url, param.toString (), headers);
                break;
            case ParamTypeConst.JSON:
                response = patchService.patchTemplate(url, JSONObject.parseObject (JSONObject.toJSONString (param)), headers);
                break;
            default:
                break;
        }
        return response;
    }
}
=============================================================================================================================


import com.alibaba.fastjson.JSON;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public interface PostRETemplate {

    String postTemplate(String url, HashMap param, Map<String, String> headers);

    String postTemplate(String url, JSON param, Map<String, String> headers);

    String postTemplate(String url, String param, Map<String, String> headers);

    String postTemplate(String url, List param, Map<String, String> headers);

    String postTemplate(String url, String param);
}

======================================================================================================

import com.alibaba.fastjson.JSON;

import java.util.Map;

public interface PatchRETemplate {
    String patchTemplate(String url, JSON param, Map<String, String> headers);

    String patchTemplate(String url, String param, Map<String, String> headers);
}

======================================================================================================

public final class ParamTypeConst {

    /**
     * 私有构造函数
     */
    private ParamTypeConst() {}

    public static final  String STRING = "string";

    public static final  String JSON = "json";

    public static final  String JSONSTRING = "jsonstring";

    public static final  String LIST = "list";

    public static final  String Map = "map";

    public static final  String XML = "xml";

    public static final String NULL = "null";

}

======================================================================================================

import java.util.Map;

public interface GetRETemplate {

    String getTemplate(String url, Map<String, String> headers);

    String getTemplate(String url, Map<String, Object> paramMap, Map<String, String> headers);
}
}

======================================================================================================

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.thunisoft.t3.sscyr.template.PostRETemplate;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class PostRETemplateImpl implements PostRETemplate {

    @Autowired
    private RestTemplate restTemplate;

    @SneakyThrows
    @Override
    public String postTemplate(String url, HashMap param, Map<String, String> headers) {

        // 设置头部信息
        HttpHeaders requestHeaders = new HttpHeaders();
        if (headers != null) {
            for (Map.Entry<String, String> header : headers.entrySet()) {
                requestHeaders.add(header.getKey(), header.getValue());
            }
        }
        HttpEntity<String> requestEntity = new HttpEntity<String>(null, requestHeaders);
        ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.POST, requestEntity, String.class,param);
        return response.getBody();
    }

    @Override
    public String postTemplate(String url, JSON param, Map<String, String> headers) {
        // 设置头部信息
        HttpHeaders requestHeaders = new HttpHeaders();
        if (headers != null) {
            for (Map.Entry<String, String> header : headers.entrySet()) {
                requestHeaders.add(header.getKey(), header.getValue());
            }
        }
        HttpEntity<String> requestEntity = new HttpEntity<String>(null, requestHeaders);
        ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.POST, requestEntity, String.class,param);
        return response.getBody();
    }

    @Override
    public String postTemplate(String url, String param, Map<String, String> headers) {
        // 设置头部信息
        HttpHeaders requestHeaders = new HttpHeaders();
        requestHeaders.add("Accept", "application/json");
        requestHeaders.add("Content-Type", "application/json");

        if (headers != null) {
            for (Map.Entry<String, String> header : headers.entrySet()) {
                requestHeaders.add(header.getKey(), header.getValue());
            }
        }

        HttpEntity<String> requestEntity = new HttpEntity<String>(JSON.parseObject(param).toJSONString(), requestHeaders);
        ResponseEntity<String> response = restTemplate.postForEntity(url, requestEntity, String.class);
        //ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.POST, requestEntity, String.class);
        return response.getBody();
    }
    @Override
    public String postTemplate(String url, String param) {
        // 设置头部信息
        HttpHeaders requestHeaders = new HttpHeaders();
        requestHeaders.add("Accept", "text/xml");
        requestHeaders.add("Accept", "application/json");
        requestHeaders.add("Content-Type", "text/xml");
        HttpEntity<String> requestEntity = new HttpEntity<String>(param, requestHeaders);
        return restTemplate.postForObject(url, requestEntity, String.class);
    }
    @Override
    public String postTemplate(String url, List param, Map<String, String> headers) {
        // 设置头部信息
        HttpHeaders requestHeaders = new HttpHeaders();
        requestHeaders.add("Accept", "application/json");
        requestHeaders.add("Content-Type", "application/json");

        if (headers != null) {
            for (Map.Entry<String, String> header : headers.entrySet()) {
                requestHeaders.add(header.getKey(), header.getValue());
            }
        }

        HttpEntity<String> requestEntity = new HttpEntity<String>(JSON.toJSONString (param, SerializerFeature.WriteNullListAsEmpty), requestHeaders);
        ResponseEntity<String> response = restTemplate.postForEntity(url, requestEntity, String.class);
        //ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.POST, requestEntity, String.class);
        return response.getBody();
    }

}

======================================================================================================

import com.alibaba.fastjson.JSON;
import com.thunisoft.t3.sscyr.template.PatchRETemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.Map;

@Service
public class PatchRETemplateImpl implements PatchRETemplate {

    @Autowired
    private RestTemplate restTemplate;

    @Override
    public String patchTemplate(String url, JSON param, Map<String, String> headers) {
        // 设置头部信息
        HttpHeaders requestHeaders = new HttpHeaders();
        if (headers != null) {
            for (Map.Entry<String, String> header : headers.entrySet()) {
                requestHeaders.add(header.getKey(), header.getValue());
            }
        }
        HttpEntity<String> requestEntity = new HttpEntity<String>(null, requestHeaders);
        ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.PATCH, requestEntity, String.class,param);
        return response.getBody();
    }

    @Override
    public String patchTemplate(String url, String param, Map<String, String> headers) {
        // 设置头部信息
        HttpHeaders requestHeaders = new HttpHeaders();
        if (headers != null) {
            for (Map.Entry<String, String> header : headers.entrySet()) {
                requestHeaders.add(header.getKey(), header.getValue());
            }
        }
        HttpEntity<String> requestEntity = new HttpEntity<String>(null, requestHeaders);
        ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.PATCH, requestEntity, String.class,param);
        return response.getBody();
    }
}

======================================================================================================

import com.thunisoft.t3.sscyr.template.GetRETemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.Map;


@Service
public class GetRETemplateImpl implements GetRETemplate {
    @Autowired
    private RestTemplate restTemplate;

    @Override
    public String getTemplate(String url, Map<String, String> headers) {
        // 设置头部信息
        HttpHeaders requestHeaders = new HttpHeaders();
        if (headers != null) {
            for (Map.Entry<String, String> header : headers.entrySet()) {
                requestHeaders.add(header.getKey(), header.getValue());
            }
        }
        HttpEntity<String> requestEntity = new HttpEntity<String>(null, requestHeaders);
        ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.GET, requestEntity, String.class);
        return response.getBody();
    }

    @Override
    public String getTemplate(String url, Map<String, Object> paramMap, Map<String, String> headers) {
        // 设置头部信息
        HttpHeaders requestHeaders = new HttpHeaders();
        if (headers != null) {
            for (Map.Entry<String, String> header : headers.entrySet()) {
                requestHeaders.add(header.getKey(), header.getValue());
            }
        }
        HttpEntity<String> requestEntity = new HttpEntity<String>(null, requestHeaders);
        ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.GET, requestEntity, String.class,paramMap);
        return response.getBody();
    }
}

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值