我正在使用Stomp编写WebSocket客户端,当我使用Stomp客户端发送请求时,日志输出为:
15:17:44.688]-[clientInboundChannel-59]-[org.springframework.web.socket.messaging.WebSocketAnnotationMethodMessageHandler]-{Searching methods to handle SEND /app/vehicle session=qnlerizz}
15:17:44.688]-[clientInboundChannel-59]-[org.springframework.web.socket.messaging.WebSocketAnnotationMethodMessageHandler]-{No matching methods.}
表示客户端没有问题。服务器找不到匹配的URL来处理请求。但我已经有了一个处理方法。这是代码:
@Controller
@Log4j2
public class WebSocketController {
public SimpMessagingTemplate template;
@Autowired
public WebSocketController(SimpMessagingTemplate template) {
this.template = template;
}
@MessageMapping("/vehicle")
@SendTo("/topic/location")
public void getloc() throws Exception {
try {
for (int i = 0; i < 10; i++) {
template.convertAndSend("/topic/location", "aaaaaaa");
}
} catch (Exception e) {
log.error(e);
}
}
}
这是我的web配置:
public class WebSocketConfig extends AbstractWebSocketMessageBrokerConfigurer {
@Override
public void configureMessageBroker(MessageBrokerRegistry config) {
config.enableSimpleBroker("/topic");
config.setApplicationDestinationPrefixes("/app");
}
@Override
public void registerStompEndpoints(StompEndpointRegistry registry) {
registry.addEndpoint("/vehicle").withSockJS();
}
}
这是客户端代码:
function connect() {
var socket = new SockJS('/clbs/vehicle');
stompClient = Stomp.over(socket);
stompClient.connect({}, function (frame) {
setConnected(true);
console.log('Connected: ' + frame);
stompClient.subscribe('/topic/location', function (greeting) {
showGreeting(JSON.parse(greeting.body).content);
});
});
}
stompClient.send("/app/vehicle",{},JSON.stringify("bbb"));
我该怎么办?哪里出了问题?
PS:控制器WebSocketController
已经自动扫描。
1.8Tomcat: 8.0.36春天:4.2.6
这个问题我已经纠结了2天了
确保控制器自动扫描成功。我遇到这个问题是因为bean没有扫描。因此无法匹配属性方法来处理stomp客户端请求。检查自动扫描配置。搜索历史日志内容,汇总的详细信息在这里。