Coder Social home page Coder Social logo

Comments (10)

dsyer avatar dsyer commented on April 19, 2024

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.

masrawi avatar masrawi commented on April 19, 2024

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.

dsyer avatar dsyer commented on April 19, 2024

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.

dsyer avatar dsyer commented on April 19, 2024

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.

dsyer avatar dsyer commented on April 19, 2024

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.

dsyer avatar dsyer commented on April 19, 2024

This works as designed actually - the SampleApplication has to be @Configuration otherwise the dispatcher servlet is created and registered twice.

from spring-boot.

tkruse avatar tkruse commented on April 19, 2024

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.

dsyer avatar dsyer commented on April 19, 2024

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.

tkruse avatar tkruse commented on April 19, 2024

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.

tkruse avatar tkruse commented on April 19, 2024

I cannot reproduce using 0.5.0.BUILD-SNAPSHOT

from spring-boot.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.