Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

server.servlet.contextPath vs spring.mvc.servlet.path

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?

like image 234
user15006167 Avatar asked Aug 31 '26 01:08

user15006167


2 Answers

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.

like image 163
João Dias Avatar answered Sep 03 '26 14:09

João Dias


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/.....

like image 36
Murat Kara Avatar answered Sep 03 '26 14:09

Murat Kara



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!