Comments (10)
If you are using the autoconfigure features (I assume you are) then you would need to add a bean with id "dispatcherServlet" and have that define the servlet mapping - typically that would mean it was a ServletRegistrationBean
with the path mapping you need in it.
I have to ask though: what is the real use case here? Why do you want to change the default mapping?
from spring-boot.
Hi,
the use case here is that I am using angularjs and I want it to handle localhost:8080/#/users_list and call GET localhost:8080/api/users (Servlet)
I get an exception if I do the following:
@ComponentScan
@EnableAutoConfiguration
public class SampleApplication
{
@Bean
public ServletRegistrationBean dispatcherRegistration()
{
ServletRegistrationBean registration = new ServletRegistrationBean(dispatcherServlet());
registration.addUrlMappings("/api/*");
return registration;
}
@Bean(name = DispatcherServletAutoConfiguration.DEFAULT_DISPATCHER_SERVLET_BEAN_NAME)
public DispatcherServlet dispatcherServlet()
{
return new DispatcherServlet();
}
public static void main(String[] args)
{
SpringApplication app = new SpringApplication(new Object[] { SampleApplication.class });
app.run(args);
}
@Bean
public ServerProperties creatServerPropertiesBean()
{
ServerProperties n = new ServerProperties();
// n.setPort(9000);
return n;
}
}
``
Caused by: java.lang.IllegalStateException: Registration is null. Was something already registered for name=[dispatcherServlet]?
at org.springframework.util.Assert.state(Assert.java:385)
at org.springframework.boot.context.embedded.RegistrationBean.configure(RegistrationBean.java:107)
at org.springframework.boot.context.embedded.ServletRegistrationBean.configure(ServletRegistrationBean.java:164)
at org.springframework.boot.context.embedded.ServletRegistrationBean.onStartup(ServletRegistrationBean.java:156)
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext$1.onStartup(EmbeddedWebApplicationContext.java:207)
at org.springframework.boot.context.embedded.tomcat.ServletContextInitializerLifecycleListener.lifecycleEvent(ServletContextInitializerLifecycleListener.java:54)
at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:119)
at org.apache.catalina.util.LifecycleBase.fireLifecycleEvent(LifecycleBase.java:90)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5322)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
from spring-boot.
I'm not really sure I follow yet. Why can't you just use localhost:8080/users? Is the /api/* prefix hardcoded into the client library?
from spring-boot.
I looked at this a bit and tried it myself. There is some kind of weird ordering/lifecycle problem if you declare the DispatcherServlet
yourself. I will continue to pursue that, but in the meantime you can just inject it, like this:
@Bean
public ServletRegistrationBean dispatcherRegistration(DispatcherServlet dispatcherServlet) {
ServletRegistrationBean registration = new ServletRegistrationBean(
dispatcherServlet);
registration.addUrlMappings("/app/*");
return registration;
}
(I.e. don't add a @Bean
for the DispatcherServlet
as well).
from spring-boot.
I changed the summary of this issue to "Adding a DispatcherServlet bean does not always override the default" because that's really the problem. It's not super urgent though since there is no need to create a custom DispatcherServlet bean in most cases.
from spring-boot.
This works as designed actually - the SampleApplication
has to be @Configuration
otherwise the dispatcher servlet is created and registered twice.
from spring-boot.
We have the same issue, we start our SpringApplication like this
SpringApplicationBuilder applicationBuilder = new SpringApplicationBuilder();
ConfigurableApplicationContext appContext = applicationBuilder.sources(ApplicationConfig.class).run(args);
And declared a ServletRegistrationBean named dispatcherServlet in a
@Bean public ServletRegistrationBean dispatcherServlet() {...}
method in the ApplicationConfig class, which is annotated with @configuration, but the method is never even invoked. This using Spring 4.0.0.RELEASE and Spring Boot 0.5.0.M6. We use your workaround for now.
from spring-boot.
Did you try a snapshot? If you did and it still doesn't work can you push a sample project somewhere that we can look at?
from spring-boot.
I am not sure what you mean. Do you mean snapshot versions of Spring boot? No, we just use 0.5.0.M6. I'd like to provide sample projects, but I don't habe much time, so it may take several days until I find free time...
from spring-boot.
I cannot reproduce using 0.5.0.BUILD-SNAPSHOT
from spring-boot.
Related Issues (20)
- Using Gradle's new file permission API is implemented in a way that prevents removal of the old API
- Support loading config yaml files embedded in env vars via spring.config.import HOT 1
- mapstruct-processor jar is missing when package from springboot2.4 HOT 3
- Reduce warnings reported by Eclipse
- Migrate class references to full javadoc links
- Use javadoc macro for links in adoc files
- Reduce warnings reported by Eclipse
- Rename one instance of DataLdapTestIntegrationTests in spring-boot-test-autoconfigure
- Rename one instance of DataLdapTestIntegrationTests in spring-boot-test-autoconfigure
- Spring Jersey does not return 404 from Controller if entity is null HOT 3
- Some @ControllerEndpoint and @RestControllerEndpoint infrastructure remains undeprecated
- @ConditionalOnProperty for branch of values not match HOT 3
- Update link to documentation for log4j-spring-boot
- Update link to documentation for log4j-spring-boot
- Move "Customize the whitelabel Error Page" how-to
- "Use Spring Data repositories" How-to incorrectly refers to Repository annotations
- Upgrade to Testcontainers 1.20.0 HOT 1
- "Use Spring Data repositories" How-to incorrectly refers to Repository annotations
- "Use Spring Data repositories" How-to incorrectly refers to Repository annotations
- Improve Configuration Experience for Multiple CacheManagers HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from spring-boot.