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 + "/api/" + headers.get(CommonConstant.HTTP_SERVICE);
        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);
        logger.debug("请求后端url" + url);

        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;
    }
}

@@ -417,26 +417,17 @@
 rk.http.
-HttpEntity
+*
 ;%0Aimport
@@ -451,24 +451,26 @@
 ork.
-http.HttpHeaders
+stereotype.Service
 ;%0Aim
@@ -498,118 +498,42 @@
 ork.
-http.HttpMethod;%0Aimport org.springframework.http.ResponseEntity;%0Aimport org.springframework.stereotype.Service
+web.client.HttpStatusCodeException
 ;%0Aim
@@ -2173,17 +2173,16 @@
 + url);%0A
-%0A
         
@@ -2220,16 +2220,65 @@
 Entity =
+ null;%0A        try %7B%0A            responseEntity =
  restTem
@@ -2332,16 +2332,396 @@
 class);%0A
+        %7D catch (HttpStatusCodeException e) %7B //%E8%BF%99%E9%87%8Cspring %E6%A1%86%E6%9E%B6 %E5%9C%A84XX %E6%88%96 5XX %E6%97%B6%E6%8A%9B%E5%87%BA HttpServerErrorException %E5%BC%82%E5%B8%B8%EF%BC%8C%E9%9C%80%E8%A6%81%E9%87%8D%E6%96%B0%E5%B0%81%E8%A3%85%E4%B8%80%E4%B8%8B%0A            responseEntity = new ResponseEntity%3CString%3E(e.getResponseBodyAsString(), e.getStatusCode());%0A        %7D catch (Exception e) %7B%0A            responseEntity = new ResponseEntity%3CString%3E(e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);%0A        %7D finally %7B%0A    
         
@@ -2762,32 +2762,36 @@
 ntity);%0A        
+    
 return responseE
@@ -2797,16 +2797,26 @@
 Entity;%0A
+        %7D%0A
     %7D%0A%0A%0A

优化400 错误