How do I choose the URL for my Spring Boot webapp?

前端 未结 6 927
粉色の甜心
粉色の甜心 2020-11-30 03:48

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.

相关标签:
6条回答
  • 2020-11-30 04:01

    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.

    0 讨论(0)
  • 2020-11-30 04:01

    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)
    0 讨论(0)
  • 2020-11-30 04:09

    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.

    0 讨论(0)
  • 2020-11-30 04:14

    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
    
    0 讨论(0)
  • 2020-11-30 04:20

    In Spring Boot 2 the property in e.g. in 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

    0 讨论(0)
  • 2020-11-30 04:22

    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

    0 讨论(0)
提交回复
热议问题