1. 程式人生 > 其它 >springboot 加入websocket後,ServerEndpointExporter配置不識別-解決

springboot 加入websocket後,ServerEndpointExporter配置不識別-解決

1.背景

springboot 加入websocket,需要配置ServerEndpointExporter的bean,發現沒法識別

2.原因

springboot 內建了tomcat,內建 的tomcat與websocket不相容,因此需要將 -start-web裡的tomcat排除掉即可

3.解決

  <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-tomcat</artifactId>
                </exclusion>
            </exclusions>
</dependency>

4.bug

tomcat排除掉後,會導致介面的檔案傳輸物件【  MultipartFile  】沒有tomcat封裝方法,導致以前的簡易操作檔案方式用不了,只能通過流來處理

4.bug解決

我自己封裝了個方法

//有websocket的專案使用這個辦法
FileUtil.writeFile(vo.getFile().getInputStream(), aboPath);
 //寫入檔案
    public static boolean writeFile(InputStream fis, String tarFilePath) {
        
try { FileOutputStream fos = new FileOutputStream(tarFilePath); byte[] b = new byte[1024]; while ((fis.read(b)) != -1) { fos.write(b);// 寫入資料 } fis.close(); fos.close();// 儲存資料 } catch (Exception e) {
return false; } return true; }