Trace
Trace

Reputation: 18859

server.servlet.context-path not working (wtf)

My application.properties:

server.port=8080
server.servlet.context-path=/api

Controller:

@RestController
@RequestMapping("/posts")
public class PostController {

    @GetMapping({ "/v1.0" })
    public ResponseEntity<List<Post>> getPosts(@RequestParam Optional<String> maxId) {
        List<Post> posts = Arrays.asList(
                new Post(new ObjectId().toString(), "Test status 1", LocalDateTime.now()),
                new Post(new ObjectId().toString(), "Test status 2", LocalDateTime.now()),
                new Post(new ObjectId().toString(), "Test status 3", LocalDateTime.now()),
                new Post(new ObjectId().toString(), "Test status 4", LocalDateTime.now()),
                new Post(new ObjectId().toString(), "Test status 5", LocalDateTime.now()),
                new Post(new ObjectId().toString(), "Test status 6", LocalDateTime.now()),
                new Post(new ObjectId().toString(), "Test status 7", LocalDateTime.now()),
                new Post(new ObjectId().toString(), "Test status 8", LocalDateTime.now()),
                new Post(new ObjectId().toString(), "Test status 9", LocalDateTime.now()),
                new Post(new ObjectId().toString(), "Test status 10", LocalDateTime.now())
        );
        return ResponseEntity.ok(posts);
    }

}

This uri gives me a result in Postman:

localhost:8080/posts/v1.0

Whereas this one does not:

localhost:8080/api/posts/v1.0

Doesn't make any sense.

Version:

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.4.2</version>
</parent>

Note: application.properties gets properly read, I can change the port number at will.

Upvotes: 5

Views: 5616

Answers (2)

Trace
Trace

Reputation: 18859

What I'm currently writing is boilerplate (this is why I hadn't implemented Webflux yet, simply to get a basic REST api working), but I have added the Maven Webflux package because I want a reactive api:

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-webflux</artifactId>
    </dependency>

In which case the following property is needed instead:

spring.webflux.base-path=/api

Upvotes: 7

adramazany
adramazany

Reputation: 664

if you want to have dynamic and changable prefix for your uri, you could use custom variables in your properties file and use them as prefix of your mapping by ${}

Upvotes: -2

Related Questions