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

Requests that should be handled by an additional DispatcherServlet result in a 404 response #22682

Closed
DreamPWJ opened this issue Jul 31, 2020 · 11 comments
Assignees
Labels
type: regression A regression from a previous release
Milestone

Comments

@DreamPWJ
Copy link

I use the ServletRegistrationBean method to add the servlet prefix '/ API.' uniformly. Version 2.3.1 prompts or does not exist 404, while version 2.3.0 allows normal access to the/API / path

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Jul 31, 2020
@wilkinsona
Copy link
Member

Thanks for the report but I cannot reproduce the behaviour that you have described. The following works as excepted:

package com.example.demo;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;

@SpringBootApplication
public class Gh22682Application {

	public static void main(String[] args) {
		SpringApplication.run(Gh22682Application.class, args);
	}
	
	@Bean
	public ServletRegistrationBean<ExampleServlet> exampleServletRegistrationBean() {
		ServletRegistrationBean<ExampleServlet> registration = 
				new ServletRegistrationBean<Gh22682Application.ExampleServlet>(new ExampleServlet());
		registration.addUrlMappings("/ping");
		return registration;
	}
	
	@SuppressWarnings("serial")
	static class ExampleServlet extends HttpServlet {

		@Override
		protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
			resp.getWriter().println("pong");
			resp.flushBuffer();
		}
		
	}

}
$ curl localhost:8080/ping
pong

If you would like us to spend some more time investigating, please spend some time providing a complete yet minimal sample that reproduces the problem. You can share it with us by pushing it to a separate repository on GitHub or by zipping it up and attaching it to this issue.

@wilkinsona wilkinsona added the status: waiting-for-feedback We need additional information before we can continue label Jul 31, 2020
@DreamPWJ
Copy link
Author

DreamPWJ commented Jul 31, 2020

@Configuration
public class ServletConfig {

    @Resource
    private MultipartConfigElement multipartConfigElement;
    
    @Bean
    public ServletRegistrationBean apiServletBean(WebApplicationContext wac) {
        DispatcherServlet servlet = new DispatcherServlet(wac);
        servlet.setThrowExceptionIfNoHandlerFound(true);
        ServletRegistrationBean bean = new ServletRegistrationBean(servlet);
        bean.setLoadOnStartup(1);
        bean.addUrlMappings("/api/*");
        bean.setMultipartConfig(multipartConfigElement);
        bean.setName("apiServlet");
        return bean;
    }
}

@spring-projects-issues spring-projects-issues added status: feedback-provided Feedback has been provided and removed status: waiting-for-feedback We need additional information before we can continue labels Jul 31, 2020
@DreamPWJ
Copy link
Author

image

@wilkinsona
Copy link
Member

Thanks, but that doesn't give us enough information to diagnose the problem. For example, I can't tell what handlers are in your application and how they are getting registered with your (additional?) DispatcherServlet. As I said above, if you would like us to spend some more time investigating, please spend some time providing a complete yet minimal sample that reproduces the problem. By complete, I mean something that provides everything that we need to reproduce the problem.

@wilkinsona wilkinsona added status: waiting-for-feedback We need additional information before we can continue and removed status: feedback-provided Feedback has been provided labels Jul 31, 2020
@spring-projects-issues
Copy link
Collaborator

If you would like us to look at this issue, please provide the requested information. If the information is not provided within the next 7 days this issue will be closed.

@spring-projects-issues spring-projects-issues added the status: feedback-reminder We've sent a reminder that we need additional information before we can continue label Aug 7, 2020
@DreamPWJ
Copy link
Author

DreamPWJ commented Aug 7, 2020

The above has provided the core code for the replication has not the replication?

@spring-projects-issues spring-projects-issues added status: feedback-provided Feedback has been provided and removed status: waiting-for-feedback We need additional information before we can continue status: feedback-reminder We've sent a reminder that we need additional information before we can continue labels Aug 7, 2020
@wilkinsona
Copy link
Member

Unfortunately not, no. As I said above, it doesn't give us enough information to reproduce the problem. For example, I can't tell what handlers are in your application and how they are getting registered with your (additional?) DispatcherServlet.

@wilkinsona wilkinsona added status: feedback-reminder We've sent a reminder that we need additional information before we can continue status: waiting-for-feedback We need additional information before we can continue and removed status: feedback-provided Feedback has been provided labels Aug 7, 2020
@DreamPWJ
Copy link
Author

DreamPWJ commented Aug 7, 2020

run tomcat?

@spring-projects-issues spring-projects-issues added status: feedback-provided Feedback has been provided and removed status: waiting-for-feedback We need additional information before we can continue status: feedback-reminder We've sent a reminder that we need additional information before we can continue labels Aug 7, 2020
@wilkinsona
Copy link
Member

Sorry, but I'm not sure how running Tomcat will help. Your screenshot shows a request being made to /api/token/base but the code that you have provided thus far has no such mapping. Without knowing how that mapping is configured, it's impossible for us to diagnose why you're getting a 404. Please spend some time providing a complete yet minimal sample that reproduces the problem. You can share it with us by pushing it to a separate repository on GitHub or by zipping it up and attaching it to this issue.

@wilkinsona wilkinsona added status: feedback-reminder We've sent a reminder that we need additional information before we can continue status: waiting-for-feedback We need additional information before we can continue and removed status: feedback-provided Feedback has been provided labels Aug 7, 2020
@DreamPWJ
Copy link
Author

DreamPWJ commented Aug 8, 2020

please check https://github.com/DreamPWJ/spring-boot-bug.git

spring boot2.3.2
curl http://127.0.0.1:8080/api/token/base
{"timestamp":"2020-08-08T00:35:22.506+00:00","status":404,"error":"Not Found","message":"","path":"/api/token/base"}

spirng boot2.3.0
curl http://127.0.0.1:8080/api/token/base
success

@spring-projects-issues spring-projects-issues added the status: feedback-provided Feedback has been provided label Aug 8, 2020
@spring-projects-issues spring-projects-issues removed status: waiting-for-feedback We need additional information before we can continue status: feedback-reminder We've sent a reminder that we need additional information before we can continue labels Aug 8, 2020
@wilkinsona
Copy link
Member

Thanks for the sample. I can now see what's happening. This is a regression caused by the changes made for #21499. In your case, there are two dispatcher servlets, one that is mapped to / and one that is mapped to /api/*. The / mapping results in setAlwaysUseFullPath(true) being called on the UrlPathHelper:

if (servletUrlMapping.equals("/")) {
UrlPathHelper urlPathHelper = new UrlPathHelper();
urlPathHelper.setAlwaysUseFullPath(true);
configurer.setUrlPathHelper(urlPathHelper);
}

This then prevents mapping a request to api/token/base to the /token/base endpoint.

@wilkinsona wilkinsona added type: regression A regression from a previous release and removed status: feedback-provided Feedback has been provided status: waiting-for-triage An issue we've not yet triaged labels Aug 8, 2020
@wilkinsona wilkinsona added this to the 2.3.3 milestone Aug 8, 2020
@wilkinsona wilkinsona added the for: team-attention An issue we'd like other members of the team to review label Aug 8, 2020
@wilkinsona wilkinsona self-assigned this Aug 10, 2020
@wilkinsona wilkinsona removed the for: team-attention An issue we'd like other members of the team to review label Aug 10, 2020
@wilkinsona wilkinsona changed the title ServletRegistrationBean bean.addUrlMappings in spring boot 2.3.1 It doesn't work later Requests that should be handled by an additional DispatcherServlet result in a 404 response Aug 11, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: regression A regression from a previous release
Projects
None yet
Development

No branches or pull requests

3 participants