I am configuring Websockets in Spring basically by following the guide provided in the documentation.
I am currently trying to send a message from the server to the client as explained in the section "Sending messages from anywhere"
Following the example, you can Autowire a class called SimpMessagingTemplate
@Controller
public class GreetingController {
private SimpMessagingTemplate template;
@Autowired
public GreetingController(SimpMessagingTemplate template) {
this.template = template;
}
@RequestMapping(value="/greetings", method=POST)
public void greet(String greeting) {
String text = "[" + getTimestamp() + "]:" + greeting;
this.template.convertAndSend("/topic/greetings", text);
}
}
However, my current project cannot find the bean "SimpMessagingTemplate". (Intellij: 'Could not autowire. No beans of SimpMessagingTemplate type found'.
I have check several examples in the internet but I cannot find how to get Spring to create an instance of SimpMessagingTemplate. How can I Autowire it ?
EDIT:
I decided to send some more background information. This is my current websocket configuration:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:websocket="http://www.springframework.org/schema/websocket"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/websocket
http://www.springframework.org/schema/websocket/spring-websocket-4.0.xsd">
<!-- TODO properties to be read from a properties file -->
<websocket:message-broker application-destination-prefix="/app">
<websocket:stomp-endpoint path="/new_session" >
<websocket:sockjs/>
</websocket:stomp-endpoint>
<websocket:simple-broker prefix="/topic"/>
</websocket:message-broker>
</beans>
Websocket works with this controller
@Controller
public class SessionController {
private static final Logger log = LoggerFactory.getLogger(SessionController.class);
@MessageMapping("/new_session")
@SendTo("/topic/session")
public SessionStatus newSession(Session session) throws Exception {
Thread.sleep(3000); // simulated delay
log.info("Response sent !!");
return new SessionStatus("StatusReport, " + session.toString() + "!");
}
}
I just not sure how to to make this work
public class SessionController {
private static final Logger log = LoggerFactory.getLogger(SessionController.class);
private SimpMessagingTemplate template;
@Autowired
public SessionController(SimpMessagingTemplate template) {
this.template = template;
}
}
As the bean "SimpMessagingTemplate template" is not found. Spring documentation does not offer more details regarding this matter.
EDIT: Example of working code in github
I had the same problem, the error occurred because my websocket config file:
@Configuration
@EnableWebSocketMessageBroker
@EnableScheduling
public class WebSocketConfig extends AbstractWebSocketMessageBrokerConfigurer {
}
was not scanned by spring.
so the fix was to add the package with this configuration file to the scanned packages.