3

I want to add my own application name as the prefix. Example: http://localhost:8084/MyApp/some-url. But when I click on some link (href) it redirects me to http://localhost:8084/some-url. Also I've added server.servlet.context-path=/MyApp in properties file. Note: I'm using spring boot 2.5.4v and embedded tomcat 9.0.53v

Waseem
  • 123
  • 1
  • 1
  • 11
  • I'm currently testing with the same versions and it works fine with `server.servlet.context-path=/MyApp` attached. See https://stackoverflow.com/questions/20405474/add-context-path-to-spring-boot-application – İsmail Y. Sep 23 '21 at 09:44
  • @İsmailY. Not working in my case. – Waseem Sep 23 '21 at 09:49
  • Share your files (application.properties etc.) in your case with us. – İsmail Y. Sep 23 '21 at 10:01
  • application.properties `server.servlet.context-path=/MyApp` Controller: `@RequestMapping("/some-url") public String somePage(Model model) { model.addAttribute("message", "Hello world!"); return "some-page"; }` – Waseem Sep 23 '21 at 10:06

1 Answers1

0

Spring boot: 2.7.9 and its embedded tomcat which is above 9.0.0.

I noticed that server.servlet.contextPath=/my-path does not work and it picks the executable jar file name which gradle builds. I mean the name of the jar file when we build it (it will get deployed in a openjdk:11-jre-slim container to aws fargate).

This one works for us:

@Bean
public WebServerFactoryCustomizer<ConfigurableServletWebServerFactory> webServerFactoryCustomizer() {
    return factory -> factory.setContextPath("my-path");
}

You may define this bean in any configuration classes. I know that this one will work with embedded tomcat too but it is more like a deployment time config:

java -jar -Dserver.servlet.context-path=/my-path your-jar-file-name.jar
Mohamad Eghlima
  • 970
  • 10
  • 23