简体   繁体   中英

Spring Async: How to call spring hateoas ControllerLinkBuilder inside Async method

I am calling ControllerLinkBuilder.linkTo method inside a spring Async method and it fails to find the current request.

service.setUrl(linkTo(Controller.class, Controller.METHOD_GET,
                headers.getFirst(HEADER_SOURCE), id).toString());

Exception:

java.util.concurrent.ExecutionException: java.lang.IllegalStateException: Could not find current request via RequestContextHolder
Caused by: java.lang.IllegalStateException: Could not find current request via RequestContextHolder

I tried to reconstruct your problem and weren't able to. Have you tried using something like the following?

linkTo(methodOn(EnvironmentRequestStateTransitioner.class)
    .approveRegistrationRequest(23L))
    .withRel("approve")

That worked fine for me using spring-boot-starter-parent version 1.3.2.RELEASE .

Complementing the _Manan reponse, You can set the attribute inheritance to true on your properties file. Please add the following line:

spring.freemarker.request-context-attribute=true

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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