将 POST 请求从一个 REST API 转发/重定向到另一个

Posted

技术标签:

【中文标题】将 POST 请求从一个 REST API 转发/重定向到另一个【英文标题】:Forwarding/redirecting a POST request from one REST API to another 【发布时间】:2017-05-07 02:10:27 【问题描述】:

我希望实现一种将 POST 请求从一个 SpringBoot 应用程序直接转发到另一个应用程序的方法。转发 GET 请求很容易,下面的代码显示了当我懒惰地尝试重用相同的重定向路径时如何得到 405 错误。

我查看了 *** 上的现有示例(最著名的是 here),但我什至无法编译该解决方案。任何人都可以建议修改下面的代码以允许我重定向整个 POST 请求吗?

@RestController
public class Routing 

    @Autowired
    private RoutingDelegate routingDelegate;

    @RequestMapping(value="/**", method=RequestMethod.GET, produces=MediaType.TEXT_PLAIN_VALUE)
    public ResponseEntity<String> catchAll(HttpServletRequest request, HttpServletResponse response)
        return routingDelegate.redirect(request, response);
    

    @RequestMapping(value="/**", method=RequestMethod.POST, produces=MediaType.TEXT_PLAIN_VALUE)
    public ResponseEntity<String> catchAllPost(HttpServletRequest request, HttpServletResponse response)
        /**
         * This returns a 405 error, which is understandable since I'm redirecting a POST request to a GET endpoint
         */
        return routingDelegate.redirect(request, response);
    

 

@Service
public class RoutingDelegate 

    private final String baseServerUrl = "http://localhost:8080";

    public ResponseEntity<String> redirect(HttpServletRequest request, HttpServletResponse response)
        try 
            String queryString = request.getQueryString();
            String redirectUrl = baseServerUrl + request.getRequestURI() +
                    (queryString != null ? "?" + queryString : "");
            response.sendRedirect(redirectUrl);
        
        catch (IOException e) 
            return new ResponseEntity<String>("REDIRECT ERROR", HttpStatus.INTERNAL_SERVER_ERROR);
        
        return new ResponseEntity<String>("", HttpStatus.OK);
    

【问题讨论】:

【参考方案1】:

我不久前解决了这个问题,以防万一有人感兴趣:

@Service
public class RoutingDelegate 

    private final String baseServerUrl = "http://localhost:8080";

    public ResponseEntity<String> redirectPost(HttpServletRequest request, HttpServletResponse response)
        String responseString = "";
        try 
            // build up the redirect URL
            String queryString = request.getQueryString();
            String redirectUrl = baseServerUrl + request.getRequestURI() +
                    (queryString != null ? "?" + queryString : "");

            responseString = forwardPostRequest(redirectUrl, request);
        
        catch (Exception e) 
            return new ResponseEntity<String>("REDIRECT ERROR", HttpStatus.INTERNAL_SERVER_ERROR);
        
        return new ResponseEntity<String>(responseString, HttpStatus.OK);
    

    private String forwardPostRequest(String url, HttpServletRequest request)
        String responseString = "";
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);

        // rebuild map from params
        MultiValueMap<String, String> map= new LinkedMultiValueMap<String, String>();
        for (Entry<String, String[]> entry : request.getParameterMap().entrySet()) 
            String key = entry.getKey();
            String[] values = entry.getValue();
            map.add(key, values[0]);
        

        HttpEntity<MultiValueMap<String, String>> newRequest = new HttpEntity<MultiValueMap<String, String>>(map, headers);

        RestTemplate restTemplate = new RestTemplate();
        ResponseEntity<String> responseEntity = restTemplate.postForEntity( url, newRequest , String.class );
        responseString = responseEntity.getBody();
        return responseString;
    

【讨论】:

以上是关于将 POST 请求从一个 REST API 转发/重定向到另一个的主要内容,如果未能解决你的问题,请参考以下文章

将请求从 example.com:8000(旧 REST API)转发到 AWS 上的 api.example.com(新 REST API)

将 POST 从 api 转发到另一个时出现错误代码 415

将雪花数据发送到 REST API (POST) 的方法

对 Wordpress REST API 的 POST 请求返回一个空对象

如何使用 Postman 表单数据将 POST 请求发送到 Spring Boot REST API?

尝试在 https://api.thetvdb.com/ 向 TVDB REST API 发出 JSON POST 请求,但出现 CORS 错误