In a Spring Boot application, the concepts of Context Path and Servlet Path are essential for understanding how requests are handled and mapped to the appropriate controllers. These paths play a crucial role in routing incoming requests to the correct endpoints. In this tutorial, we’ll explore the differences between Context Path and Servlet Path in Spring Boot with examples to clarify their usage.
Context Path
The Context Path represents the root of your application’s URL space. It is a prefix added to all URLs handled by your application. In a Spring Boot application, the Context Path is often set to /
by default, meaning your application’s endpoints will be at the root level of the domain.
Example:
Let’s consider a simple Spring Boot application with a single REST controller:
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@GetMapping("/hello")
public String hello() {
return "Hello from Spring Boot!";
}
}
By default, with no custom context path set, this controller’s hello
endpoint would be accessible at http://localhost:8080/hello
.
Setting a Custom Context Path
You can set a custom context path for your Spring Boot application in the application.properties
or application.yml
file:
application.properties
server.servlet.context-path=/myapp
application.yml
server:
servlet:
context-path: /myapp
With this configuration, your application’s endpoints will now be prefixed with /myapp
. The hello
endpoint would be accessible at http://localhost:8080/myapp/hello
.
Servlet Path
The Servlet Path represents the part of the URL that corresponds to the path pattern used to map the servlet. In a Spring Boot application, the Servlet Path is determined by the @RequestMapping
or @GetMapping
, @PostMapping
, etc., annotations on your controller methods.
Example:
Let’s modify our HelloController
to have multiple endpoints:
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@GetMapping("/greet")
public String greet() {
return "Greetings!";
}
@GetMapping("/hello/{name}")
public String hello(@PathVariable String name) {
return "Hello, " + name + "!";
}
}
In this example:
- The
greet
endpoint is mapped to/greet
, so its Servlet Path is/greet
. - The
hello
endpoint with a path variable is mapped to/hello/{name}
, so its Servlet Path is/hello
.
Request URLs:
http://localhost:8080/greet
- Servlet Path:
/greet
http://localhost:8080/hello/world
- Servlet Path:
/hello
Combining Context Path and Servlet Path
When you set a custom Context Path and define Servlet Paths in your controllers, the paths combine accordingly.
Example:
Let’s assume we have set our custom context path to /myapp
:
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@GetMapping("/greet")
public String greet() {
return "Greetings!";
}
@GetMapping("/hello/{name}")
public String hello(@PathVariable String name) {
return "Hello, " + name + "!";
}
}
With this setup:
- The
greet
endpoint’s full URL would behttp://localhost:8080/myapp/greet
- Context Path:
/myapp
- Servlet Path:
/greet
- The
hello
endpoint’s full URL with a path variable would behttp://localhost:8080/myapp/hello/world
- Context Path:
/myapp
- Servlet Path:
/hello
Conclusion
Understanding the difference between Context Path and Servlet Path in a Spring Boot application is crucial for proper URL mapping and endpoint configuration. Remember:
- Context Path is the root URL prefix for your entire application.
- Servlet Path is the portion of the URL that matches the pattern defined in your controller mappings.
By setting a custom Context Path, you can organize your application’s endpoints under a specific URL prefix. Servlet Path, on the other hand, is determined by your controller mappings, providing further granularity in URL handling.
Consider your application’s structure and requirements when deciding on Context Path and Servlet Path configurations to ensure clean and meaningful URL patterns for your Spring Boot application.