Reputation: 4481
I used spring boot in my project. when I start the application using ide everything is okay, but when I run the jar file and open the first page in browser, I see this text :
Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.
There was an unexpected error (type=Not Found, status=404).
/login.html
Spring boot configuration class is :
@EnableAutoConfiguration
@EnableTransactionManagement
@EnableJpaAuditing
@EnableCaching
@EnableWebMvc
@EnableWebMvcSecurity
@Import({SecurityLauncher.class})
@EnableGlobalMethodSecurity(securedEnabled = true, prePostEnabled = true)
public class Launcher extends WebMvcConfigurerAdapter {
public static void main(String[] args) {
SpringApplication.run(Launcher.class);
}
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
super.addResourceHandlers(registry);
}
@Bean
public DispatcherServlet dispatcherServlet() {
return new DispatcherServlet();
}
@Override
public void configureDefaultServletHandling(
DefaultServletHandlerConfigurer configurer) {
configurer.enable();
}
}
How to solve this problem?
Edit -> Project tree
├── src
│ ├── main
│ │ ├── java
│ │ ├── resources
│ │ └── webapp
│ │ ├── app
│ │ │ ├── css
│ │ │ ├── js
│ │ │ │ ├── controllers
│ │ │ │ ├── directives
│ │ │ │ ├── factory
│ │ │ │ ├── filters
│ │ │ │ └── services
│ │ │ ├── lib
│ │ │ ├── resources
│ │ │ ├── templates
│ │ │ └── view
│ │ └── WEB-INF
| | |__ index and login files
│ └── test
│ └── java
Edit 2 -> Spring security config:
public class SecurityLauncher extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests().antMatchers("/login/*", "/login.html*").permitAll().antMatchers("/*").authenticated()
.and().formLogin().loginProcessingUrl("/login").loginPage("/login.html")
.passwordParameter("password").usernameParameter("username").defaultSuccessUrl("/").and()
.logout().logoutUrl("/logout").deleteCookies("JSESSIONID").permitAll().and().csrf().disable();
}
@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth, UserSecurityProvider securityProvider) throws Exception {
auth.userDetailsService(securityProvider);
}
}
Upvotes: 3
Views: 6100
Reputation: 1
By default Spring Boot will serve static content from a directory called /static (or /public or /resources or /META-INF/resources) in the classpath or from the root of the ServletContext. It uses the ResourceHttpRequestHandler from Spring MVC so you can modify that behavior by adding your own WebMvcConfigurerAdapter and overriding the addResourceHandlers method.
In a stand-alone web application the default servlet from the container is also enabled, and acts as a fallback, serving content from the root of the ServletContext if Spring decides not to handle it. Most of the time this will not happen (unless you modify the default MVC configuration) because Spring will always be able to handle requests through the DispatcherServlet.
By default, resources are mapped on /** but you can tune that via spring.mvc.static-path-pattern. For instance, relocating all resources to /resources/** can be achieved as follows:
spring.mvc.static-path-pattern=/resources/**
Hope helpful!!!
Upvotes: 0
Reputation: 756
The issue lies in contextPath. there are two ways to solve it:
Upvotes: 0
Reputation: 3738
The @SpringBootApplication is scanning for controller in the wrong package and does not find the controller so you have to remove @SpringBootApplication and let @ComponentScan(basePackages = "...") look for controller to get scaned right. Do you see a mapping message in log when you start for login.html ?
Upvotes: 2
Reputation: 8324
If it is just a html file you need to add to the path src/main/resources/static
src/main/resources/static/index.html
And the call it
http://{IP}:{port}/index.html
Upvotes: 0
Reputation:
Typically, in a Spring Boot application you place your static files under src/main/resources/static
, src/main/resources/public
, src/main/resources/resources
or src/main/resources/META-INF/resources
. Your templates belong to src/main/resources/templates
.
The official documentation says:
Do not use the src/main/webapp directory if your application will be packaged as a jar. Although this directory is a common standard, it will only work with war packaging and it will be silently ignored by most build tools if you generate a jar.
Upvotes: 1