当前位置:   article > 正文

http请求调用其他项目的get请求和Post请求接口的方法_http调用前台项目接口

http调用前台项目接口

1.编写工具类封装调用

import com.alibaba.fastjson.JSONObject;
import com.jic.hrois.admin.model.http.RequestModel;
import com.jic.hrois.admin.model.http.Response;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.http.*;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import java.util.Arrays;

@Slf4j
@Component
public class HttpUtil {
    private static final RestTemplate restTemplate = new RestTemplate();
    public static final String PROD_PREFIX = "生产环境地址";
    public static final String TEST_PREFIX = "测试环境地址";
    public static final String LOCAL_PREFIX = "http://localhost:8080";

    @Autowired
    private Environment environment;

    public static ResponseEntity<JSONObject> getForObject(String authorization, String url) {
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        //请求头的token
        headers.set("Authorization", "Bearer "+authorization);
        log.info("url request headers {}",headers);
        HttpEntity<JSONObject> httpEntity = new HttpEntity<>( headers);
        ResponseEntity<JSONObject> result = restTemplate.exchange(url, HttpMethod.GET, httpEntity, JSONObject.class);
        return result;
    }

    public static Response postForObject(String url, String authorization, String param) {
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        headers.set("Authorization", "Bearer "+authorization);
        log.info("调用接口参数:" + param);
        Response response = new Response();
        try{
             response = restTemplate.postForObject(url, new HttpEntity<>(param, headers), Response.class);
        }catch (Exception e){
            response.setCode(-1);
        }
        log.info("接口返回:" + JSONObject.toJSONString(response));
        return response;
    }

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

在其他地方调用就可以了

 import lombok.Data;

@Data
public class RequestModel {
    private String url;
    private String token;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
 @Override
    public void cabinReceive(RequestModel requestModel) {
        String authorization = requestModel.getToken();
        String url = requestModel.getUrl();
        url = url+"接口地址";
        ResponseEntity<JSONObject> result = HttpUtil.getForObject(authorization, url);
        JSONObject body = result.getBody();
        assert body != null;
        Response<JSONObject> response = JSONObject.parseObject(body.toJSONString(), Response.class);
        log.info("返回--- "+response);
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
 @Override
    public void preCustMethod(RequestModel requestModel,ActExtNodeFuncLogDTO actExtNodeFuncLogDTO) throws Exception {
        String authorization = requestModel.getToken();
        String url = requestModel.getUrl();
        url = url+"接口地址";
        //请求参数,接口对应的RequestBody的entity
        String param = new ObjectMapper().writeValueAsString(actExtNodeFuncLogDTO);
        HttpUtil.postForObject(url,authorization,param);
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/AllinToyou/article/detail/259619
推荐阅读
相关标签
  

闽ICP备14008679号