4

I have a spring boot application named "springapp" which has a base URI as:

 http://localhost:8080/

When I run it as a standalone application it is working fine. But when I run it like "run as --> Run on Server" in Eclipse, it would append the war name as springapp-1.0-snapshot and the base URL would become:

 http://localhost:8080/springapp-1.0-snapshot

Is there any way I could fix this. I want the URI to be

 http://localhost:8080/springapp

I tried all below configurations in my application.properties but none of them worked:

 spring.webservices.path=/springapp
 server.tomcat.basedir=/springapp
 spring.jersey.application-path=/springapp
 server.contextPath=/springapp
 server.servlet-path=/springapp

This is my starter class:

    @SpringBootApplication(exclude = { DataSourceAutoConfiguration.class })
    @ImportResource("classpath:spring-config.xml")
    public class SpringAppStarter extends SpringBootServletInitializer {

        private static final Logger GENERAL_LOG = LogManager.getLogger(SpringAppStarter .class);

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

        public static void main(String[] args) {
            // TODO Auto-generated method stub
            GENERAL_LOG.info("Starting Spring  Service App");

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

and my request mapping class:

 @RestController
 @RequestMapping("/Customers")
 public class SpringAppResource {
    @RequestMapping("/")
    public String index() {
        return "Greetings from Spring Boot!";
    }
 }

I also addded context configuration in the META-INF folder as:

 <Context>
     <context docBase="springapp" path="/springapp" reloadable="true"/>
 </Context>

I am using Maven to build the project. And I do not want to change the service name. I need to provide the context path in the application itself somewhere. There cannot be any dependency on server. I am supposed to provide all configurations, so after deployment no configuration needs to be made.

Monzurul Shimul
  • 8,132
  • 2
  • 28
  • 42
Madie
  • 231
  • 3
  • 10
  • 1
    Tomcat by default use your war file name as context path. Quick search given me this : http://stackoverflow.com/q/7276989/395202 which tells you how to use as your app's context path, or https://tomcat.apache.org/tomcat-5.5-doc/config/context.html for how to setup context path in Tomcat – Adrian Shum Sep 09 '16 at 06:53
  • Thanks. Ideally this is what we do for regular web services. I have already tried it but it wont work with spring boot. – Madie Sep 09 '16 at 09:12
  • I don't think it has anything to do with Spring Boot here. Context path should be something decided by container, before the web app (which is using Spring Boot here) can do anything. – Adrian Shum Sep 09 '16 at 09:22
  • ah... I just found that you are running it thru Eclipse. iirc, there are some tricks in Eclipse running Tomcat to provide the Tomcat config. Something like there is a separate directory containing the config so that Eclipse will copy over to the Tomcat directory everytime. So that if you update configs in Tomcat directory directly it will not work as it got replaced silently. – Adrian Shum Sep 09 '16 at 09:24
  • exactly. It works for normal spring web services. I am just unable to do it for spring boot. – Madie Sep 09 '16 at 11:02

3 Answers3

3

You should be able to rename war like this

<build>
  <finalName>springapp</finalName>
 . . .
</build>
Adrian Shum
  • 38,812
  • 10
  • 83
  • 131
kuhajeyan
  • 10,727
  • 10
  • 46
  • 71
  • 1. You don't need to rename war to change context path in tomcat. 2. This is part of a Maven POM, and there is no hint in OP's question that he is using Maven for build – Adrian Shum Sep 09 '16 at 08:30
  • Thank you:). I am using maven but I should not change the finalName . – Madie Sep 09 '16 at 08:47
3

You can also change the app name on Tomcat server as:

  1. Double click your server instance in "Servers" view
  2. Switch tab from "Overview" to "Modules" (at the bottom of the configuration screen)
  3. Select your application in the list and click "Edit" on the right hand side
  4. Set "Path" as "springapp" (or any name you want for the URI)
  5. Click "OK" and save the changes.

Now your app will be at the URI: http://localhost:8080/springapp

Bahadir Tasdemir
  • 10,325
  • 4
  • 49
  • 61
  • Thanks for the solution. But I am supposed to provide all configurations in application itself. – Madie Sep 09 '16 at 08:46
  • You are welcome. I am using my project at root level "/" (no project name) and with this setting, I am very eligible to manage it. – Bahadir Tasdemir Sep 09 '16 at 09:15
1

If you are using eclipse then you can change root context by going to yourproject->properties->Web Project settings. then change context root here and restart your server

Jekin Kalariya
  • 3,475
  • 2
  • 20
  • 32