简体   繁体   English

server.servlet.context-path 不工作(wtf)

[英]server.servlet.context-path not working (wtf)

My application.properties:我的应用程序属性:

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

Controller: 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:这个 uri 给了我 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.注意:application.properties 被正确读取,我可以随意更改端口号。

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: 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

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 ${}如果您想为您的 uri 使用动态且可更改的前缀,您可以在属性文件中使用自定义变量,并将它们用作映射的前缀 ${}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM