Spring 中 SimpMessagingTemplate 的 NullPointerException
NullPointerException on SimpMessagingTemplate in Spring
我正在构建一个应用程序,它将使用 STOMP 通过 websockets 发送消息。我想在没有客户端发出请求的情况下发送消息。根据 documentation,我可以使用 convertAndSend 来做到这一点。
但是,当我尝试执行此操作时,出现空指针异常。请看下面的代码:
public class ParseJSON {
@Autowired
private SimpMessagingTemplate template;
public void getDetails(String json) {
try {
Tweet status = sendDetails(TwitterObjectFactory.createStatus(json));
sentToWebApp(status);
} catch (TwitterException e) {
e.printStackTrace();
}
}
@Scheduled(fixedDelay = 50)
private void sentToWebApp(Tweet status) {
System.out.println(status);
this.template.convertAndSend("/tweet/update", status);
}
}
堆栈跟踪:
java.lang.NullPointerException: null
at org.myproject.worker.ParseJSON.sentToWebApp(ParseJSON.java:62)
at org.myproject.worker.ParseJSON.getDetails(ParseJSON.java:51)
at org.myproject.worker.TwitterClient.run(TwitterClient.java:50)
at org.myproject.Controllers.TweetController.greeting(TweetController.java:37)
任何人都可以了解我的情况,以便我能够通过 websocket 发送消息而不会遇到异常。
提前致谢。
您的模板变量未正确自动装配,这就是它给出 nullpointerException 的原因。
您可以检查 spring 配置以正确自动装配
我正在构建一个应用程序,它将使用 STOMP 通过 websockets 发送消息。我想在没有客户端发出请求的情况下发送消息。根据 documentation,我可以使用 convertAndSend 来做到这一点。
但是,当我尝试执行此操作时,出现空指针异常。请看下面的代码:
public class ParseJSON {
@Autowired
private SimpMessagingTemplate template;
public void getDetails(String json) {
try {
Tweet status = sendDetails(TwitterObjectFactory.createStatus(json));
sentToWebApp(status);
} catch (TwitterException e) {
e.printStackTrace();
}
}
@Scheduled(fixedDelay = 50)
private void sentToWebApp(Tweet status) {
System.out.println(status);
this.template.convertAndSend("/tweet/update", status);
}
}
堆栈跟踪:
java.lang.NullPointerException: null
at org.myproject.worker.ParseJSON.sentToWebApp(ParseJSON.java:62)
at org.myproject.worker.ParseJSON.getDetails(ParseJSON.java:51)
at org.myproject.worker.TwitterClient.run(TwitterClient.java:50)
at org.myproject.Controllers.TweetController.greeting(TweetController.java:37)
任何人都可以了解我的情况,以便我能够通过 websocket 发送消息而不会遇到异常。
提前致谢。
您的模板变量未正确自动装配,这就是它给出 nullpointerException 的原因。
您可以检查 spring 配置以正确自动装配