吴学文
2019-11-18 ca210f592eed9194b85e8a7b1900ebf8741e4fb0
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package com.java110.app.smo.api.impl;
 
import com.alibaba.fastjson.JSONObject;
import com.java110.app.smo.api.IApiSMO;
import com.java110.core.component.BaseComponentSMO;
import com.java110.utils.constant.CommonConstant;
import com.java110.utils.constant.ServiceConstant;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
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("apiSMOImpl")
public class ApiSMOImpl extends BaseComponentSMO implements IApiSMO {
 
 
    private final static Logger logger = LoggerFactory.getLogger(ApiSMOImpl.class);
 
    @Autowired
    private RestTemplate restTemplate;
 
    @Override
    public ResponseEntity<String> doApi(String body, Map<String, String> headers) {
 
        logger.debug("api请求头" + headers + ";请求内容:" + body);
        HttpMethod method = null;
        String url = ServiceConstant.SERVICE_API_URL;
        if (CommonConstant.HTTP_METHOD_POST.equals(headers.get(CommonConstant.HTTP_METHOD))) {
            method = HttpMethod.POST;
        } else if (CommonConstant.HTTP_METHOD_GET.equals(headers.get(CommonConstant.HTTP_METHOD))) {
            method = HttpMethod.GET;
            url += super.mapToUrlParam(JSONObject.parseObject(body));
        } else if (CommonConstant.HTTP_METHOD_DELETE.equals(headers.get(CommonConstant.HTTP_METHOD))) {
            method = HttpMethod.DELETE;
        } else if (CommonConstant.HTTP_METHOD_PUT.equals(headers.get(CommonConstant.HTTP_METHOD))) {
            method = HttpMethod.PUT;
 
        } else {
            throw new IllegalArgumentException("不支持的请求方式" + headers.get(CommonConstant.HTTP_METHOD));
        }
 
        HttpHeaders header = new HttpHeaders();
        for (String key : headers.keySet()
        ) {
            header.add(key, headers.get(key));
        }
 
        HttpEntity<String> httpEntity = new HttpEntity<String>(body, header);
 
        ResponseEntity<String> responseEntity = restTemplate.exchange(url, method, httpEntity, String.class);
        logger.debug("api返回信息" + responseEntity);
        return responseEntity;
    }
 
 
    public RestTemplate getRestTemplate() {
        return restTemplate;
    }
 
    public void setRestTemplate(RestTemplate restTemplate) {
        this.restTemplate = restTemplate;
    }
}