Spring Boot 2.x context path settings

How to set the context path for SpringBoot2.X?

In Spring MVC, I set the following in web.xml, but I didn't know how to use Spring Boot, so make a note.

web.xml


    <servlet>
        <servlet-name>appServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath*:META-INF/spring/spring-mvc.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>appServlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

background

If you want to develop API with SpringBoot and set it as http: /// localhost: 8080 / api / v1 / app, it is mapped as http: /// localhost: 8080 / app by default. So I think it's redundant to implement / api / v1 in Request Mapping of Controller.

Main subject

The information is different even if you search unexpectedly. .. .. I thought, so I looked it up.

Looking at the Spring Boot Official Appendix, there are three properties that are likely to be applicable.

server.servlet.context-path= server.servlet-path= spring.mvc.servlet.path=

server.servlet-path Honke is currently deprecated. Use spring.mvc.servlet.path !!

spring.mvc.servlet.path Servlet path, not context path. At first, I set this up. Since Controller that sets spring.mvc.servlet.path and maps with Request Mapping works I had a misunderstanding.

The reason why I investigated because the link expression of Thymeleaf was not rendered well.

server.servlet.context-path This was the context path setting

Conclusion

If it is a context path, the correct answer is to set it as follows

application.properties


server.servlet.context-path=/api/v1

It seems that the context path and the Servlet path were messed up. By the way, in SpringBoot1.X, you can set it with server.context-path = / api / v1.

Supplement

Thymeleaf link expressions add a context path if you start with /.

Original html


<a th:href="@{/app/member}">Member display</a>

render


<a href="/api/v1/app/member">Member display</a>

Reference URL

Spring Boot Change Context Path

Recommended Posts

Spring Boot 2.x context path settings
Spring Boot Hot Swapping settings
Spring Boot performance related settings
Cassandra x Spring Boot struggle record
Use cache with EhCashe 2.x with Spring Boot
Challenge Spring Boot
Spring Boot Form
Spring Boot Memorandum
gae + spring boot
Use Servlet filter in Spring Boot [Spring Boot 1.x, 2.x compatible]
[Java] Hello World with Java 14 x Spring Boot 2.3 x JUnit 5 ~
LINE Bot x Java (Spring Boot) construction procedure
Settings for connecting to MySQL with Spring Boot + Spring JDBC
SPRING BOOT learning record 01
Spring Boot + Heroku Postgres
Spring boot memo writing (1)
A memorandum of addiction to Spring Boot2 x Doma2
First Spring Boot (DI)
SPRING BOOT learning record 02
Spring Boot2 cheat sheet
Spring Boot with Spring Security Filter settings and addictive points
Spring Boot exception handling
Spring Boot Servlet mapping
Spring boot development-development environment-
Spring Boot learning procedure
SSO with GitHub OAuth in Spring Boot 1.5.x environment
Learning Spring Boot [Beginning]
Spring boot memo writing (2)
Spring Boot 2.2 Document Summary
[Spring Boot] DataSourceProperties $ DataSourceBeanCreationException
Spring Boot 2.3 Application Availability
Spring boot tutorials Topics
Download with Spring Boot
Spring Boot 1.x will reach EOL in the next year.
How to change application.properties settings at boot time in Spring boot
Spring Boot application that specifies DB connection settings with parameters
[Spring Boot] Environment construction (macOS)
Set context-param in Spring Boot
Try Spring Boot from 0 to 100.
Generate barcode with Spring Boot
Hello World with Spring Boot
Spring Boot on Microsoft Azure
Implement GraphQL with Spring Boot
About Spring Framework context error
Spring Boot tutorial task schedule
Spring 5 & Spring Boot 2 Hands-on preparation procedure
Get started with Spring boot
Hello World with Spring Boot!
Spring Boot 2 multi-project in Gradle
[Spring Boot] Web application creation
spring boot port duplication problem
Run LIFF with Spring Boot
SNS login with Spring Boot
[Java] Thymeleaf Basic (Spring Boot)
Introduction to Spring Boot ① ~ DI ~
File upload with Spring Boot
Spring Boot starting with copy
Introduction to Spring Boot ② ~ AOP ~
CICS-Run Java application-(4) Spring Boot application
Spring Boot starting with Docker
Spring Boot + Springfox springfox-boot-starter 3.0.0 Use