正在尝试按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);
    }
}


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

最佳答案

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


  代替Spring MVC中的@RequestMapping或@GetMapping批注,我们将使用@MessageMapping批注:

@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());
}

09-12 00:19