Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Handle the same URL with Spring MVC RequestMappingHandlerMapping and Spring Websocket's ServletWebSocketHandlerRegistry

What do I want to have:

  • Client sends GET / HTTP/1.1(without Connection: upgrade) - this request should be handled by RequestMappingHandlerMapping
  • Client sends Connection: upgrade along with GET request - this request should be handled by ServletWebSocketHandlerRegistry

My Java configuration:

@Configuration
@EnableWebSocket
public class WebsocketConfiguration extends WebMvcConfigurationSupport 
                                    implements WebSocketConfigurer {
    @Bean
    WebsocketComponent wsHandler() {
        return new WebsocketComponent();
    }

    @Override
    public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
        registry.addHandler(wsHandler(), "/").setAllowedOrigins("*");
    }
}

My webmvc controller:

@Controller
public class Status {
    @RequestMapping(value = "/", method = RequestMethod.GET)
    public String status() {
        return "OK";
    }
}

The problem is - when MVC controller taking precedence, it always respond with HTTP 200, WebSocket handler never reached. When WebSocket handler have precedence - it works with WebSocket client, but when I try http client(browser) it responds with Can "Upgrade" only to "WebSocket". Is it possible to replace somehow this error page with fallback to my MVC mapping? Any other configurations to make what I described first?

like image 474
vitalyster Avatar asked Jul 14 '16 14:07

vitalyster


People also ask

What do Restcontroller bypass and instead return a body?

Therefore, @RestControllers, by default, return XML/JSON, instead of HTML.

What is handler method annotation in Spring MVC?

annotation. RequestMapping annotation is used to map web requests onto specific handler classes and/or handler methods. @RequestMapping can be applied to the controller class as well as methods. Today we will look into various usage of this annotation with example and other annotations @PathVariable and @RequestParam .

What is true about @RequestMapping annotation in Spring MVC?

The @RequestMapping annotation can be applied to class-level and/or method-level in a controller. The class-level annotation maps a specific request path or pattern onto a controller. You can then apply additional method-level annotations to make mappings more specific to handler methods.

What is the use of handler mapping in Spring MVC?

Using a handler mapping you can map incoming web requests to appropriate handlers. There are some handler mappings you can use out of the box, for example, the SimpleUrlHandlerMapping or the BeanNameUrlHandlerMapping , but let's first examine the general concept of a HandlerMapping .


1 Answers

The problem is - when MVC controller taking precedence, it always respond with HTTP 200, WebSocket handler never reached

When RequestMappingHandlerMapping takes precedence over WebSocketHandlerMapping, for a request to an endpoint that both of them can handle (If you just consider the URL), DispatcherServlet would dispatch the request to @RequestMapping methods, not the WebSocket handler. In order to solve this problem, restrict @RequestMapping method to just serve the request without Connection:Upgrade header:

@Controller
public class Status {
    @RequestMapping(value = "/", method = GET, headers = "Connection!=Upgrade")
    public String status() {
        return "OK";
    }
}

This way, when DispatcherServlet searches for a handler for that common endpoint, it would consider the presence or absence of the Connection:Upgrade header to determine the right handler to fulfill the request.

like image 79
Ali Dehghani Avatar answered Oct 25 '22 13:10

Ali Dehghani