在我的spring boot项目中,我尝试这样截取rest api帖子:
Rest控制器:@Rest控制器@请求映射(“/”)公共类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);
}
}
}服务:
@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 (用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");
}对于一些帖子,我没有问题,实际上我得到了200,我还可以看到我在输入中传递的map中存在的所有参数……对于其他POST,我得到了错误400的错误请求,并且我注意到输入参数的映射打印为空白。我该如何解决这个问题?在我看来,这个问题与如下事实有关:在入口处,我发现自己是一张空地图……在这些情况下,我应该怎么做?
发布于 2021-07-16 12:00:44
在rest控制器类中添加@RestController注释。
https://stackoverflow.com/questions/68399002
复制相似问题