Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Partial Path pattern not match headers #34322

Closed
cn19 opened this issue Jan 25, 2025 · 2 comments
Closed

Partial Path pattern not match headers #34322

cn19 opened this issue Jan 25, 2025 · 2 comments
Labels
in: web Issues in web modules (web, webmvc, webflux, websocket) status: invalid An issue that we don't feel is valid

Comments

@cn19
Copy link

cn19 commented Jan 25, 2025

I want to use to use PostMapping with path pattern all ("/") and header condition "Connection!=Upgrade". But when i send a websocket request which always contains header "Connection=Upgrade", it match the "/" method and give message "Method GET not supportted". I debug the program and find the "/**" method is selected and throw execption when method not matched in org.springframework.web.servlet.mvc.method.RequestMappingInfoHandlerMapping#handleNoMatch.

The DispatchSelvet iterate HandlerMapping to get request handler. RequestMappingInfoHandlerMapping fallback to handleNoMatch() when no method found. It use PartialMatchHelper to get matched path pattern matched conditions. Then throw exception if method/ consume/ params not matched. It also does not match headers.

  1. As RequestMappingInfoHandlerMapping is the first attempt in DispatchSevlet, other HandlerMapping will ignored when it throws exception.
  2. When path pattern matches but any other condition not matches, it should return nothing matched and try match other handlers.
@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Jan 25, 2025
@cn19
Copy link
Author

cn19 commented Jan 25, 2025

My PostMappping shows bellow.

    @PostMapping(path = "/**", headers = {"Connection!=Upgrade"})
    public void fallback(HttpServletRequest request, HttpServletResponse response) throws IOException {
    }

I find the handleNoMatch() method is just give mismatch information why no handler matches request. I think logging is ok.

@bclozel
Copy link
Member

bclozel commented Jan 27, 2025

Hello @cn19

The behavior you're describing is by design: the RequestMappingInfoHandlerMapping is meant to handle partial matches and send HTTP responses accordingly. The mapping you are sharing is also quite unusual for an application: mapping the entire path for POST methods is usually covered by a Servlet directly.

We won't change the RequestMappingInfoHandlerMapping behavior for this, but I believe you can update your configuration to support this case. I think you could contribute to your application context a BeanPostProcessor bean that reorders the websocket handler ahead of the request mapping one. Currently, the websocket is ordered at "1" and the request mapping one at "0" (lower is earlier).

You can use this class and contribute it as a bean to your application context.

	class WebSocketReorderingPostProcessor implements BeanPostProcessor {

		@Override
		public @Nullable Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
			if (bean instanceof WebSocketHandlerMapping handlerMapping) {
				handlerMapping.setOrder(-1);
			}
			return bean;
		}
	}

We will not be applying this order change by default in Spring Framework as this setup is really unusual and ordering the websocket handler ahead would introduce a performance hit for most applications.

@bclozel bclozel closed this as not planned Won't fix, can't repro, duplicate, stale Jan 27, 2025
@bclozel bclozel added in: web Issues in web modules (web, webmvc, webflux, websocket) status: invalid An issue that we don't feel is valid and removed status: waiting-for-triage An issue we've not yet triaged or decided on labels Jan 27, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: web Issues in web modules (web, webmvc, webflux, websocket) status: invalid An issue that we don't feel is valid
Projects
None yet
Development

No branches or pull requests

3 participants