Forward HttpServletRequest to a different server

前端 未结 5 1612
星月不相逢
星月不相逢 2021-01-31 15:24

I got a HttpServletRequest request in my Spring Servlet which I would like to forward AS-IS (i.e. GET or POST content) to a different server.

What would be

5条回答
  •  春和景丽
    2021-01-31 16:14

    @RequestMapping(value = "/**")
    public ResponseEntity route(HttpServletRequest request) throws IOException {
        String body = IOUtils.toString(request.getInputStream(), Charset.forName(request.getCharacterEncoding()));
        try {
            ResponseEntity exchange = restTemplate.exchange(firstUrl + request.getRequestURI(),
                    HttpMethod.valueOf(request.getMethod()),
                    new HttpEntity<>(body),
                    Object.class,
                    request.getParameterMap());
            return exchange;
        } catch (final HttpClientErrorException e) {
            return new ResponseEntity<>(e.getResponseBodyAsByteArray(), e.getResponseHeaders(), e.getStatusCode());
        }
    }
    
        

    提交回复
    热议问题