3

I am wiring a AngularJS and spring-boot application together by hand for the first time. The issues I am running into is my @RestController is not returning the index page:

@RestController
public class IndexController {

    @RequestMapping("/")
    public String index(){
        System.out.println("Looking in the index controller.........");
        return "index";
    }

}

Directory:

enter image description here

It keeps rendering the default 404 error page:

enter image description here

----------------UPDATE 1------------------

I have added a configuration file:

@Configuration
public class IndexPageConfiguration {

    @Bean
    public InternalResourceViewResolver viewResolver(){

        InternalResourceViewResolver resolver = new InternalResourceViewResolver();
        resolver.setPrefix("/app/");
        resolver.setSuffix(".html");
        return resolver;

    }

}

RestController

@RestController
public class IndexController {

    @RequestMapping("/")
    public String index(){
        System.out.println("Looking in the index controller.........");
        return "index";
    }

}

main class:

@SpringBootApplication(scanBasePackages = { "com.serviceImpl","com.service","com.config" },exclude = { ErrorMvcAutoConfiguration.class })
public class SpringCrudApplication {

    public static void main(String[] args) {

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

The above main class is still returning the default 404 error page.

Pragnani
  • 20,075
  • 6
  • 49
  • 74
Mike3355
  • 11,305
  • 24
  • 96
  • 184

2 Answers2

2

On the other hand, Spring will automatically look for the index.html page if you put it directly under webapp folder. So you don't need any configuration.

This is just another way to do it.

OPK
  • 4,120
  • 6
  • 36
  • 66
1

You need to configure InternalRosourceViewResolver to let the spring know your jsp location

@Bean
    public InternalResourceViewResolver viewResolver() {
        InternalResourceViewResolver resolver = new InternalResourceViewResolver();
        resolver.setPrefix("/app/");
        resolver.setSuffix(".html");
        return resolver;

    }

So Spring will append and append location and suffix to your View returned.

I think it is good idea to keep your views separately in any other folder and configure your folder location according to it.

If you want to continue with your current set up

you should return "/app/index.html" from your controller.

Spring boot provides White label error page to hide your stack trace when a Server side error/ exception occurs, this will help us from protecting our code from intruders. If you want to get rid of white label error.

In your @SpringBootApplication specify excludes ErrorMvcAutoConfiguration.class

@SpringBootApplication(scanBasePackages = { "com.ekart.app" }, exclude = { ErrorMvcAutoConfiguration.class })

If you are not using @SpringBootApplication annotatio, you should supply same same excludes in @EnableAutoConfiguration annotation

Pragnani
  • 20,075
  • 6
  • 49
  • 74
  • @Drew1208 try removing while label error. and also remove `spring security` from your pom.xml because spring boot will autoconfigure spring security – Pragnani Mar 11 '16 at 20:11
  • @Drew1208 don't do both, either return "index" if you place IRVR, otherwise return "/app/index.html" – Pragnani Mar 11 '16 at 20:13
  • @Pranani Kinnera I am not using spring security at the moment. – Mike3355 Mar 11 '16 at 20:13
  • GOT IT.... Thank you. I needed to do what you suggested and add scan my rest controller. However it is just rendering "index" not the index.html page what is tied into my angularJS – Mike3355 Mar 11 '16 at 20:15
  • @Drew1208 I have edited your question, with what is need to do. please follow that edit – Pragnani Mar 11 '16 at 20:21
  • it is returning the string "index" from the controller, not rendering the index.html page. – Mike3355 Mar 11 '16 at 20:27
  • @Drew1208 Also include this, as I have faced the same issue when I was learning spring boot http://stackoverflow.com/questions/20602010/jsp-file-not-rendering-in-spring-boot-web-application – Pragnani Mar 11 '16 at 20:31
  • Thank you very much for your help. I have added the dependencies but still coming into the same issue. – Mike3355 Mar 11 '16 at 20:38
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/106051/discussion-between-pragnani-kinnera-and-drew1208). – Pragnani Mar 11 '16 at 20:42