使用RestTemplate实现http请求转发代理转发

在做微服务和单独集成等同构异构的项目过程中,可能会遇到做代理转发、请求转发等相关的功能,下面的代码(项目片段demo代码)可能具有一定的参与功能

下面代码仅供参考,具体还需要根据自己的项目情况对代码进行改造。可以在springMVC springboot项目中使用

import org.springframework.http.*;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.net.URI;
import java.net.URISyntaxException;


/**
 * Created by zhangguoye on 2020/6/3
 * @author zhangguoye
 * 请求代理,请求转发
 * mapping的规则需要根据项目情况定义
 */
@RestController
@RequestMapping("/newportal")
public class HomeController {

    /** 以下请求服务器的相关信息,请在配置文件中进行配置 */
    private String uriSchema = "http";
    private String uriHostIp = "192.168.10.127";
    private Integer uriHostPort = 8088;
    /** mapping的规则需要根据项目情况定义 */
    private String uriRegex = "/newportal/api";

    /**
     * 向PC端转发get请求
     * @param request
     * @param response
     * @param httpMethod
     * @return
     * @throws URISyntaxException
     */
    @GetMapping(value = "/api/**")
    public ResponseEntity<Object> getPCWithParams(HttpServletRequest request, HttpServletResponse response, HttpMethod httpMethod) throws URISyntaxException {
        ResponseEntity res = request2PC(request, httpMethod, "");
        return res;
    }

    /**
     * 向PC端转发post请求  JSON数据请求
     * @param body  body为JSON数据
     * @param request
     * @param response
     * @param httpMethod
     * @return
     * @throws URISyntaxException
     */
    @PostMapping(value = "/api/**", consumes = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<Object> postPCWithJson(@RequestBody Object body,HttpServletRequest request, HttpServletResponse response, HttpMethod httpMethod) throws URISyntaxException {
        ResponseEntity res = request2PC(request, httpMethod, body);
        return res;
    }

    /**
     * 向PC端转发post请求  formUrlEncode数据请求
     * @param formUrlEncode formUrlEncode数据
     * @param request
     * @param response
     * @param httpMethod
     * @return
     * @throws URISyntaxException
     */
    @PostMapping(value = "/api/**", consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
    public ResponseEntity<Object> postPCWithFormUrlEncode(@RequestBody MultiValueMap<String, String> formUrlEncode, HttpServletRequest request, HttpServletResponse response, HttpMethod httpMethod) throws URISyntaxException {
        ResponseEntity res = request2PC(request, httpMethod, formUrlEncode);
        return res;
    }

    /**
     * 向PC端转发post请求  formData数据请求 文件上传请求
     * @param request
     * @param response
     * @param httpMethod
     * @return
     * @throws URISyntaxException
     */
    @PostMapping(value = "/api/**", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public ResponseEntity<Object> postPCWithFormData(HttpServletRequest request, HttpServletResponse response, HttpMethod httpMethod) throws URISyntaxException {
//        ResponseEntity res = request2PC(request, httpMethod, "");
//        return res;
        return null;
    }

    /**
     * 向PC端转发请求
     * @param request  HttpServletRequest
     * @param httpMethod 请求方式
     * @param body 请求body
     * @return
     * @throws URISyntaxException
     */
    private ResponseEntity request2PC(HttpServletRequest request, HttpMethod httpMethod, Object body) throws URISyntaxException {
        String[] requestURIArr = request.getRequestURI().split(uriRegex);
        if (requestURIArr.length < 1) {
            throw new RuntimeException("请求路径异常!");
        }
        String requestURI = requestURIArr[1];
        URI thirdPartyApi = new URI(uriSchema, null, uriHostIp, uriHostPort, requestURI, request.getQueryString(), null);
        RestTemplate restTemplate = new RestTemplate();
        HttpEntity httpEntity = new HttpEntity(body);
        ResponseEntity<Object> response = restTemplate.exchange(thirdPartyApi, httpMethod, httpEntity, Object.class);
        return  response;
    }

}

一篇RestTemplate使用说明(中文):https://my.oschina.net/sdlvzg/blog/1800395

如果能给你提供到帮助,希望能点个赞👍🏻

Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐