Spring Boot App 注册 RequestContextListener 失败
Spring Boot App fails to register RequestContextListener
我正在创建简单的 REST 控制器,为此我在我的 spring 启动应用程序中添加了 RequestContextListener
的配置
@Configuration
@WebListener
public class DataApiRequestContextListener extends RequestContextListener {
}
在控制器中,我尝试构建位置 header 以获得成功的 post 请求
@Async("webExecutor")
@PostMapping("/company")
public CompletableFuture<ResponseEntity<Object>> save(@RequestBody Company company) {
Company savedCompany = companyRepository.save(company);
URI location = ServletUriComponentsBuilder.fromCurrentRequest().path("/{id}")
.buildAndExpand(savedCompany.getId()).toUri();
ResponseEntity<Object> response = ResponseEntity.created(location).build();
LOGGER.debug("Reponse Status for POST Request is :: " + response.getStatusCodeValue());
LOGGER.debug("Reponse Data for POST Request is :: " + response.getHeaders().getLocation().toString());
return CompletableFuture.completedFuture(response);
}
我得到异常
java.lang.IllegalStateException: No current ServletRequestAttributes
当我在这一行尝试使用 ServletUriComponentsBuilder
构建位置 URI 时
URI location = ServletUriComponentsBuilder.fromCurrentRequest().path("/{id}")
.buildAndExpand(savedCompany.getId()).toUri();
如果使用 @async
,则使用 fromRequestUri
@Async("webExecutor")
@PostMapping("/company")
public CompletableFuture<ResponseEntity<Object>> save(HttpServletRequest request, @RequestBody Company company) {
Company savedCompany = companyRepository.save(company);
URI location = ServletUriComponentsBuilder.fromRequestUri(request).path("/{id}")
.buildAndExpand(savedOrganization.getId()).toUri();
ResponseEntity<Object> response = ResponseEntity.created(location).build();
LOGGER.debug("Reponse Status for POST Request is :: " + response.getStatusCodeValue());
LOGGER.debug("Reponse Data for POST Request is :: " + response.getHeaders().getLocation().toString());
return CompletableFuture.completedFuture(response);
}
或没有 @async
应该可以工作,return 你的位置 uri。
@PostMapping("/company")
public CompletableFuture<ResponseEntity<Object>> save(@RequestBody Company company) {
Company savedCompany = companyRepository.save(company);
URI location = ServletUriComponentsBuilder.fromCurrentRequest().path("/{id}")
.buildAndExpand(savedCompany.getId()).toUri();
ResponseEntity<Object> response = ResponseEntity.created(location).build();
LOGGER.debug("Reponse Status for POST Request is :: " + response.getStatusCodeValue());
LOGGER.debug("Reponse Data for POST Request is :: " + response.getHeaders().getLocation().toString());
return CompletableFuture.completedFuture(response);
}
我正在创建简单的 REST 控制器,为此我在我的 spring 启动应用程序中添加了 RequestContextListener
@Configuration
@WebListener
public class DataApiRequestContextListener extends RequestContextListener {
}
在控制器中,我尝试构建位置 header 以获得成功的 post 请求
@Async("webExecutor")
@PostMapping("/company")
public CompletableFuture<ResponseEntity<Object>> save(@RequestBody Company company) {
Company savedCompany = companyRepository.save(company);
URI location = ServletUriComponentsBuilder.fromCurrentRequest().path("/{id}")
.buildAndExpand(savedCompany.getId()).toUri();
ResponseEntity<Object> response = ResponseEntity.created(location).build();
LOGGER.debug("Reponse Status for POST Request is :: " + response.getStatusCodeValue());
LOGGER.debug("Reponse Data for POST Request is :: " + response.getHeaders().getLocation().toString());
return CompletableFuture.completedFuture(response);
}
我得到异常
java.lang.IllegalStateException: No current ServletRequestAttributes
当我在这一行尝试使用 ServletUriComponentsBuilder
构建位置 URI 时
URI location = ServletUriComponentsBuilder.fromCurrentRequest().path("/{id}")
.buildAndExpand(savedCompany.getId()).toUri();
如果使用 @async
,则使用 fromRequestUri
@Async("webExecutor")
@PostMapping("/company")
public CompletableFuture<ResponseEntity<Object>> save(HttpServletRequest request, @RequestBody Company company) {
Company savedCompany = companyRepository.save(company);
URI location = ServletUriComponentsBuilder.fromRequestUri(request).path("/{id}")
.buildAndExpand(savedOrganization.getId()).toUri();
ResponseEntity<Object> response = ResponseEntity.created(location).build();
LOGGER.debug("Reponse Status for POST Request is :: " + response.getStatusCodeValue());
LOGGER.debug("Reponse Data for POST Request is :: " + response.getHeaders().getLocation().toString());
return CompletableFuture.completedFuture(response);
}
或没有 @async
应该可以工作,return 你的位置 uri。
@PostMapping("/company")
public CompletableFuture<ResponseEntity<Object>> save(@RequestBody Company company) {
Company savedCompany = companyRepository.save(company);
URI location = ServletUriComponentsBuilder.fromCurrentRequest().path("/{id}")
.buildAndExpand(savedCompany.getId()).toUri();
ResponseEntity<Object> response = ResponseEntity.created(location).build();
LOGGER.debug("Reponse Status for POST Request is :: " + response.getStatusCodeValue());
LOGGER.debug("Reponse Data for POST Request is :: " + response.getHeaders().getLocation().toString());
return CompletableFuture.completedFuture(response);
}