简体   繁体   English

转发具有完全相同的json正文的请求

[英]Forward a request with the exact same json body

I have a SpringBoot application which simply acts as a middleman. 我有一个仅充当中间人的SpringBoot应用程序。 It receives an API request in JSON and forwards this to another server S by calling S's API with the exact same body. 它接收JSON中的API请求,并通过使用完全相同的主体调用S的API将该请求转发到另一个服务器S。

I was exploring the solutions and came across a solution which involved the usage of RestTemplate and MultiValueMap . 我正在探索解决方案,并遇到了涉及RestTemplateMultiValueMap用法的解决方案。 However, since the json body contains objects rather than simple String, I believe I have to create a DTO with corresponding POJO for the solution to work. 但是,由于json主体包含对象而不是简单的String,我相信我必须创建具有相应POJO的DTO才能起作用。

May I ask is the above the only solution, or there is a simple way to forward the request over and get back the response? 请问以上是唯一的解决方案,还是有一种简单的方法可以转发请求并获取响应?

The middleman server can expose a endpoint that accepts a @RequestBody of Object and HttpServletRequest then use RestTemplate to forward it to the remote server. 中间人服务器可以公开一个接受Object@RequestBodyHttpServletRequest的终结点,然后使用RestTemplate将其转发到远程服务器。

The middleman 中间人

@RestController
@RequestMapping("/middleman")
public class MiddleManRestController {

    private RestTemplate restTemplate;

    @PostConstruct
    public void init() {
        this.restTemplate = new RestTemplate();
        this.restTemplate.setRequestFactory(new BufferingClientHttpRequestFactory(this.restTemplate.getRequestFactory()));
    }

    @RequestMapping(value = "/forward", method = RequestMethod.POST)
    public ResponseEntity<Object> forward(@RequestBody Object object, HttpServletRequest request) throws RestClientException {

        //setup the url and path
        final UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl("Remote server URL").path("EnpointPath");

        //add query params from previous request
        addQueryParams(request, builder);

        //specify the method
        final RequestEntity.BodyBuilder requestBuilder = RequestEntity.method(HttpMethod.POST, builder.build().toUri());

        //add headers from previous request
        addHeaders(request, requestBuilder);

        RequestEntity<Object> requestEntity = requestBuilder.body(object);
        ParameterizedTypeReference<Object> returnType = new ParameterizedTypeReference<Object>() {};

        //forward to the remote server
        return this.restTemplate.exchange(requestEntity, returnType);
    }

    private void addHeaders(HttpServletRequest request, RequestEntity.BodyBuilder requestBuilder) {
        Enumeration<String> headerNames = request.getHeaderNames();
        while(headerNames.hasMoreElements()) {
            String headerName = headerNames.nextElement();
            String headerValue = request.getHeader(headerName);
            requestBuilder.header(headerName, headerValue);
        }
    }

    private void addQueryParams(HttpServletRequest request, UriComponentsBuilder builder) {
        final MultiValueMap<String, String> queryParams = new LinkedMultiValueMap<String, String>();
        Map<String, String[]> parameterMap = request.getParameterMap();
        if (parameterMap != null) {
            parameterMap.forEach((key, value) -> queryParams.addAll(key, Arrays.asList(value)));
        }
        builder.queryParams(queryParams);
    }
}

Even complex and nested JSON objects can be taken into a Map with key as String and value as Object. 甚至可以将复杂且嵌套的JSON对象放入键为String且值为Object的Map中。 I believe you should just use such a map as your request body and transfer the same to another api. 我相信您应该只使用这样的地图作为请求主体,然后将其转移到另一个API。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM