server.port=8080
server.servlet.context-path=/api
控制器:
@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);
}
}
此uri在Postman中为我提供了一个结果:
localhost:8080/posts/v1.0
而这一条没有:
localhost:8080/api/posts/v1.0
没有任何意义。
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.4.2</version>
</parent>
注:application.properties 如果正确读取,我可以随意更改端口号。