国产片侵犯亲女视频播放_亚洲精品二区_在线免费国产视频_欧美精品一区二区三区在线_少妇久久久_在线观看av不卡

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術(shù)|正則表達(dá)式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務(wù)器之家 - 編程語言 - Java教程 - Spring WebSocket 404錯誤的解決方法

Spring WebSocket 404錯誤的解決方法

2021-03-02 11:01Silent_Paladin Java教程

這篇文章主要為大家詳細(xì)介紹了Spring WebSocket 404錯誤的解決方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下

近來學(xué)習(xí) spring websocket 時按照 spring in action 中示例編寫代碼,運行時瀏覽器報404 錯誤

websocket connection to 'ws://localhost/websocket/marco' failed: error during websocket handshake: unexpected response code: 404

Spring WebSocket 404錯誤的解決方法

按照 spring in action 中步驟:
首先,繼承 abstractwebsockethandler,重載以下 3 個方法:
- handletextmessage – 處理文本類型消息
- afterconnectionestablished – 新連接建立后調(diào)用
- afterconnectionclosed – 連接關(guān)閉后調(diào)用

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import org.springframework.web.socket.closestatus;
import org.springframework.web.socket.textmessage;
import org.springframework.web.socket.websocketsession;
import org.springframework.web.socket.handler.abstractwebsockethandler;
 
public class marcohandler extends abstractwebsockethandler {
 
 protected void handletextmessage(websocketsession session, textmessage message) throws exception {
  system.out.println("received message: " + message.getpayload());
  thread.sleep(2000);
  session.sendmessage(new textmessage("polo!"));
 }
 
 @override
 public void afterconnectionestablished(websocketsession session) {
  system.out.println("connection established!");
 }
 
 @override
 public void afterconnectionclosed(websocketsession session, closestatus status) {
  system.out.println("connection closed. status: " + status);
 }
 
}

其次,使用 javaconfig 啟用 websocket 并映射消息處理器

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
import org.springframework.context.annotation.bean;
import org.springframework.web.socket.config.annotation.enablewebsocket;
import org.springframework.web.socket.config.annotation.websocketconfigurer;
import org.springframework.web.socket.config.annotation.websockethandlerregistry;
 
@enablewebsocket
public class websocketconfig implements websocketconfigurer {
 
 @override
 public void registerwebsockethandlers(websockethandlerregistry registry) {
  registry.addhandler(marcohandler(), "/marco");
 }
 
 @bean
 public marcohandler marcohandler() {
  return new marcohandler();
 }
 
}

最后,編寫前端 js 代碼發(fā)起連接請求及后續(xù)消息交互

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
var url = 'ws://' + window.location.host + '/websocket/marco';
var sock = new websocket(url);
 
sock.onopen = function() {
 console.log('opening');
 sock.send('marco!');
};
 
sock.onmessage = function(e) {
 console.log('received message: ', e.data);
 settimeout(function() {
  saymarco()
 }, 2000);
};
 
sock.onclose = function() {
 console.log('closing');
};
 
function saymarco() {
 console.log('sending marco!');
 sock.send('marco!');
}

部署后打開瀏覽器運行,直接報 404 錯誤

Spring WebSocket 404錯誤的解決方法

上網(wǎng)搜索了一晚上解決方案,包括參考 stackoverflow.com 上的經(jīng)驗都未解決該問題,直到查看到以下文章:
spring集成websocket頁面訪問404問題的解決方法

在此自己也做個記錄避免以后遺忘。

websocket 實質(zhì)上借用 http 請求進(jìn)行握手,啟用 spring websocket 需要在 org.springframework.web.servlet.dispatcherservlet 里配置攔截此請求。

以下是解決步驟:

首先,修改 websocketconfig 類定義,在類上添加 @configuration 注解,表明該類以 javaconfig 形式用作 bean 定義的源(相當(dāng)于 xml 配置中的 <beans> 元素)。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import org.springframework.context.annotation.bean;
import org.springframework.context.annotation.configuration;
import org.springframework.web.socket.config.annotation.enablewebsocket;
import org.springframework.web.socket.config.annotation.websocketconfigurer;
import org.springframework.web.socket.config.annotation.websockethandlerregistry;
 
@configuration
@enablewebsocket
public class websocketconfig implements websocketconfigurer {
 
 @override
 public void registerwebsockethandlers(websockethandlerregistry registry) {
  registry.addhandler(marcohandler(), "/marco");
 }
 
 @bean
 public marcohandler marcohandler() {
  return new marcohandler();
 }
 
}

其次,使用 javaconfig 配置 dispatcherservlet,繼承org.springframework.web.servlet.support.abstractannotationconfigdispatcherservletinitializer ,重載以下 3 個方法:
- getrootconfigclasses – 返回帶有 @configuration 注解的類將會用來配置 contextloaderlistener 創(chuàng)建的應(yīng)用上下文中的 bean
- getservletconfigclasses – 返回帶有 @configuration 注解的類將會用來定義 dispatcherservlet 應(yīng)用上下文中的 bean
- getservletmappings – 將一個或多個路徑映射到 dispatcherservlet 上

實際上,如果只需要 spring websocket 生效,則只需要在 getservletconfigclasses 方法中返回用來定義 dispatcherservlet 應(yīng)用上下文中的 bean

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
import org.springframework.web.servlet.support.abstractannotationconfigdispatcherservletinitializer;
 
public class websocketinitializer extends abstractannotationconfigdispatcherservletinitializer {
 
 @override
 protected class<?>[] getrootconfigclasses() {
  return null;
 }
 
 @override
 protected class<?>[] getservletconfigclasses() {
  return new class<?>[] {websocketconfig.class};
 }
 
 @override
 protected string[] getservletmappings() {
  return new string[] {"/"};
 }
 
}

重新部署后代開瀏覽器運行成功

客戶端消息

Spring WebSocket 404錯誤的解決方法

服務(wù)器消息

Spring WebSocket 404錯誤的解決方法

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。

原文鏈接:http://blog.csdn.net/silent_paladin/article/details/78269929

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 精品国产一区二区三区久久久蜜月 | 在线视频 亚洲 | 日韩精品三区 | 国产精品国产精品国产专区不片 | 久久99精品国产麻豆婷婷洗澡 | 亚洲欧美日韩国产综合精品二区 | 亚洲人视频在线 | 精品免费视频 | 草草视频网站 | 中文字幕日韩在线视频 | 午夜精品一区二区三区在线视频 | 成人在线不卡 | 中文字幕,久热精品,视频在线 | 日日操操 | 这里只有精品视频在线 | 波多野结衣福利电影 | 久久精品国产99国产精2020新增功能 | 日韩欧美一二三区 | 亚洲va中文字幕 | 国产成人精品视频 | 99伊人| 国产精品久久久久aaaa | 日本三级韩国三级三级a级中文 | 四虎免费在线播放 | 国产欧美精品一区二区三区四区 | 福利片在线免费观看 | 国产精品国产精品国产专区不片 | 日韩欧美一区二区三区久久婷婷 | 国产精品久久久久久av公交车 | 久久久高清 | 一本在线 | 97国产超碰 | 含羞草www国产在线视频 | 99re6在线视频精品免费 | www.日韩系列 | 看亚洲a级一级毛片 | 韩日精品一区 | 91精品国产欧美一区二区成人 | 精品久草 | 另类国产ts人妖高潮系列视频 | 久久精品国产亚洲一区二区三区 |