问题描述
package com.openiot.websocket;import java.io.IOException;import javax.websocket.OnClose;import javax.websocket.OnMessage;import javax.websocket.OnOpen;import javax.websocket.Session;import javax.websocket.server.ServerEndpoint;import org.hibernate.service.spi.InjectService;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Component;import org.springframework.stereotype.Controller;import com.openiot.entity.Device;import com.openiot.entity.ResponseResult;import com.openiot.service.DeviceService;//@Component@ServerEndpoint(value="/websocket")public class TestWebsocket{private Session session;@Autowiredprivate DeviceService deviceService;@OnOpen public void open(Session session) { this.session = session; System.out.println("open"); } @OnMessage public void inMessage(String message) { try { Device device = deviceService.findDeviceById(Long.parseLong(message)); System.out.println(device); session.getBasicRemote().sendText(device.name);} catch (Exception e) {e.printStackTrace();} System.out.println("*** WebSocket Received from sessionId " + this.session.getId() + ": " + message); } @OnClose public void end() { System.out.println("*** WebSocket closed from sessionId " + this.session.getId()); } }debug结果是device为空,在类前面加@Component也试过,也是同样的问题!想请大家分析下
解决方案
配置文件呢贴上来啊
解决方案二:
能告诉下怎么解决吗?我也遇到了这个问题
解决方案三:
感觉好吊啊。