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?
CodePudding user response:
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.
CodePudding user response:
By server.servlet.contextPath
means the path by which your application will be accessed by the end users where spring.mvc.servlet.path
means the servlet path. If you don't set any servlet path[default value is /
], contextPath
will be used for user's interaction with your service. But if servlet
path is provided, then you context path will be changed like this:
http://application_domain_name:port/cotextPath_provided/_servlet_path_provided
CodePudding user response:
Spring Boot offers both ("/") by default.
If we enter different values for both, their difference will appear;
For example:
spring.mvc.servlet.path=/test
server.servlet.contextPath=/demo
this endpoints: http://localhost:8080/demo/test/.....