Skip to content

Commit a7b5554

Browse files
Ramesh FadatareRamesh Fadatare
Ramesh Fadatare
authored and
Ramesh Fadatare
committed
springdoc rest api documentation
1 parent 02a80ff commit a7b5554

File tree

4 files changed

+95
-7
lines changed

4 files changed

+95
-7
lines changed

pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,12 @@
8282
<version>0.11.5</version>
8383
<scope>runtime</scope>
8484
</dependency>
85+
<!-- https://mvnrepository.com/artifact/org.springdoc/springdoc-openapi-starter-webmvc-ui -->
86+
<dependency>
87+
<groupId>org.springdoc</groupId>
88+
<artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
89+
<version>2.0.4</version>
90+
</dependency>
8591
</dependencies>
8692

8793
<build>

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 = "Bear Authentication",
29+
type = SecuritySchemeType.HTTP,
30+
bearerFormat = "JWT",
31+
scheme = "bearer"
32+
)
2533
public class SecurityConfig {
2634

2735
private UserDetailsService userDetailsService;
@@ -57,6 +65,8 @@ SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
5765
authorize.requestMatchers(HttpMethod.GET, "/api/**").permitAll()
5866
//.requestMatchers(HttpMethod.GET, "/api/categories/**").permitAll()
5967
.requestMatchers("/api/auth/**").permitAll()
68+
.requestMatchers("/swagger-ui/**").permitAll()
69+
.requestMatchers("/v3/api-docs/**").permitAll()
6070
.anyRequest().authenticated()
6171

6272
).exceptionHandling( exception -> exception

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

Lines changed: 63 additions & 7 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 org.springframework.http.HttpStatus;
812
import org.springframework.http.ResponseEntity;
913
import org.springframework.security.access.prepost.PreAuthorize;
@@ -13,7 +17,10 @@
1317
import java.util.List;
1418

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

1926
private PostService postService;
@@ -22,15 +29,34 @@ public PostController(PostService postService) {
2229
this.postService = postService;
2330
}
2431

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

50+
@Operation(
51+
summary = "Get All Posts REST API",
52+
description = "Get All Posts REST API is used to fetch all the posts from the database"
53+
)
54+
@ApiResponse(
55+
responseCode = "200",
56+
description = "Http Status 200 SUCCESS"
57+
)
3258
// get all posts rest api
33-
@GetMapping("/api/v1/posts")
59+
@GetMapping
3460
public PostResponse getAllPosts(
3561
@RequestParam(value = "pageNo", defaultValue = AppConstants.DEFAULT_PAGE_NUMBER, required = false) int pageNo,
3662
@RequestParam(value = "pageSize", defaultValue = AppConstants.DEFAULT_PAGE_SIZE, required = false) int pageSize,
@@ -40,25 +66,55 @@ public PostResponse getAllPosts(
4066
return postService.getAllPosts(pageNo, pageSize, sortBy, sortDir);
4167
}
4268

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

83+
@Operation(
84+
summary = "update Post REST API",
85+
description = "Update Post REST API is used to update a particular post in the database"
86+
)
87+
@ApiResponse(
88+
responseCode = "200",
89+
description = "Http Status 200 SUCCESS"
90+
)
91+
@SecurityRequirement(
92+
name = "Bear Authentication"
93+
)
4994
// update post by id rest api
5095
@PreAuthorize("hasRole('ADMIN')")
51-
@PutMapping("/api/v1/posts/{id}")
96+
@PutMapping("/{id}")
5297
public ResponseEntity<PostDto> updatePost(@Valid @RequestBody PostDto postDto, @PathVariable(name = "id") long id){
5398

5499
PostDto postResponse = postService.updatePost(postDto, id);
55100

56101
return new ResponseEntity<>(postResponse, HttpStatus.OK);
57102
}
58103

104+
@Operation(
105+
summary = "Delete Post REST API",
106+
description = "Delete Post REST API is used to delete a particular post from the database"
107+
)
108+
@ApiResponse(
109+
responseCode = "200",
110+
description = "Http Status 200 SUCCESS"
111+
)
112+
@SecurityRequirement(
113+
name = "Bear Authentication"
114+
)
59115
// delete post rest api
60116
@PreAuthorize("hasRole('ADMIN')")
61-
@DeleteMapping("/api/v1/posts/{id}")
117+
@DeleteMapping("/{id}")
62118
public ResponseEntity<String> deletePost(@PathVariable(name = "id") long id){
63119

64120
postService.deletePostById(id);
@@ -68,7 +124,7 @@ public ResponseEntity<String> deletePost(@PathVariable(name = "id") long id){
68124

69125
// Build Get Posts by Category REST API
70126
// http://localhost:8080/api/posts/category/3
71-
@GetMapping("/api/v1/posts/category/{id}")
127+
@GetMapping("/category/{id}")
72128
public ResponseEntity<List<PostDto>> getPostsByCategory(@PathVariable("id") Long categoryId){
73129
List<PostDto> postDtos = postService.getPostsByCategory(categoryId);
74130
return ResponseEntity.ok(postDtos);
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 lombok.Data;
45

56
import jakarta.validation.constraints.NotEmpty;
67
import jakarta.validation.constraints.Size;
78
import java.util.Set;
89

910
@Data
11+
@Schema(
12+
description = "PostDto Model Information"
13+
)
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 not 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)