简体   繁体   English

如何将未知的传入 requestParam 传递给 spring boot 中的帖子?

[英]How to pass an unknown incoming requestParam to a post in spring boot?

In my spring boot project I'am trying to intercept a rest api post in this way:在我的spring boot项目中,我试图以这种方式拦截一个rest api post:

Rest Controller: @Rest Controller @RequestMapping("/") public class FourStoreControllerInterface {休息控制器:@Rest 控制器 @RequestMapping("/") 公共类 FourStoreControllerInterface {

        @ApiOperation(value = "Manage Multiple 4Store Post")
        @ApiResponses(value = { @ApiResponse(code = 401, message = "Unauthorized"),
                @ApiResponse(code = 403, message = "Forbidden") })
        @PostMapping("**")
        public ResponseEntity<?> manageMultiplePost4StoreWithRestTemplate(
                @RequestParam @ApiParam(hidden = true) Map<String, String> allParams) throws Exception{
            final String methodName = "manageMultiplePost4StoreWithRestTemplate()";
            try {
    
                startLog(methodName);
    
                return fourStoreService.managePostEndpointsWithRestTemplate(allParams);
    
            } catch (final Exception e) {
                this.errorLog(methodName, e);
                throw e;
            } finally {
                endLog(methodName);
            }
    }

}

Service:服务:

@ResponseBody
public ResponseEntity<?> managePostEndpointsWithRestTemplate(Map<String, String> allParams) {
    final String methodName = "managePostEndpointsWithRestTemplate(Map<String, String> allParams, JSONObject jsonParams)";
    try {
        startLog(methodName);
        return managePost(allParams);
    } catch (Exception e) {
        logger.error(e.getMessage());
        throw e;
    } finally {
        endLog(methodName);
    }
}

managePost (method implemented in AbstractService): managePost(在 AbstractService 中实现的方法):

public ResponseEntity<?> managePost(Map<String, String> allParams) {
        try {
            try {

            logger.debug("I AM HERE WITH MAP");

            // REQUEST 1
            HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes())
                    .getRequest();

            Route routeConfiguration = findRootPosts(request);
            if (routeConfiguration != null) {
                String url = routeConfiguration.getDestination().getProtocol()
                        + routeConfiguration.getDestination().getIp() + ":"
                        + routeConfiguration.getDestination().getPort() + request.getRequestURI();

                boolean first = true;
                for (String key : allParams.keySet()) {
                    logger.debug("OLD ALL PARAMETERS : {} ", key + "=" + allParams.get(key));
                }

                HttpHeaders headers = new HttpHeaders();
                headers.setBasicAuth(routeConfiguration.getDestination().getUsername(),
                        routeConfiguration.getDestination().getPassword());

                for (String headersName : Collections.list(request.getHeaderNames())) {
                    List<String> headersValue = Collections.list(request.getHeaders(headersName));
                    headers.put(headersName, headersValue);
                    logger.debug(" REQUEST 1 HEADERS : {} = {} ", headersName, headersValue);
                }

                for (Cookie c : request.getCookies()) {
                    logger.debug(" REQUEST 1 COOKIES : {} = {} ", c.getName(), c.getValue());
                }

                MultiValueMap<String, String> map = new LinkedMultiValueMap<String, String>();
//                  map.putAll(headers);
                    for (String key : allParams.keySet()) {
                        map.put(key, Arrays.asList(allParams.get(key)));
                    }
                    logger.debug("MAP OF PARAMETERS : {} ", map);

                // REQUEST 2
                HttpEntity<MultiValueMap<String, String>> request2;
                if (allParams != null && !allParams.isEmpty())
                    request2 = new HttpEntity<MultiValueMap<String, String>>(map, headers);
                else
                    request2 = new HttpEntity(headers);

                logger.debug("BODY REQUEST 2: {} ", request2.getBody());

                if (url.startsWith("https")) {
                    restTemplate = getRestTemplateForSelfSsl();
                } else {
//                      restTemplate = new RestTemplate();
                    }
                    logger.debug("URL POST: {} ", url);

                UriComponents uriComponents = UriComponentsBuilder.fromHttpUrl(url).build(true);
                ResponseEntity<?> response;
                if (xssResponseFilter) {
                    response = restTemplate.exchange(new URI(uriComponents.toUriString()), HttpMethod.POST,
                            request2, String.class);
                } else {
                    response = restTemplate.exchange(new URI(uriComponents.toUriString()), HttpMethod.POST,
                            request2, byte[].class);
                }

                HttpStatus statusCode = response.getStatusCode();
                logger.debug("STATUS POST: {} ", statusCode);

                HttpHeaders responseHeaders = response.getHeaders();
                logger.debug("RESPONSE HEADERS : {} ", responseHeaders);

                logger.debug("RESPONSE POST: {} ", response);

                if (xssResponseFilter) {
                    response = sanitizeResponseBody((ResponseEntity<String>) response);
                }
                return response;
            }
        } catch (HttpStatusCodeException e) {
            logger.error(e.getMessage());
            return ResponseEntity.status(e.getStatusCode()).body(e.getResponseBodyAsByteArray());
        }
    } catch (Exception e) {
        logger.error(e.getMessage());
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(e.getMessage());
    }
    return ResponseEntity.status(HttpStatus.NOT_FOUND).body("Not valid route found");
}

With some POST I have no problems, in fact I get 200, and I can also see all the parameters that are present in the map that I pass in input ... with other POST I get error 400 BAD REQUEST and I notice that the map of the input parameters comes to me printed blank.使用一些 POST 我没有问题,实际上我得到 200,而且我还可以看到我在输入中传递的地图中存在的所有参数......使用其他 POST 我得到错误 400 BAD REQUEST,我注意到输入参数的图来找我打印空白。 How can I solve the problem?我该如何解决问题? In my opinion the problem concerns the fact that at the entrance I find myself an empty map ... what should I do in these cases?在我看来,问题在于在入口处我发现自己有一张空地图……在这些情况下我该怎么办?

在 rest 控制器类中添加 @RestController 注释。

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

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