从websocket调用ControllerLinkBuilder.linkTo时出现以下错误.
java.lang.IllegalStateException: Could not find current request via RequestContextHolder
at org.springframework.util.Assert.state(Assert.java:385)
at org.springframework.hateoas.mvc.ControllerLinkBuilder.getCurrentRequest(ControllerLinkBuilder.java:234)
at org.springframework.hateoas.mvc.ControllerLinkBuilder.getBuilder(ControllerLinkBuilder.java:186)
at org.springframework.hateoas.mvc.ControllerLinkBuilderFactory.linkTo(ControllerLinkBuilderFactory.java:117)
at org.springframework.hateoas.mvc.ControllerLinkBuilder.linkTo(ControllerLinkBuilder.java:135)
at urlshortener2014.common.web.UrlShortenerController.createAndSaveIfValid(UrlShortenerController.java:94)
at urlshortener2014.richcarmine.web.UrlShortenerControllerWithLogs.access$200(UrlShortenerControllerWithLogs.java:45)
at urlshortener2014.richcarmine.web.UrlShortenerControllerWithLogs$CreateCallable.call(UrlShortenerControllerWithLogs.java:226)
at urlshortener2014.richcarmine.massiveShortenerNaiveWS.ShortURLWSGenerator.onCall(ShortURLWSGenerator.java:41)
at urlshortener2014.richcarmine.massiveShortenerNaiveWS.ShortURLWSGenerator.onCall(ShortURLWSGenerator.java:15)
at urlshortener2014.richcarmine.massiveShortenerREST.RequestContextAwareCallable.call(RequestContextAwareCallable.java:26)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
整个项目是关于缩短网址,因为我的第一个方法是弹簧websockets我试图通过从任何网址回复缩短的网址来使其工作.
我的TextWebSocketHandler
public class MyHandler extends TextWebSocketHandler {
AtomicLong messageOrder = new AtomicLong(0);
ExecutorService threadPool = Executors.newCachedThreadPool();
CompletionService
这是ShortURLWSGenerator
public class ShortURLWSGenerator extends RequestContextAwareCallable
使用RequestContextAwareCallable时,在实现与REST服务相同的功能时解决了同样的问题,在任何情况下,即使使用简单的Callable,我仍然会得到相同的错误.
这是CreateCallable,它只包含main函数
public class CreateCallable implements Callable
最后这里是createAndSaveIfValid,它调用ControllerLinkBuilder.linkTo
protected ShortURL createAndSaveIfValid(String url,String sponsor,String brand,String owner,String ip) {
UrlValidator urlValidator = new UrlValidator(new String[] { "http","https" });
if (urlValidator.isValid(url)) {
String id = Hashing.murmur3_32()
.hashString(url,StandardCharsets.UTF_8).toString();
ShortURL su = new ShortURL(id,url,linkTo(
methodOn(UrlShortenerController.class).redirectTo(
id,null)).toUri(),new Date(
System.currentTimeMillis()),HttpStatus.TEMPORARY_REDIRECT.value(),true,ip,null);
return shortURLRepository.save(su);
} else {
return null;
}
}
完整的项目可以在here on github找到
最佳答案
linkTo取决于当前的HTTP请求,但HTTP当前请求不存在,因为该调用是由WebSocket事件发起的.因此,您需要一种不同的方法.
原文链接:https://www.f2er.com/spring/432074.html>创建一个名为eg的方法createAndSaveIfValidExtended基于createAndSaveIfValid.代码是相同的但是linkTo(methodOn(UrlShortenerController.class).redirectTo(id,null)).toUri()被方法createLink(id)替换
>创建方法String createLink(String id).此方法将使用application.properties中定义的属性构建URL(请参阅此处how),其值将在一个字段中注入,该字段表示应用程序将与/ l连接的位置以及“d”的值.
>在CreateCallable中,调用createAndSaveIfValidExtended而不是createAndSaveIfValid