Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 09f6c6f

Browse files
Merge pull request #4 from adiletkdev/develop
SpringDoc OpenAPI (Swagger)
2 parents bdcfd0c + d61d29a commit 09f6c6f

File tree

5 files changed

+113
-0
lines changed

5 files changed

+113
-0
lines changed

‎pom.xml‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,12 @@
8989
<scope>runtime</scope>
9090
</dependency>
9191

92+
<!-- https://mvnrepository.com/artifact/org.springdoc/springdoc-openapi-starter-webmvc-ui -->
93+
<dependency>
94+
<groupId>org.springdoc</groupId>
95+
<artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
96+
<version>2.1.0</version>
97+
</dependency>
9298

9399
</dependencies>
94100

‎src/main/java/com/springboot/blog/SpringbootBlogRestApiApplication.java‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,36 @@
11
package com.springboot.blog;
22

3+
import io.swagger.v3.oas.annotations.ExternalDocumentation;
4+
import io.swagger.v3.oas.annotations.OpenAPIDefinition;
5+
import io.swagger.v3.oas.annotations.info.Contact;
6+
import io.swagger.v3.oas.annotations.info.Info;
7+
import io.swagger.v3.oas.annotations.info.License;
38
import org.modelmapper.ModelMapper;
49
import org.springframework.boot.SpringApplication;
510
import org.springframework.boot.autoconfigure.SpringBootApplication;
611
import org.springframework.context.annotation.Bean;
712

813
@SpringBootApplication
14+
@OpenAPIDefinition(
15+
info = @Info(
16+
title = "Spring Boot Blog App REST APIs",
17+
description = "Spring Boot Blog App REST APIs documentation",
18+
version = "v1.0",
19+
contact = @Contact(
20+
name = "Adilet Kozubaev",
21+
email = "adiletkdev@gmail.com",
22+
url = "https://www.******.net"
23+
),
24+
license = @License(
25+
name = "Apache 2.0",
26+
url = "https://www.******.net/license"
27+
)
28+
),
29+
externalDocs = @ExternalDocumentation(
30+
description = "Spring Boot Blog App documentation",
31+
url = "https://github.com/adiletkdev/springboot-blog-rest-api"
32+
)
33+
)
934
public class SpringbootBlogRestApiApplication {
1035

1136
@Bean

‎src/main/java/com/springboot/blog/config/SecurityConfig.java‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
import com.springboot.blog.security.JwtAuthenticationEntryPoint;
44
import com.springboot.blog.security.JwtAuthenticationFilter;
5+
import io.swagger.v3.oas.annotations.enums.SecuritySchemeType;
6+
import io.swagger.v3.oas.annotations.security.SecurityScheme;
57
import org.springframework.context.annotation.Bean;
68
import org.springframework.context.annotation.Configuration;
79
import org.springframework.http.HttpMethod;
@@ -22,6 +24,12 @@
2224

2325
@Configuration
2426
@EnableMethodSecurity
27+
@SecurityScheme(
28+
name = "Bearer Authentication",
29+
type = SecuritySchemeType.HTTP,
30+
bearerFormat = "JWT",
31+
scheme = "bearer"
32+
)
2533
public class SecurityConfig {
2634

2735
private UserDetailsService userDetailsService;
@@ -56,6 +64,8 @@ SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
5664
//authorize.anyRequest().authenticated()
5765
authorize.requestMatchers(HttpMethod.GET, "/api/**").permitAll()
5866
.requestMatchers("/api/auth/**").permitAll()
67+
.requestMatchers("/swagger-ui/**").permitAll()
68+
.requestMatchers("/v3/api-docs/**").permitAll()
5969
.anyRequest().authenticated()
6070

6171
).exceptionHandling(exception -> exception

‎src/main/java/com/springboot/blog/controller/PostController.java‎

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44
import com.springboot.blog.payload.PostResponse;
55
import com.springboot.blog.service.PostService;
66
import com.springboot.blog.utils.AppConstants;
7+
import io.swagger.v3.oas.annotations.Operation;
8+
import io.swagger.v3.oas.annotations.responses.ApiResponse;
9+
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
10+
import io.swagger.v3.oas.annotations.tags.Tag;
711
import jakarta.validation.Valid;
812
import org.springframework.http.HttpStatus;
913
import org.springframework.http.ResponseEntity;
@@ -14,6 +18,9 @@
1418

1519
@RestController
1620
@RequestMapping("/api/posts")
21+
@Tag(
22+
name = "CRUD REST APIs for Post Resource"
23+
)
1724
public class PostController {
1825

1926
private PostService postService;
@@ -23,13 +30,32 @@ public PostController(PostService postService) {
2330
}
2431

2532
// create blog post
33+
@Operation(
34+
summary = "Create Post REST API",
35+
description = "Create Post REST API is used to save post into database"
36+
)
37+
@ApiResponse(
38+
responseCode = "201",
39+
description = "Http Status 201 CREATED"
40+
)
41+
@SecurityRequirement(
42+
name = "Bearer Authentication"
43+
)
2644
@PreAuthorize("hasRole('ADMIN')")
2745
@PostMapping
2846
public ResponseEntity<PostDto> createPost(@Valid @RequestBody PostDto postDto) {
2947
return new ResponseEntity<>(postService.createPost(postDto), HttpStatus.CREATED);
3048
}
3149

3250
// get all posts rest api
51+
@Operation(
52+
summary = "Get All Posts REST API",
53+
description = "Get All Posts REST API is used to fetch all the posts from the database"
54+
)
55+
@ApiResponse(
56+
responseCode = "200",
57+
description = "Http Status 200 SUCCESS"
58+
)
3359
@GetMapping
3460
public PostResponse getAllPosts(
3561
@RequestParam(value = "pageNo", defaultValue = AppConstants.DEFAULT_PAGE_NUMBER, required = false) int pageNo,
@@ -41,12 +67,31 @@ public PostResponse getAllPosts(
4167
}
4268

4369
// get post by id
70+
@Operation(
71+
summary = "Get Post by Id REST API",
72+
description = "Get Post by Id REST API is used to get single post from the database"
73+
)
74+
@ApiResponse(
75+
responseCode = "200",
76+
description = "Http Status 200 SUCCESS"
77+
)
4478
@GetMapping("/{id}")
4579
public ResponseEntity<PostDto> getPostId(@PathVariable(name = "id") long id) {
4680
return ResponseEntity.ok(postService.getPostById(id));
4781
}
4882

4983
// update post by id rest api
84+
@Operation(
85+
summary = "Update Post REST API",
86+
description = "Update Post REST API is used to update a particular post in the database"
87+
)
88+
@ApiResponse(
89+
responseCode = "200",
90+
description = "Http Status 200 SUCCESS"
91+
)
92+
@SecurityRequirement(
93+
name = "Bearer Authentication"
94+
)
5095
@PreAuthorize("hasRole('ADMIN')")
5196
@PutMapping("/{id}")
5297
public ResponseEntity<PostDto> updatePost(
@@ -59,6 +104,17 @@ public ResponseEntity<PostDto> updatePost(
59104
}
60105

61106
// delete post rest api
107+
@Operation(
108+
summary = "Delete Post REST API",
109+
description = "Delete Post REST API is used to delete a particular post from the database"
110+
)
111+
@ApiResponse(
112+
responseCode = "200",
113+
description = "Http Status 200 SUCCESS"
114+
)
115+
@SecurityRequirement(
116+
name = "Bearer Authentication"
117+
)
62118
@PreAuthorize("hasRole('ADMIN')")
63119
@DeleteMapping("/{id}")
64120
public ResponseEntity<String> deletePost(@PathVariable(name = "id") long id) {
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,47 @@
11
package com.springboot.blog.payload;
22

3+
import io.swagger.v3.oas.annotations.media.Schema;
34
import jakarta.validation.constraints.NotEmpty;
45
import jakarta.validation.constraints.Size;
56
import lombok.Data;
67

78
import java.util.Set;
89

10+
@Schema(
11+
description = "PostDto Model Information"
12+
)
913
@Data
1014
public class PostDto {
1115
private long id;
1216

17+
@Schema(
18+
description = "Blog Post Title"
19+
)
1320
// title should not be null or empty
1421
// title should have at least 2 characters
1522
@NotEmpty
1623
@Size(min = 2, message = "Post title should have at least 2 characters")
1724
private String title;
1825

26+
@Schema(
27+
description = "Blog Post Description"
28+
)
1929
// post description should be null or empty
2030
// post description should have at least 10 characters
2131
@NotEmpty
2232
@Size(min = 10, message = "Post description should have at least 10 characters")
2333
private String description;
2434

35+
@Schema(
36+
description = "Blog Post Content"
37+
)
2538
// post content should not be null or empty
2639
@NotEmpty
2740
private String content;
2841
private Set<CommentDto> comments;
2942

43+
@Schema(
44+
description = "Blog Post Category"
45+
)
3046
private Long categoryId;
3147
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /