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)
- Set artifact properties on the docs zip when releasing
- Unlike DataSourceAutoConfiguration, DevToolsDataSourceAutoConfiguration assumes that javax.sql.DataSource will always be available HOT 6
- Add an option to stop the application from spring-boot-properties-migrator
- spring.config.additional-location (on embedded Tomcat) causing broken search path for static files HOT 2
- 升级springboot3.x + maven,在使用多环境配置时会多了native和nativeTest两个环境,请问如何去掉呢 HOT 1
- build native image using Paketo Buildpack for Bellsoft Liberica 10.7.0 (which has JDK 22 support) instead of current 10.5.5 HOT 1
- How to manually create a ConversionService bean with the same converters and formatters in a none web application as the auto created bean in web application. HOT 3
- Unlike DataSourceAutoConfiguration, DevToolsDataSourceAutoConfiguration assumes that javax.sql.DataSource will always be available
- Unlike DataSourceAutoConfiguration, DevToolsDataSourceAutoConfiguration assumes that javax.sql.DataSource will always be available
- Set artifact properties on the docs zip when releasing
- Move steps for preparing a Gradle build into a reusable action
- Move steps for preparing a Gradle build into a reusable action
- Move steps for preparing a Gradle build into a reusable action
- Run system tests on GitHub Actions
- Run system tests on GitHub Actions
- Run system tests on GitHub Actions
- Native image doesn't start and doesn't log anything if an environment post processor throws an exception
- Native image doesn't start and doesn't log anything if an environment post processor throws an exception
- TestRestTemplate equivalent class for RestClient HOT 1
- improve jwt error message HOT 1
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.