title: Spring Boot - Error during WebSocket handshake
body: <p>When I tried using WebSocket in my spring boot application I got this error : "WebSocket connection to 'ws://localhost:8080/Moda/chatroomServerEndpoint' failed: Error during WebSocket handshake: Unexpected response code: 400" (Default.html-Line 11).. My aim : Send message by two different tabs (ex: in Google Chrome) Please help me! Thank you...</p>
  
  <p>Default.html</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;!&lt;!DOCTYPE html&gt;
  &lt;html lang="en"&gt;
   
  
  &lt;head&gt;
  &lt;meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"&gt;
  &lt;title&gt;Deneme&lt;/title&gt;
  
  &lt;script type = "text/javascript"&gt;
  
   var websocket = new WebSocket("ws://localhost:8080/Moda/chatroomServerEndpoint");
   websocket.onmessage = function processMessage(message) {
    var jsonData = JSON.parse(message.data);
    if(jsonData.message != null) messagesTextArea.value += jsonData.message + "\n";
   }
   function sendMessage(){
    websocket.send(messageText.value);
    messageText.value = "";
   }
  
  &lt;/script&gt;
  
   &lt;/head&gt;
   
    &lt;body&gt;
  &lt;textarea id="messagesTextArea" readonly = "readonly" rows="10" cols="45"&gt;&lt;/textarea&gt;&lt;br/&gt;
  &lt;input type = "text" id="messageText" size="50" /&gt;&lt;input type="button" value="Send" onclick="sendMessage();" /&gt;
     
   &lt;/body&gt;
    
    &lt;/html&gt;</code></pre>
  </div>
  </div>
  </p>
  
  <p>ModaApplication.java</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>package com.moda;
  
  import java.io.IOException;
  import java.io.StringWriter;
  import java.util.Collections;
  import java.util.HashSet;
  import java.util.Iterator;
  import java.util.Set;
  
  import javax.json.Json;
  import javax.json.JsonObject;
  import javax.json.JsonWriter;
  import javax.websocket.OnClose;
  import javax.websocket.OnMessage;
  import javax.websocket.OnOpen;
  import javax.websocket.Session;
  import javax.websocket.server.ServerEndpoint;
  
  import org.springframework.boot.SpringApplication;
  import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
  import org.springframework.context.annotation.Configuration;
  import org.springframework.context.annotation.Import;
  import org.springframework.web.socket.config.annotation.EnableWebSocket;
  
  
  
  import com.moda.videochat.MyWebSocketConfigurator;
  
  @Configuration
  @EnableAutoConfiguration
  @ServerEndpoint("/chatroomServerEndpoint")
  public class ModaApplication {
  
   public static void main(String[] args) {
    SpringApplication.run(ModaApplication.class, args);
   }
   static Set&lt;Session&gt; chatroomUsers = Collections.synchronizedSet(new HashSet&lt;Session&gt;());
   
   @OnOpen
   public void handleOpen(Session userSession)
   {
    chatroomUsers.add(userSession);
   }
   
   @OnMessage
   public void handleMessage(String message, Session userSession) throws IOException {
    String username = (String) userSession.getUserProperties().get("username");
    if(username == null)
    {
     userSession.getUserProperties().get("username");
     userSession.getBasicRemote().sendText(buildJsonData("System","you are now connected as " + message));
    }
    else
    {
     Iterator&lt;Session&gt; iterator = chatroomUsers.iterator();
     while(iterator.hasNext()) iterator.next().getBasicRemote().sendText(buildJsonData(username,message));
    }
   }
   
   @OnClose
   public void handleClose(Session userSession)
   {
    chatroomUsers.remove(userSession);
   }
   
   private String buildJsonData(String username, String message)
   {
    JsonObject jsonObject = Json.createObjectBuilder().add("message",username+": "+message).build();
    StringWriter stringWriter = new StringWriter();
    try (JsonWriter jsonWriter = Json.createWriter(stringWriter)) {jsonWriter.write(jsonObject);}
    return null;
   }
  
  }</code></pre>
  </div>
  </div>
  </p>