【问题标题】:@GetMapping and RSocketServer with spring-boot-starter-rsocket@GetMapping 和 RSocketServer 与 spring-boot-starter-rsocket
【发布时间】:2019-11-03 08:53:49
【问题描述】:

正在尝试https://www.baeldung.com/spring-boot-rsocket 第 4 节中指定的 RSocket 请求/响应。所以有一个 RSocketServer 自动配置并在端口 7000 上侦听。当从浏览器点击相同的方法时,无法连接到带有 @GetMapping 注释的方法

@RestController
public class MarketDataRestController {

    private final RSocketRequester rSocketRequester;

    public MarketDataRestController(RSocketRequester rSocketRequester) {
        this.rSocketRequester = rSocketRequester;
    }

    @GetMapping(value = "/current/{stock}")
    public Publisher<MarketData> current(@PathVariable("stock") String stock) {
        return rSocketRequester
          .route("currentMarketData")
          .data(new MarketDataRequest(stock))
          .retrieveMono(MarketData.class);
    }
}

当从浏览器请求相同的内容时,期望能够连接到 MarketDataRestController 类的 current() 注释 @GetMapping,例如:http://localhost:7000/current/APPLE。 不知道如何连接。

【问题讨论】:

    标签: java spring-boot rsocket


    【解决方案1】:

    您不能将@RequestMapping 与套接字一起使用,请改用@MessageMapping

    我们将使用@MessageMapping 注解,而不是像Spring MVC 中的@RequestMapping 或@GetMapping 注解:

    @Controller
    public class MarketDataRSocketController {
    private final MarketDataRepository marketDataRepository;
    public MarketDataRSocketController(MarketDataRepository marketDataRepository) {
        this.marketDataRepository = marketDataRepository;
    }
    @MessageMapping("currentMarketData")
    public Mono<MarketData> currentMarketData(MarketDataRequest marketDataRequest) {
        return marketDataRepository.getOne(marketDataRequest.getStock());
    }
    

    【讨论】:

    • 感谢您的回复。我的理解是 MarketDataRSocketController 中的 MessageMapping 处理程序方法 currentMarketData() 预计将从 MarketDataRestController 中的 current() 调用。如果我错了,请纠正我。
    • 将 REST 客户端分离到它自己的 Web 应用程序中,并在 RSocket 服务器中调用 @MessageMapping 处理程序方法,但得到 org.springframework.messaging.MessageDeliveryException: No handler for destination ''
    • 上述问题已通过使用 RSocketRequester.Builder 来获得docs.spring.io/spring-boot/docs/2.2.0.M6/reference/html/… 中提到的 RSocketRequester 得到解决。感谢 Trisha Gee 在 stackoverflow.com/questions/57426578/… 中发表相同评论
    猜你喜欢
    • 2016-01-29
    • 2015-07-07
    • 1970-01-01
    • 2017-02-19
    • 2014-04-07
    • 1970-01-01
    • 2018-03-29
    • 2019-10-28
    • 1970-01-01
    相关资源
    最近更新 更多