Issue
Consider the below code,
@RestController
@RequestMapping("/v1")
class Controller {
}
What I am supposed to do is, remove the @RequestMapping and configure the path through application.properties.
I came across two ways with which I can achieve this,
spring.mvc.servlet.path=/v1
and
server.servlet.contextPath=/v1
But how do they differ, since I haven't noticed any difference in both these configurations? Which one would be ideal for what I am trying to achieve?
Solution
The context path is a name with which a web application is accessed. It is the root of the application and by default, Spring Boot serves the content on the root context path (“/”). This context path can be changed with the property server.servlet.context-path
.
On the other hand, the servlet path represents the path of the main DispatcherServlet
. The default value is similar to the context path, i.e. (“/”) and it can be changed by configuring a different spring.mvc.servlet.path
property. Given that a servlet belongs to a servlet context, changing the context path will also affect the servlet path.
Keeping both pieces of information in mind if you have the following configuration:
server.servlet.context-path=/context-path
spring.mvc.servlet.path=/servlet-path
Then the application servlet path will become http://localhost:8080/context-path/servlet-path
.
Having written all this, I would say that in your case it is ok to use any of the properties.
Answered By - João Dias
Answer Checked By - Mary Flores (JavaFixing Volunteer)