hell_storm2004
hell_storm2004

Reputation: 1605

Spring Boot Unable to Load HTML (500 Error)

Let me start by saying I am currently getting this error when trying to load my Spring Boot app:

2022-07-04 15:26:03.640 DEBUG 15484 --- [nio-8881-exec-1] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped to com.gtt.gcaas.console.controller.ConsoleController#greeting()
2022-07-04 15:26:03.655 DEBUG 15484 --- [nio-8881-exec-1] o.s.web.servlet.DispatcherServlet        : Failed to complete request: javax.servlet.ServletException: Could not resolve view with name 'welcome' in servlet with name 'dispatcherServlet'
2022-07-04 15:26:03.665 ERROR 15484 --- [nio-8881-exec-1] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Could not resolve view with name 'welcome' in servlet with name 'dispatcherServlet'] with root cause

javax.servlet.ServletException: Could not resolve view with name 'welcome' in servlet with name 'dispatcherServlet'
    at org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1380) ~[spring-webmvc-5.3.20.jar:5.3.20]

My Spring Main class is:

@SpringBootApplication
@ComponentScan(basePackages = {"com.gtt.gcaas.console.controller", "com.gtt.gcaas.device.details",  "com.gtt.gcaas.device.root", "com.gtt.gcaas.device.db", "com.gtt.gcaas.device.db.service"})
@ConfigurationPropertiesScan(basePackages = {"com.gtt.gcaas.device.configuration"})
@EnableJpaRepositories(basePackages = {"com.gtt.gcaas.device.db.repository"})
@EnableWebMvc
@EntityScan(basePackages = {"com.gtt.gcaas.device.db.jpa"})
public class NcaasDeviceConnectionApplication {

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

My Controller Class is:

package com.gtt.gcaas.console.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;

@Controller
public class ConsoleController {

    @GetMapping("/greeting")
    public String greeting() {    
        return "welcome";
    }
}

I have declared this in my application.properties along with a few other hibernate properties:

logging.level.ch.qos.logback==DEBUG
spring.devtools.restart.enabled=true
debug=true
logging.file.path=C:/logs
spring.mvc.view.prefix=/WEB-INF/html/
spring.mvc.view.suffix=.html

But when I hit the URL http://localhost:8080/greeting I get the above error mentioned. First I tried with JAR packaging, but then switched to WAR, but the outcome is the same.

My folder structure is:

enter image description here

Any help on this would be nice. Thanks in advance.

Upvotes: 0

Views: 774

Answers (1)

unconditional
unconditional

Reputation: 7656

Just put your welcome.html to /src/main/resources/WEB-INF/html/ and add the following properties to the application.properties :

spring.mvc.view.suffix=.html
spring.web.resources.static-locations=classpath:/WEB-INF/html

Usually there's not need to set the static resource locations, since there are some predefined/well-known ones:

By default, Spring Boot serves 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.

(https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#web.servlet.spring-mvc.static-content)

I.e. if you put your static resources in any of those directories under src/main/resources Spring will see them.

Upvotes: 1

Related Questions