- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
从 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)
整个项目都是关于缩短 url,作为我使用 spring websockets 的第一种方法,我试图通过从任何 url 回复缩短的 url 来让它工作。
我的TextWebSocketHandler
public class MyHandler extends TextWebSocketHandler {
AtomicLong messageOrder = new AtomicLong(0);
ExecutorService threadPool = Executors.newCachedThreadPool();
CompletionService<CSVContent> pool = new ExecutorCompletionService<>(threadPool);
/* controller reference */
@Autowired
UrlShortenerControllerWithLogs controller;
@Override
protected void handleTextMessage(WebSocketSession session, TextMessage message) throws Exception {
logger.info("WS: " + message.getPayload());
long order = messageOrder.getAndIncrement();
pool.submit(new ShortURLWSGenerator(order,message.getPayload(),"","","",session.getRemoteAddress(),controller));
CSVContent content = pool.take().get();
session.sendMessage(new TextMessage("Echo Test: " + content.getShortURL().getUri()));
}
}
这是ShortURLWSGenerator
public class ShortURLWSGenerator extends RequestContextAwareCallable<CSVContent>{
...
@Override
public CSVContent onCall() {
ShortURL shortURL = null;
try {
shortURL = controller. new CreateCallable(url,sponsor,brand,owner,address.toString()).call();
} catch (Exception e) {
e.printStackTrace();
}
CSVContent content = new CSVContent();
content.setOrder(order);
content.setShortURL(shortURL);
return content;
}
}
二手 RequestContextAwareCallable它在实现与 REST 服务相同的功能时解决了同样的问题,无论如何,即使使用简单的 Callable,我仍然会遇到同样的错误。
这里是 CreateCallable 只是包装了 main 函数
public class CreateCallable implements Callable<ShortURL>{
...
@Override
public ShortURL call() throws Exception {
/* explodes while creating the new short url */
return createAndSaveIfValid(url,sponsor,brand,owner,ip);
}
}
最后是 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(), sponsor, new Date(
System.currentTimeMillis()), owner,
HttpStatus.TEMPORARY_REDIRECT.value(), true, ip, null);
return shortURLRepository.save(su);
} else {
return null;
}
}
完整工程可见here on github
最佳答案
密切相关的一点是,对于那些单元测试其具体 ResourceAssemblerSupport
实现并体验相同堆栈跟踪的人,以下是模拟它的方法:
@Before
public void setup() {
HttpServletRequest mockRequest = new MockHttpServletRequest();
ServletRequestAttributes servletRequestAttributes = new ServletRequestAttributes(mockRequest);
RequestContextHolder.setRequestAttributes(servletRequestAttributes);
}
@After
public void teardown() {
RequestContextHolder.resetRequestAttributes();
}
关于java - Spring Boot,从 websocket 调用 ControllerLinkBuilder.linkTo 时出现 java.lang.IllegalStateException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27802786/
我目前正在尝试对部分 URL 进行编码,但不是全部 这是一个示例: http://localhost:8080/resourceSearch?type=http%3A%2F%2Fexample.com
我希望我的回复包括以下内容: "keyMaps":{ "href":"http://localhost/api/keyMaps{/keyMapId}", "templated":true }
我正在尝试使用 spring-hateoas 将 HATEOAS 应用于我的 spring boot 应用程序.在我将我的 REST 调用包装在 HystrixCommand 之前,这一直很好用: @
我正在学习有关 Spring REST 的教程,并尝试将 HATEOAS 链接添加到我的 Controller 结果中。 我有一个简单的用户类和一个 CRUD Controller 。 class U
我正在尝试将 Spring HATEOAS 合并到现有的服务工具和 REST API 中。但是,我遇到的问题之一是 ControllerLinkBuilder 似乎删除了尾部斜杠(这是由于现有约束的要
设置:所以我有一个用 java 编写的 RESTfull API,使用 spring-boot 和 spring-hates 添加链接到资源(超媒体驱动的 RESTful Web 服务)。我拥有的一切
Spring HATEOAS 提供了得心应手的ControllerLinkBuilder创建指向 Controller 方法的链接,这些链接将作为 href 添加到返回给客户端的 JSON/XML 中
在使用 ControllerLinkBuilder 和 methodOn 功能时,我不明白的一件事是,当您的 Controller 具有这样的方法签名时,您应该做什么: public HttpEnti
我正在尝试调用 Spring 的 ControllerLinkBuilder.methodOn()使用非 String 类型,它总是失败。而我不知道是哪种Converter使用以及在哪里注册。 这是我
我在 Boot 应用程序中使用 Spring Hateoas 以避免在 View 中手动创建链接。它在 Thymeleaf View 中工作得很好,当 Controller 调用服务发送同样由 Thy
我在 spring Async 方法中调用 ControllerLinkBuilder.linkTo 方法,但它无法找到当前请求。 service.setUrl(linkTo(Controller.c
从 websocket 调用 ControllerLinkBuilder.linkTo 时出现以下错误。 java.lang.IllegalStateException: Could not fin
我是一名优秀的程序员,十分优秀!