Ne AS
Ne AS

Reputation: 1550

Spring boot not displaying first view

I'm building a spring boot application. My problem is that when I run the project, my login page is not shown. This is my code:

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class RestLogin {

    @RequestMapping("/")
    public String login() {
        return "login";
    }
}

I get only a white page and "login" is written in it. When I try to change the @RestController with @Controller I get this GET http://localhost:8080/ 404 (). My login.html page is located under the webapp>tpl>login.html

How can I display my login page?

Edit

This is my application class

@SpringBootApplication
public class ExampleApplication extends SpringBootServletInitializer {

    private static Logger logger = LoggerFactory.getLogger(ExampleApplication.class);

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
        return builder.sources(ExampleApplication.class);
    }

    public static void main(String[] args) {

        SpringApplication.run(ExampleApplication.class, args);
    }
}

Upvotes: 0

Views: 1939

Answers (4)

Moshe Arad
Moshe Arad

Reputation: 3733

According to your posted code and your description, you're getting an expected behavior.

When you annotate your controller with @RestController, that means that your methods on that controller will try to return their result as JSON.

According to your code:

@RestController
public class RestLogin {

    @RequestMapping("/")
    public String login() {
        return "login";
    }
}

You're returning the String "login", that's why you're getting empty white page with the word login as JSON

If you will change the @RestController to @Controller then it no longer will return your string as JSON,

but Spring will try to figure out from the that "login" string a view, and for that you'll need to add a view resolver bean to your project.

Upvotes: 0

ddarellis
ddarellis

Reputation: 3960

I dont know your configuration but:

@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.httpBasic().and().authorizeRequests()
                                         .antMatchers("/**").permitAll();
        http.authorizeRequests().antMatchers("/**").permitAll();
    }
}

In the Application.properties file add:

spring.mvc.view.suffix: .html

Change @RestController to @Controller for RestLogin class. Also put your html file inside the static folder inside resources folder.

Upvotes: 2

Montassar El Béhi
Montassar El Béhi

Reputation: 637

This is the normal behavior.

New version of Spring web comes with @RestController annotation which nothing but @Controller + @ResponseBody. So when you have a return statement in a method you must use @RestController or annotate your method with @ResponseBody.

Here the problem is that Spring don't know a lot about the http method type, can you please try to use @GetMapping("/") to combinbe path and method at the same time.

Upvotes: 0

jmhage
jmhage

Reputation: 63

You need an application class with a main method. See this tutorial.

Here's a snippet:

package hello;

import java.util.Arrays;

import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;

@SpringBootApplication
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

    @Bean
    public CommandLineRunner commandLineRunner(ApplicationContext ctx) {
        return args -> {

            System.out.println("Let's inspect the beans provided by Spring Boot:");

            String[] beanNames = ctx.getBeanDefinitionNames();
            Arrays.sort(beanNames);
            for (String beanName : beanNames) {
                System.out.println(beanName);
            }

        };
    }

}

Upvotes: 0

Related Questions