Nanor Nanor - 2 months ago 19
Java Question

Spring on Wildfly not rendering index.html when root URL hit

Due to project requirements I have to deploy a Spring application to a server incapable of running Tomcat and only capable of running WildFly. When I had a very simple project running on Tomcat and the root URL was hit

(localhost:8080)
it rendered my
index.html
. Since migrating to WildFly and refactoring the structure of my project,
localhost:8080
no longer renders the
index.html
but I can still reach other URLs.

I've tried to implement a
jboss-web.xml
file under
BrassDucks/src/main/webapp/WEB-INF/jboss-web.xml
like this:

<jboss-web>
<context-root>Brass-Ducks</context-root>
</jboss-web>


Where
Brass-Ducks
is the
artifactID
but to no avail.

Consider my
ApplicationConfig.java


package brass.ducks;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.support.SpringBootServletInitializer;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


@Configuration
@ComponentScan
@EnableAutoConfiguration
public class ApplicationConfig extends SpringBootServletInitializer{

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

@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(applicationClass);
}

private static Class<ApplicationConfig> applicationClass = ApplicationConfig.class;



}

@RestController
class GreetingController {

@RequestMapping("/hello/{name}")
String hello(@PathVariable String name) {
return "Hello, " + name + "!";
}
}


and consider my
Controller.java


package brass.ducks.application;

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

@RestController
@RequestMapping("/")
public class Controller {

@RequestMapping("/greet")
@ResponseBody
public String greeting() {
return "Hello, there.";
}
}


And finally should it be relevant, my folder structure:

hierarchy

localhost:8080/greet
returns "Hello, there" and
localhost:8080/hello/name
returns "Hello name". How can I fix this?

Answer

Depending on your exact configuration something along the lines of this should work:

@Controller
public class LandingPageController {

@RequestMapping({"/","/home"})
public String showHomePage(Map<String, Object> model) {
    return "/WEB-INF/index.html";   
   }
}

This is going to explicitly map / to index.html.

Comments