3

let's say I have a application.yml with this content

server:
  port: 8000
  context-path: /rest

so, all controllers and html will be accessed like this http://server:8000/rest/controller

so, having this configuration...

it is possible to add some static html element to its root level without changing its context-path? (I added a index.html to static folder already).

in other words:

having that application.yml, I need to retrieve a empty html when accessing to our app root-level

http://server:8000

but, currently, I can only access that html on this url

http://server:8000/rest

any ideas? thanks in advance :)

EDIT: i'm doing this because our load balancer, when accessing http://server:8000 will get a 404 from spring-boot, thus marking our server/app as non-accessible.

since from springboot, I have control over tomcat. is there any way I could add a basic html to its root level so our load balancer does not mark our application as unavailable? pd: I have no access to setup that load balancer :(

dsncode
  • 2,407
  • 2
  • 22
  • 36
  • You could use `/` as context path for your application and prefix all controller's `@RequestMapping`s with `/rest`. – ST-DDT May 03 '18 at 10:39
  • I know, but that does not sound like a good approach. in fact, we have a lot of controllers and that approach seems a bit of an overkill :( – dsncode May 03 '18 at 10:41
  • This question was asked in https://stackoverflow.com/questions/28006501/how-to-specify-prefix-for-all-controllers-in-spring-boot before, but has no satisfying answers yet. – ST-DDT May 03 '18 at 10:44

3 Answers3

1

context-path affects the entire application. In Tomcat or other servlet container it's used to distinguish between multiple WARs deployed to the same servlet container e.g. http://localhost:8000/abc and http://localhost:8000/xyz.

Since you are using Spring Boot you are most likely packaging the servlet container with your application. In this case set server.context-path = / and use @RestController("/rest") to expose the REST API as http://localhost:8000/rest.

Karol Dowbecki
  • 43,645
  • 9
  • 78
  • 111
1

Create a controller with @RequestMapping("/") which will return view name but for this requires any view configuration.

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

@Controller
public class IndexController {
    @RequestMapping("/")
    public String index() {
        return "springbootexampleindex";
    }
}
Eby Jacob
  • 1,418
  • 1
  • 10
  • 28
0

You could not add a context path, but add a custom J2EE or spring filter. anything request with "/rest/" in the path, send it to the controllers as a forward after removing the "rest/" portion. Anything without /rest/ dont filter it - let the request continue as is, so spring will serve static content as is. This serve your static from root and your APIs appear to be as under /rest/

tgkprog
  • 4,493
  • 4
  • 41
  • 70