Skip to content

Commit a2b4f5f

Browse files
authored
BAEL-9212 How to control tag order in SpringDoc OpenAPI (#18438)
* Create BookController.java * Create Book.java * Update BookController.java * Update Book.java * Update BookController.java * Update application.properties * Rename BookController.java to BookController.java * Update BookController.java * Create Book.java * Delete spring-boot-modules/spring-boot-springdoc/src/main/java/com/baeldung/springdoc/tag-order-example directory * Update BookController.java * Update BookController.java * Update and rename BookController.java to BooksController.java * Update BooksController.java * Update application.properties * Update BooksController.java * Create BooksController_2.java * Update BooksController.java * Update BooksController_2.java
1 parent 382896c commit a2b4f5f

File tree

4 files changed

+150
-1
lines changed

4 files changed

+150
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.baeldung.springdoc.tagorderexample;
2+
3+
public class Book {
4+
private long id;
5+
6+
public void setId(long id) {
7+
this.id = id;
8+
}
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package com.baeldung.springdoc.tagorderexample;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
import jakarta.validation.Valid;
7+
import jakarta.validation.constraints.NotBlank;
8+
import jakarta.validation.constraints.NotNull;
9+
import jakarta.validation.constraints.Size;
10+
11+
import org.springframework.validation.annotation.Validated;
12+
import org.springframework.web.bind.annotation.DeleteMapping;
13+
import org.springframework.web.bind.annotation.GetMapping;
14+
import org.springframework.web.bind.annotation.PostMapping;
15+
import org.springframework.web.bind.annotation.PutMapping;
16+
import org.springframework.web.bind.annotation.RequestBody;
17+
import org.springframework.web.bind.annotation.RequestParam;
18+
import org.springframework.web.bind.annotation.RestController;
19+
20+
import io.swagger.v3.oas.annotations.tags.Tag;
21+
22+
@RestController
23+
@Validated
24+
@Tag(name = "tag_at_class_level", description = "Books related class level tag")
25+
public class BooksController {
26+
27+
@Tag(name = "create")
28+
@Tag(name = "common_tag_at_method_level")
29+
@Tag(name = "createBook")
30+
@PostMapping(path = "/book")
31+
@io.swagger.v3.oas.annotations.parameters.RequestBody(required = true)
32+
public Book book(@Valid @RequestBody Book book) {
33+
34+
return book;
35+
}
36+
37+
@Tag(name = "find")
38+
@Tag(name = "common_tag_at_method_level")
39+
@Tag(name = "findBook", description = "Find Books related tag")
40+
@GetMapping(path = "/findBookById")
41+
public List findById(@RequestParam(name = "id", required = true)
42+
@NotNull @NotBlank @Size(max = 10) long id) {
43+
List bookList = new ArrayList<>();
44+
Book book = new Book();
45+
46+
book.setId(1);
47+
bookList.add(book);
48+
return bookList;
49+
}
50+
51+
@Tag(name = "delete")
52+
@Tag(name = "common_tag_at_method_level")
53+
@Tag(name = "deleteBook")
54+
@DeleteMapping(path = "/deleteBookById")
55+
public long deleteById(@RequestParam(name = "id", required = true)
56+
@NotNull @NotBlank @Size(max = 10) long id) {
57+
58+
return id;
59+
}
60+
61+
@Tag(name = "update")
62+
@Tag(name = "common_tag_at_method_level")
63+
@Tag(name = "updateBook")
64+
@PutMapping(path = "/updateBookById")
65+
public long updateById(@RequestParam(name = "id", required = true)
66+
@NotNull @NotBlank @Size(max = 10) long id) {
67+
return id;
68+
}
69+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package com.baeldung.springdoc.tagorderexample;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
import jakarta.validation.Valid;
7+
import jakarta.validation.constraints.NotBlank;
8+
import jakarta.validation.constraints.NotNull;
9+
import jakarta.validation.constraints.Size;
10+
11+
import org.springframework.validation.annotation.Validated;
12+
import org.springframework.web.bind.annotation.DeleteMapping;
13+
import org.springframework.web.bind.annotation.GetMapping;
14+
import org.springframework.web.bind.annotation.PostMapping;
15+
import org.springframework.web.bind.annotation.PutMapping;
16+
import org.springframework.web.bind.annotation.RequestBody;
17+
import org.springframework.web.bind.annotation.RequestParam;
18+
import org.springframework.web.bind.annotation.RestController;
19+
20+
import io.swagger.v3.oas.annotations.tags.Tag;
21+
import io.swagger.v3.oas.annotations.OpenAPIDefinition;
22+
23+
@RestController
24+
@Validated
25+
@OpenAPIDefinition(tags = { @Tag(name = "create", description = "Add book to inventory"),
26+
@Tag(name = "delete", description = "Delete book from inventory"),
27+
@Tag(name = "find", description = "Find book from inventory"),
28+
@Tag(name = "update", description = "Update book in inventory"),
29+
@Tag(name = "createBook", description = "Add book to inventory"),
30+
@Tag(name = "deleteBook", description = "Delete book from inventory"),
31+
@Tag(name = "findBook", description = "Find book from inventory"),
32+
@Tag(name = "updateBook", description = "Update book in inventory") })
33+
public class BooksController_2 {
34+
35+
@Tag(name = "create")
36+
@Tag(name = "createBook")
37+
@PostMapping(path = "/addBook")
38+
@io.swagger.v3.oas.annotations.parameters.RequestBody(required = true)
39+
public Book addBook(@Valid @RequestBody Book book) {
40+
41+
return book;
42+
}
43+
44+
@Tag(name = "find")
45+
@Tag(name = "findBook")
46+
@GetMapping(path = "/findABookById")
47+
public List<Book> findABookById(@RequestParam(name = "id", required = true) @NotNull @NotBlank @Size(max = 10) long id) {
48+
List<Book> bookList = new ArrayList<>();
49+
Book book = new Book();
50+
51+
book.setId(1);
52+
bookList.add(book);
53+
return bookList;
54+
}
55+
56+
@Tag(name = "delete")
57+
@Tag(name = "deleteBook")
58+
@DeleteMapping(path = "/deleteABookById")
59+
public long deleteABookById(@RequestParam(name = "id", required = true) @NotNull @NotBlank @Size(max = 10) long id) {
60+
61+
return id;
62+
}
63+
64+
@Tag(name = "update")
65+
@Tag(name = "updateBook")
66+
@PutMapping(path = "/updateABookById")
67+
public long updateABookById(@RequestParam(name = "id", required = true) @NotNull @NotBlank @Size(max = 10) long id) {
68+
return id;
69+
}
70+
}

spring-boot-modules/spring-boot-springdoc/src/main/resources/application.properties

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,5 @@ tos.uri=terms-of-service
2121
api.server.url=https://www.baeldung.com
2222
api.description=The User API is used to create, update, and delete users. Users can be created with or without an associated account. If an account is created, the user will be granted the <strong>ROLE_USER</strong> role. If an account is not created, the user will be granted the <b>ROLE_USER</b> role.
2323
springdoc.swagger-ui.operationsSorter=alpha
24-
springdoc.swagger-ui.tagsSorter=alpha
24+
##springdoc.swagger-ui.tagsSorter=alpha
25+
##springdoc.writer-with-order-by-keys=true

0 commit comments

Comments
 (0)