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>
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
.
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
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
.
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>
Spring Boot Change Context Path
Recommended Posts