springmvc+websocket 拦截器进去以后到handler一直报空指针异常

作者:超级管理员 更新时间:2017-09-13 17:57:38 来源:未知 点击:15642
严重: Servlet.service() for servlet [springMVC] in context with path [/term] threw exception [Request 
严重: Servlet.service() for servlet [springMVC] in context with path [/term] threw exception [Request processing failed; nested exception is org.springframework.web.socket.server.HandshakeFailureException: Uncaught failure for request http://localhost:8080/term/hello.ws; nested exception is java.lang.NullPointerException] with root cause
java.lang.NullPointerException
at org.eclipse.jetty.websocket.server.WebSocketServerFactory.upgrade(WebSocketServerFactory.java:517)
at org.eclipse.jetty.websocket.server.WebSocketServerFactory.acceptWebSocket(WebSocketServerFactory.java:186)
at org.eclipse.jetty.websocket.server.WebSocketServerFactory.acceptWebSocket(WebSocketServerFactory.java:153)
at org.springframework.web.socket.server.jetty.JettyRequestUpgradeStrategy.upgrade(JettyRequestUpgradeStrategy.java:153)
at org.springframework.web.socket.server.support.DefaultHandshakeHandler.doHandshake(DefaultHandshakeHandler.java:221)
at org.springframework.web.socket.server.support.WebSocketHttpRequestHandler.handleRequest(WebSocketHttpRequestHandler.java:127)
at org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter.handle(HttpRequestHandlerAdapter.java:51)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:943)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:877)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:966)
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:857)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:622)
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:842)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:292)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:240)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)
at org.springframework.orm.hibernate4.support.OpenSessionInViewFilter.doFilterInternal(OpenSessionInViewFilter.java:150)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:240)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)
at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:240)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:212)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:94)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:504)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:141)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:620)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:502)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1132)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:684)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1539)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1495)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:745)

-------------------------------
最少把代码贴出来吧,只有错误很难判断
-------------------------------
package com.xpgk.test;


import org.eclipse.jetty.websocket.api.WebSocketBehavior;
import org.eclipse.jetty.websocket.api.WebSocketPolicy;
import org.eclipse.jetty.websocket.server.WebSocketServerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;
import org.springframework.web.socket.server.jetty.JettyRequestUpgradeStrategy;
import org.springframework.web.socket.server.standard.ServletServerContainerFactoryBean;
import org.springframework.web.socket.server.support.DefaultHandshakeHandler;
@Configuration
@EnableWebMvc
@EnableWebSocket
public class TestWebSocket extends WebMvcConfigurerAdapter implements WebSocketConfigurer{
@Override
public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
registry.addHandler(myHandler(), "hello.ws").addInterceptors(new WebSocketHandshakeInterceptor()).setHandshakeHandler(handshakeHandler());
}
@Bean
public ChatWebSocketHandler myHandler(){
return new ChatWebSocketHandler();
}
@Bean
public ServletServerContainerFactoryBean createWebSocketContainer() {
ServletServerContainerFactoryBean container = new ServletServerContainerFactoryBean();
    container.setMaxTextMessageBufferSize(8192);
    container.setMaxBinaryMessageBufferSize(8192);
    return container;
 }
 @Bean
    public DefaultHandshakeHandler handshakeHandler() {
        WebSocketPolicy policy = new WebSocketPolicy(WebSocketBehavior.SERVER);
        policy.setInputBufferSize(8192);
        policy.setIdleTimeout(600000);
        return new DefaultHandshakeHandler(
                new JettyRequestUpgradeStrategy(new WebSocketServerFactory(policy)));
    }
}
-------------------------------
package com.xpgk.test;

import java.util.Map;

import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.web.socket.WebSocketHandler;
import org.springframework.web.socket.server.support.HttpSessionHandshakeInterceptor;
public class WebSocketHandshakeInterceptor extends HttpSessionHandshakeInterceptor{

@Override
public void afterHandshake(ServerHttpRequest request, ServerHttpResponse response, WebSocketHandler wsHandler,
Exception ex) {
System.out.println("456789");
System.out.println(request.getURI());
super.afterHandshake(request, response, wsHandler, ex);
}

@Override
public boolean beforeHandshake(ServerHttpRequest arg0, ServerHttpResponse arg1, WebSocketHandler arg2,
Map<String, Object> arg3) throws Exception {
System.out.println("123456789");
System.out.println(arg0.getRemoteAddress());
System.out.println(arg0.getHeaders());
System.out.println(arg0.getURI());
System.out.println(arg1.getHeaders());
System.out.println(arg2);
System.out.println(arg3);
return super.beforeHandshake(arg0, arg1, arg2, arg3);
}



}
-------------------------------
package com.xpgk.test;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.springframework.web.socket.CloseStatus;
import org.springframework.web.socket.TextMessage;
import org.springframework.web.socket.WebSocketSession;
import org.springframework.web.socket.handler.TextWebSocketHandler;

import javax.servlet.annotation.HandlesTypes;
import javax.websocket.OnClose;
import javax.websocket.OnError;
import javax.websocket.OnMessage;
//import javax.websocket.*;
import javax.websocket.OnOpen;

@SuppressWarnings("unused")

public class ChatWebSocketHandler extends TextWebSocketHandler{
 private final static List<WebSocketSession> sessions = Collections.synchronizedList(new ArrayList<WebSocketSession>());
    //接收文本消息,并发送出去
    @Override
    @OnMessage
    protected void handleTextMessage(WebSocketSession session, TextMessage message) throws Exception {
     //接收到客户端消息时调用
     System.out.println("text message:" + session.getId() + "-" + message.getPayload()); 
//         super.handleTextMessage(session, message);
    }
    //连接建立后处理
    @SuppressWarnings("unchecked")
    @Override
    @OnOpen
    public void afterConnectionEstablished(WebSocketSession session) throws Exception {
     // 与客户端完成连接后调用 
     System.out.println("afterConnectionEstablished"); 
//      System.out.println("getId:" + session.getId()); 
//      System.out.println("getLocalAddress:" + session.getLocalAddress().toString()); 
//      System.out.println("getTextMessageSizeLimit:" + session); 
//      System.out.println("getUri:" + session.getUri().toString()); 
//      System.out.println("getPrincipal:" + session.getPrincipal()); 
//      String s="你好";
//      CharSequence b="你好";
//      session.sendMessage(new TextMessage(b)); 
     session.sendMessage(new TextMessage("connect"));  
        session.sendMessage(new TextMessage("new_msg"));
//         //处理离线消息
    }
    //抛出异常时处理
    @Override
    @OnError
    public void handleTransportError(WebSocketSession session, Throwable exception) throws Exception {
     // 消息传输出错时调用 
     System.out.println("handleTransportError"); 
    }
    //连接关闭后处理
    @Override
    @OnClose
    public void afterConnectionClosed(WebSocketSession session, CloseStatus closeStatus) throws Exception {
     // 一个客户端连接断开时关闭 
     System.out.println("afterConnectionClosed"); 
    }

    @Override
    public boolean supportsPartialMessages() {
        return false;
    }
}

推荐阅读

热门内容

求问大神java空指针异常

--------------------...

求问大神javaGui空指针异常

--------------------...

诡异的问题,在return的时候,出问题

--------------------...

SpringMvc中如何将数组在前台遍历

各位大佬求助,因为前台使用的是HTML ...

Ajax 跨越访问后台,返回出错

$.ajax({            ...

jdk1.8和jdk1.7中HashMa

本帖最后由kai_12于2017-09-...

明明有分号,怎么还提示没有分号

--------------------...

jsp用ajax提交form表单

没接触过ajax,找了网上的例子试了下。...

关于HQL的一个报错

本帖最后由zxy16305于2017-0...

这哪错了???

--------------------...

最新内容

求问大神java空指针异常

-------------------------------构造器内声明的jf...

求问大神javaGui空指针异常

-------------------------------39行空指针。。。...

求一个div能够拖动的代码

如题-------------------------------http://...

springboot 整合 mybatis 封装dao的问题

dao接口:public interface AdminDao {method ...

诡异的问题,在return的时候,出问题了

-------------------------------所以debug的重...

JavaObject类中的registNatives()方法实现了那些事情

百度上普遍说注册本地方法wait、notify、notifyAll、hashCo...

ajax请求成功后怎么把data数据显示到页面上

 success: function (data){         var s...

SpringMvc中如何将数组在前台遍历

各位大佬求助,因为前台使用的是HTML  没有使用jstl遍历在controll...

springmvc+websocket 拦截器进去以后到handler一直报空指针异常

严重: Servlet.service() for servlet [sprin...

JAVA虚拟机java栈内存的问题!!

最近在学习JVM,学到java栈的部分,看到书上说栈溢出是因为给栈分配的内存不够...

maven 项目运行不了

maven项目有多个模块,分别是model、dao、service、web(使用...

SSH2项目下的所有JavaBean包括Struts2的所有Action和拦截器等都受Spring IoC容器管理吗?

SSH2项目下的所有JavaBean包括Struts2的所有Action和拦截器...

下载word文件打不开

使用response 方式,使用浏览器从服务器端下载文件,文件下载成功,并且文件...

uploadify插件问题,小白求教啊!

文件都已经成功上传了,但是谷歌浏览器可以成功调用onUploadSuccess函...

php页面能够获取到后台设置的session吗?

如果前端通过JS提交了一次用户登录的表单,后台验证成功后设置了该用户登录状态的s...

Ajax 跨越访问后台,返回出错

$.ajax({            url: requestUrl,   /...

自动加载类,__autoload 是如何实现的

麻烦大神解释下 这个例子吧 什么文件名 减6 的 什么Action 的  都干嘛...

spring 配置时遇到的一个奇怪的问题

使用spring  xml 引入了properties文件  

请问写原生的php程序需要注意什么事项?如何做到安全最大化?linux环境

因为最近在写一个微信小程序。功能很简单主要有1:手机号注册(通过短信形式)2:用...

引用其他人写的方法出现问题,大家帮忙看看,谢谢了

错误如下图上面是别人写的加载天地图的方法,在他给的工程里可以运行,我新建一个挪过...