53

I am using Spring Boot to create a web app, and I am not sure how to change the URL from localhost:8080 to something like localhost:8080/myWebApp.

I have a seen a lot of resources online referencing an application.properties file and adding that to the classpath. But, I'm not sure exactly where to put that.

Questions

  • In my src/main/resources?

  • How would I assign the URL within the file?

nbro
  • 15,395
  • 32
  • 113
  • 196
Theo
  • 883
  • 2
  • 10
  • 19

6 Answers6

81

You need to set the property server.contextPath to /myWebApp.

Check out this part of the documentation

The easiest way to set that property would be in the properties file you are using (most likely application.properties) but Spring Boot provides a whole lot of different way to set properties. Check out this part of the documentation

EDIT

As has been mentioned by @AbdullahKhan, as of Spring Boot 2.x the property has been deprecated and should be replaced with server.servlet.contextPath as has been correctly mentioned in this answer.

geoand
  • 60,071
  • 24
  • 172
  • 190
  • Thanks! That worked perfectly, except now my messaging system isn't functioning correctly. Do you know why that might be? – Theo Jun 27 '14 at 13:03
  • 1
    @Glad that it worked! There could be various reasons that your messaging system doesn't work. If you need help finding it, it would probably be best to start a new SO question so every one can see without being distracted by the current one – geoand Jun 27 '14 at 13:04
  • 1
    N.B. you can also set `server.servlet-path` if you want to put other stuff in the root context "/". – Dave Syer Jun 27 '14 at 15:45
  • 1
    Be sure to include leading "/" so it should be "/myWebApp" – GameSalutes Oct 18 '15 at 21:51
  • 2
    How do you enforce this to only work with RestControllers and access the normal Controllers without the context? – Siya Sosibo May 23 '16 at 20:43
  • @Stoan I can't think of any way to do that – geoand May 23 '16 at 20:57
  • @geoand Thanks anyway, I will try this solution in the min time, http://stackoverflow.com/a/32943991/1458556 – Siya Sosibo May 23 '16 at 21:07
  • server.contextPath is getting ignored and it's pointing with war file name – Anshad Vattapoyil Jun 10 '17 at 21:02
  • @devo It would probably be best to start a new question containing the specifics of your problem – geoand Jun 12 '17 at 08:33
  • 4
    As of spring boot 2 the `server.contextPath` property is deprecated. Use `server.servlet.contextPath` instead. Check out [this](https://stackoverflow.com/a/48987109/3094731) answer. – Abdullah Khan Oct 15 '18 at 09:31
  • If you set server.servlet.contextPath on the command line it overrides what is set in the application.properties file – ssimm Nov 07 '18 at 16:27
  • @ssimm yes, that is standard spring boot behavior – geoand Nov 08 '18 at 07:10
21

As of spring boot 2 the server.contextPath property is deprecated. Instead you should use server.servlet.contextPath.

So in your application.properties file add:

server.servlet.contextPath=/myWebApp

For more details see: https://github.com/spring-projects/spring-boot/wiki/Spring-Boot-2.0-Migration-Guide#servlet-specific-server-properties

brafdlog
  • 2,642
  • 1
  • 18
  • 21
17

In your src/main/resources put an application.properties or application.yml and put a server.contextPath in there.

server.contextPath=/your/context/here

When starting your application the application will be available at http://localhost:8080/your/context/here.

For a comprehensive list of properties to set see Appendix A. of the Spring Boot reference guide.

Instead of putting it in the application.properties you can also pass it as a system property when starting your application

java -jar yourapp.jar -Dserver.contextPath=/your/path/here
Alexander Suraphel
  • 10,103
  • 10
  • 55
  • 90
M. Deinum
  • 115,695
  • 22
  • 220
  • 224
  • 3
    `server.context-path` is now deprecated, use `server.contextPath` now – dspies Feb 16 '16 at 21:50
  • 1
    @dspies those are actually the same... You can use camel casing or use a `-` both will work. – M. Deinum Feb 17 '16 at 06:26
  • 2
    How do you enforce this to only work with RestControllers and access the normal Controllers without the context? – Siya Sosibo May 23 '16 at 20:43
  • 2
    server.contextPath in my application.properties is ignored :( Running Spring v1.4.2.RELEASE on Tomcat 8.5.6 – syr Dec 02 '16 at 10:00
  • @ChristianSchäfer: That is the case for me too, in Eclipse. My `@SpringBootApplication` class name is always my context root.Were you able to solve it? I am using 1.5.3. – Sabir Khan Jun 05 '17 at 06:23
  • server.contextPath is getting ignored and it's pointing with war file name. – Anshad Vattapoyil Jun 10 '17 at 21:01
  • @devo if you are deploying a war file to a tomcat instance most of the `server.*` properties are useless as those are for configuring the embedded tomcat. – M. Deinum Jun 11 '17 at 06:45
  • @M.Deinum is there any solution to achieve custom URL here in spring boot? – Anshad Vattapoyil Jun 11 '17 at 08:26
  • Ask a question for that (or search the question). You are deploying an app (regardless of the fact that it is a spring boot app) so the normal rules apply then. – M. Deinum Jun 11 '17 at 08:44
5

In Spring Boot 2 the property in e.g. application.properties is server.servlet.context-path=/myWebApp to set the context path.

https://docs.spring.io/spring-boot/docs/2.0.1.BUILD-SNAPSHOT/reference/htmlsingle/#_custom_context_path

k_o_
  • 5,143
  • 1
  • 34
  • 43
1

The server.contextPath or server.context-path works if

in pom.xml

  1. packing should be war not jar
  2. Add following dependencies

    <dependency>
        <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <!-- Tomcat/TC server -->
     <dependency>
         <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-tomcat</artifactId>
        <scope>provided</scope>
     </dependency>
    

    In eclipse, right click on project --> Run as --> Spring Boot App.

Adriaan
  • 17,741
  • 7
  • 42
  • 75
Manousos
  • 21
  • 1
  • 5
0

The issue of changing the context path of a Spring application is handled very well in the post titled Spring Boot Change Context Path

Basically the post discusses multiple ways of realizing this viz.

  1. Java Config
  2. Command Line Arguments
  3. Java System Properties
  4. OS Environment Variables
  5. application.properties in Current Directory
  6. application.properties in the classpath (src/main/resources or the packaged jar file)
Sandeep
  • 1,245
  • 1
  • 13
  • 33