I developed a Spring application that serves Spring MVC REST services. Now I want to implement Angularjs in the webapp directory of my Spring application. The problem is that I do not know how to properly configure this.
What I want to achieve:
- / api / ... = URL that serves REST services through Spring MVC, e.g. localhost: 8080 / api / user / 1
- / ... = URL containing static html pages like localhost: 8080 / index.html
I would also like index.html to load by default.
I am currently using the following configuration, which works, but by default it does not load index.html. But I do not know if this is really the case, how to set up static pages in a Spring container:
web.xml
<listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <servlet> <servlet-name>spring</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>dispatchOptionsRequest</param-name> <param-value>true</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>spring</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping>
applicationContext.xml
<context:property-placeholder properties-ref="deployProperties" /> <context:annotation-config /> <context:component-scan base-package="com.eerra" /> <mvc:annotation-driven/> <mvc:resources mapping="/" location="/resources/ang2/app/"/>
In the current configuration, everything works, but when I try to point my browser to localhost: 8080 /, instead of downloading the index.html file, 404 appears, not found. When I try with localhost: 8080 / index.html, then everything works.
Can someone point me in the right direction how to set up such a setting?
Below you can see my debug.log. The problem here is as follows:
Did not find a handler method for [/]
Therefore, I assume that DispatcherServlet is trying to map the path to the controller. But I do not know how I can avoid this.
21:55:04.896 [qtp581501261-25] DEBUG org.eclipse.jetty.http.HttpParser - filled 314/314 21:55:04.897 [qtp581501261-25 - /] DEBUG org.eclipse.jetty.server.Server - REQUEST / on AsyncHttpConnection@f908897 ,g=HttpGenerator{s=0,h=-1,b=-1,c=-1},p=HttpParser{s=-5,l=9,c=0},r=1 21:55:04.897 [qtp581501261-25 - /] DEBUG oejserver.handler.ContextHandler - scope null||/ @ omjpJettyWebAppContext{/,file:/C:/Users/charms/Documents/Intellij%20Projects/cardkeeper/src/main/webapp/},file:/C:/Users/charms/Documents/Intellij%20Projects/cardkeeper/src/main/webapp/ 21:55:04.897 [qtp581501261-25 - /] DEBUG oejserver.handler.ContextHandler - context=||/ @ omjpJettyWebAppContext{/,file:/C:/Users/charms/Documents/Intellij%20Projects/cardkeeper/src/main/webapp/},file:/C:/Users/charms/Documents/Intellij%20Projects/cardkeeper/src/main/webapp/ 21:55:04.897 [qtp581501261-25 - /] DEBUG org.eclipse.jetty.server.session - ses sionManager=org.eclipse.jetty.server.session.HashSessionManager@ 70799896 21:55:04.897 [qtp581501261-25 - /] DEBUG org.eclipse.jetty.server.session - session=null 21:55:04.897 [qtp581501261-25 - /] DEBUG oejetty.servlet.ServletHandler - servlet |/|null -> spring 21:55:04.897 [qtp581501261-25 - /] DEBUG oejetty.servlet.ServletHandler - chain=springSecurityFilterChain->spring 21:55:04.897 [qtp581501261-25 - /] DEBUG oejetty.servlet.ServletHandler - call filter springSecurityFilterChain 21:55:04.897 [qtp581501261-25 - /] DEBUG ossweb.util.AntPathRequestMatcher - Checking match of request : '/'; against '/favicon.ico*' 21:55:04.897 [qtp581501261-25 - /] DEBUG ossweb.util.AntPathRequestMatcher - Checking match of request : '/'; against '/resources/**' 21:55:04.897 [qtp581501261-25 - /] DEBUG ossecurity.web.FilterChainProxy - / at position 1 of 9 in additional filter chain; firing Filter: 'SecurityContextPersistenceFilter' 21:55:04.898 [qtp581501261-25 - /] DEBUG ossecurity.web.FilterChainProxy - / at position 2 of 9 in additional filter chain; firing Filter: 'LogoutFilter' 21:55:04.898 [qtp581501261-25 - /] DEBUG ossecurity.web.FilterChainProxy - / at position 3 of 9 in additional filter chain; firing Filter: 'UsernamePasswordAuthenticationFilter' 21:55:04.898 [qtp581501261-25 - /] DEBUG ossecurity.web.FilterChainProxy - / at position 4 of 9 in additional filter chain; firing Filter: 'DefaultLoginPageGeneratingFilter' 21:55:04.898 [qtp581501261-25 - /] DEBUG ossecurity.web.FilterChainProxy - / at position 5 of 9 in additional filter chain; firing Filter: 'BasicAuthenticationFilter' 21:55:04.898 [qtp581501261-25 - /] DEBUG ossecurity.web.FilterChainProxy - / at position 6 of 9 in additional filter chain; firing Filter: 'SecurityContextHolderAwareRequestFilter' 21:55:04.898 [qtp581501261-25 - /] DEBUG ossecurity.web.FilterChainProxy - / at position 7 of 9 in additional filter chain; firing Filter: 'AnonymousAuthenticationFilter' 21:55:04.898 [qtp581501261-25 - /] DEBUG osswaAnonymousAuthenticationFilter - Populated SecurityContextHolder with anonymous token: 'org.sprin gframework.security.authentication.AnonymousAuthenticationToken@ 9055c2bc: Principal: anonymousUser; Credentials: [PROTECTED]; Authenticated: true; Details: org.sprin gframework.security.web.authentication.WebAuthenticationDetails@ b364: RemoteIpAddress: 0:0:0:0:0:0:0:1; SessionId: null; Granted Authorities: ROLE_ANONYMOUS' 21:55:04.898 [qtp581501261-25 - /] DEBUG ossecurity.web.FilterChainProxy - / at position 8 of 9 in additional filter chain; firing Filter: 'ExceptionTranslationFilter' 21:55:04.898 [qtp581501261-25 - /] DEBUG ossecurity.web.FilterChainProxy - / at position 9 of 9 in additional filter chain; firing Filter: 'FilterSecurityInterceptor' 21:55:04.898 [qtp581501261-25 - /] DEBUG ossweb.util.AntPathRequestMatcher - Checking match of request : '/'; against '/api/**' 21:55:04.899 [qtp581501261-25 - /] DEBUG osswaiFilterSecurityInterceptor - Public object - authentication not attempted 21:55:04.899 [qtp581501261-25 - /] TRACE oswcsXmlWebApplicationContext - Publishing event in Root WebApplicationContext: org.springframework.security.access.event.PublicInvocationEvent[source=FilterInvocation: URL: /] 21:55:04.899 [qtp581501261-25 - /] DEBUG ossecurity.web.FilterChainProxy - / reached end of additional filter chain; proceeding with original chain 21:55:04.899 [qtp581501261-25 - /] DEBUG oejetty.servlet.ServletHandler - call servlet spring 21:55:04.899 [qtp581501261-25 - /] TRACE osweb.servlet.DispatcherServlet - Bound request context to thread: SecurityContextHolderAwareRequestWrapper[ FirewalledRequest[ (GET /)@604241542 org.eclipse.jetty.server.Request@2403fe86 ]] 21:55:04.899 [qtp581501261-25 - /] DEBUG osweb.servlet.DispatcherServlet - DispatcherServlet with name 'spring' processing GET request for [/] 21:55:04.899 [qtp581501261-25 - /] TRACE osweb.servlet.DispatcherServlet - Testing handler map [org.springframework .web.servlet.mvc.method.annotation.RequestMappingHandlerMapping@ 12440d38] in DispatcherServlet with name 'spring' 21:55:04.899 [qtp581501261-25 - /] DEBUG oswsmmaRequestMappingHandlerMapping - Looking up handler method for path / 21:55:04.912 [qtp581501261-25 - /] DEBUG oswsmmaRequestMappingHandlerMapping - Did not find handler method for [/] 21:55:04.912 [qtp581501261-25 - /] TRACE osweb.servlet.DispatcherServlet - Testing handler map [or g.springframework.web.servlet.handler.BeanNameUrlHandlerMapping@ c518734] in DispatcherServlet with name 'spring' 21:55:04.912 [qtp581501261-25 - /] TRACE oswshBeanNameUrlHandlerMapping - No handler mapping found for [/] 21:55:04.912 [qtp581501261-25 - /] TRACE osweb.servlet.DispatcherServlet - Testing handler map [ org.springframework.web.servlet.handler.SimpleUrlHandlerMapping@ 3c836d3d] in DispatcherServlet with name 'spring' 21:55:04.912 [qtp581501261-25 - /] DEBUG oswshSimpleUrlHandlerMapping - Matching patterns for request [/] are [/**] 21:55:04.912 [qtp581501261-25 - /] DEBUG oswshSimpleUrlHandlerMapping - URI Template variables for request [/] are {} 21:55:04.912 [qtp581501261-25 - /] DEBUG oswshSimpleUrlHandlerMapping - Mapping [/] to HandlerExecutionChain with handler [org. springframework.web.servlet.resource.ResourceHttpRequestHandler@ 6abe6713] and 1 interceptor 21:55:04.912 [qtp581501261-25 - /] TRACE osweb.servlet.DispatcherServlet - Testing handler adapter [org.springframework .web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter@ 7a614724] 21:55:04.912 [qtp581501261-25 - /] TRACE osweb.servlet.DispatcherServlet - Testing handler adapter [ org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter@88 5cb41] 21:55:04.912 [qtp581501261-25 - /] DEBUG osweb.servlet.DispatcherServlet - Last-Modified value for [/] is: -1 21:55:04.912 [qtp581501261-25 - /] DEBUG oswsrResourceHttpRequestHandler - Ignoring invalid resource path [] 21:55:04.913 [qtp581501261-25 - /] DEBUG oswsrResourceHttpRequestHandler - No matching resource found - returning 404 21:55:04.913 [qtp581501261-25 - /] DEBUG osweb.servlet.DispatcherServlet - Null ModelAndView returned to DispatcherServlet with name 'spring': assuming HandlerAdapter completed request handling 21:55:04.913 [qtp581501261-25 - /] TRACE osweb.servlet.DispatcherServlet - Cleared thread-bound request context: SecurityContextHolderAwareRequestWrapper[ FirewalledRequest[ [GET /]@604241542 org.eclipse.jetty.server.Request@2403fe86 ]] 21:55:04.914 [qtp581501261-25 - /] DEBUG osweb.servlet.DispatcherServlet - Successfully completed request 21:55:04.914 [qtp581501261-25 - /] TRACE oswcsXmlWebApplicationContext - Publishing event in WebApplicationContext for namespace 'spring-servlet': ServletRequestHandledEvent: url=[/]; client=[0:0:0:0:0:0:0:1]; method=[GET]; servlet=[spring]; session=[null]; user=[null]; time=[15ms]; status=[OK] 21:55:04.914 [qtp581501261-25 - /] TRACE oswcsXmlWebApplicationContext - Publishing event in Root WebApplicationContext: ServletRequestHandledEvent: url=[/]; client=[0:0:0:0:0:0:0:1]; method=[GET]; servlet=[spring]; session=[null]; user=[null]; time=[15ms]; status=[OK] 21:55:04.914 [qtp581501261-25 - /] DEBUG osswaExceptionTranslationFilter - Chain processed normally 21:55:04.914 [qtp581501261-25 - /] DEBUG osswcSecurityContextPersistenceFilter - SecurityContextHolder now cleared, as request processing completed 21:55:04.914 [qtp581501261-25 - /] DEBUG org.eclipse.jetty.server.Server - RESPONSE / 404 handled=true 21:55:04.915 [qtp581501261-25] DEBUG oejetty.server.AsyncHttpConnection - Enabled read interest SCEP@7cee85c5 {l(/0:0:0:0:0:0:0:1:51001)<->r(/0:0:0:0:0:0:0:1:8080),d=true,open=true,ishut=false,oshut=false,rb=false,wb=false,w=true,i=0r}-{ AsyncHttpConnection@f908897 ,g=HttpGenerator{s=4,h=0,b=0,c=-1},p=HttpParser{s=0,l=9,c=0},r=1} 21:55:04.915 [qtp581501261-25] DEBUG org.eclipse.jetty.http.HttpParser - filled 0/0