Я пытаюсь сделать что-то похожее на следующее. Я хочу иметь возможность установить infoVar в myController и получить его в MyResponseEntityExceptionHandler. Я попытался установить правильную область (я думаю, Запрос), но я продолжаю получать ошибки, подобные:
Caused by: java.lang.IllegalStateException: No thread-bound request found: Are you referring to request attributes outside of an actual web request, or processing a request outside of the originally receiving thread? If you are actually operating within a web request and still receive this message, your code is probably running outside of DispatcherServlet: In this case, use RequestContextListener or RequestContextFilter to expose the current request.
Контроллер
@RestController()
public class myController() {
@Autowired private InfoVar infoVar;
@PostMapping(path = "/stuff")
public @ResponseBody sutff getStuff(@RequestBody String string) throws Exception {
infoVar.setVar("Test123");
return stuff;
}
}
обработка ошибок
@ControllerAdvice
@RestController
public class MyResponseEntityExceptionHandler extends ResponseEntityExceptionHandler {
@Autowired private InfoVar infoVar
@ExceptionHandler(Exception.class)
public final ResponseEntity<Object> handleStuff(Exception ex, WebRequest request) {
infoVar.getVar();
return stuff;
}
}
infoVar
@Component
@Scope("request")
public class InfoVar{
private String infoVar;
getter and setter for infovar
}