Skip to content

Commit 55fcc24

Browse files
committed
Swagger Api documentation is done successfully.
1 parent d1f806b commit 55fcc24

File tree

9 files changed

+116
-1
lines changed

9 files changed

+116
-1
lines changed

pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,12 @@
6464
<artifactId>jjwt</artifactId>
6565
<version>0.9.1</version>
6666
</dependency>
67+
<!-- https://mvnrepository.com/artifact/io.springfox/springfox-boot-starter -->
68+
<dependency>
69+
<groupId>io.springfox</groupId>
70+
<artifactId>springfox-boot-starter</artifactId>
71+
<version>3.0.0</version>
72+
</dependency>
6773

6874
<dependency>
6975
<groupId>org.springframework.boot</groupId>

src/main/java/com/springboot/blog/rest/api/config/SecurityConfig.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,12 @@ protected void configure(HttpSecurity http) throws Exception {
4949
.authorizeRequests()
5050
.antMatchers(HttpMethod.GET, "/api/posts/**").permitAll()
5151
.antMatchers( "/api/auth/**").permitAll()
52+
//swagger link
53+
.antMatchers("/v2/api-docs/**").permitAll()
54+
.antMatchers("/swagger-ui/**").permitAll()
55+
.antMatchers("/swagger-resources/**").permitAll()
56+
.antMatchers("/swagger-ui.html").permitAll()
57+
.antMatchers("/webjars/**").permitAll()
5258
.anyRequest()
5359
.authenticated();
5460
//Basic auth
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package com.springboot.blog.rest.api.config;
2+
3+
import org.springframework.context.annotation.Bean;
4+
import org.springframework.context.annotation.Configuration;
5+
import springfox.documentation.RequestHandler;
6+
import springfox.documentation.builders.PathSelectors;
7+
import springfox.documentation.builders.RequestHandlerSelectors;
8+
import springfox.documentation.service.*;
9+
import springfox.documentation.spi.DocumentationType;
10+
import springfox.documentation.spi.service.contexts.SecurityContext;
11+
import springfox.documentation.spring.web.plugins.Docket;
12+
13+
import java.util.Arrays;
14+
import java.util.Collections;
15+
import java.util.List;
16+
17+
@Configuration
18+
public class SwaggerConfig {
19+
public static final String AUTHORIZATION_HEADER = "Authorization";
20+
21+
private ApiKey apiKey(){
22+
return new ApiKey("JWT", AUTHORIZATION_HEADER, "header");
23+
}
24+
private ApiInfo apiInfo(){
25+
return new ApiInfo(
26+
"Spring Boot Blog Rest Api",
27+
"Spring Boot Blog Rest Api Documentation",
28+
"1",
29+
"Terms of service",
30+
new Contact("Mahamat Nour","www.github.com/manirDev", "[email protected]"),
31+
"Licence of the API",
32+
"Api licence Url",
33+
Collections.emptyList()
34+
);
35+
}
36+
37+
@Bean
38+
public Docket api(){
39+
return new Docket(DocumentationType.SWAGGER_2)
40+
.apiInfo(apiInfo())
41+
//enable jwt
42+
.securityContexts(Arrays.asList(securityContext()))
43+
.securitySchemes(Arrays.asList(apiKey()))
44+
//----------------------------
45+
.select()
46+
.apis(RequestHandlerSelectors.any())
47+
.paths(PathSelectors.any())
48+
.build();
49+
}
50+
51+
private SecurityContext securityContext(){
52+
return SecurityContext.builder().securityReferences(defaultAuth()).build();
53+
}
54+
55+
private List<SecurityReference> defaultAuth(){
56+
AuthorizationScope authorizationScope = new AuthorizationScope("global", "accessEverything");
57+
AuthorizationScope[] authorizationScopes = new AuthorizationScope[1];
58+
authorizationScopes[0] = authorizationScope;
59+
60+
return Arrays.asList(new SecurityReference("JWt", authorizationScopes));
61+
}
62+
63+
}

src/main/java/com/springboot/blog/rest/api/controller/AuthController.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
import com.springboot.blog.rest.api.repository.UserRepository;
1010
import com.springboot.blog.rest.api.security.JwtTokenProvider;
1111
import com.springboot.blog.rest.api.service.UserRegisterService;
12+
import io.swagger.annotations.Api;
13+
import io.swagger.annotations.ApiOperation;
1214
import org.springframework.beans.factory.annotation.Autowired;
1315
import org.springframework.http.HttpStatus;
1416
import org.springframework.http.ResponseEntity;
@@ -21,6 +23,7 @@
2123

2224
import java.util.Collections;
2325

26+
@Api(value = "Auth Controller provide signIn and signUp Rest APIs")
2427
@RestController
2528
@RequestMapping(path = "api/auth")
2629
public class AuthController {
@@ -33,6 +36,8 @@ public class AuthController {
3336
private UserRegisterService userRegisterService;
3437
@Autowired
3538
private JwtTokenProvider jwtTokenProvider;
39+
40+
@ApiOperation(value = "Rest API for User register to the Blog app")
3641
@PostMapping("/signIn")
3742
public ResponseEntity<JwtAuthResponse> authenticator(@RequestBody LoginDto loginDto){
3843
Authentication authentication = authenticationManager.authenticate(new UsernamePasswordAuthenticationToken(
@@ -47,6 +52,7 @@ public ResponseEntity<JwtAuthResponse> authenticator(@RequestBody LoginDto login
4752
return ResponseEntity.ok(new JwtAuthResponse(token));
4853
}
4954

55+
@ApiOperation(value = "Rest API for User login to the Blog app")
5056
@PostMapping("/signUp")
5157
public ResponseEntity<?> registerUser(@RequestBody SignUpDto signUpDto){
5258
//check if email or username exist

src/main/java/com/springboot/blog/rest/api/controller/CommentController.java

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

33
import com.springboot.blog.rest.api.dto.CommentDto;
44
import com.springboot.blog.rest.api.service.CommentService;
5+
import io.swagger.annotations.Api;
6+
import io.swagger.annotations.ApiOperation;
57
import org.springframework.http.HttpStatus;
68
import org.springframework.http.ResponseEntity;
79
import org.springframework.web.bind.annotation.*;
@@ -10,6 +12,7 @@
1012
import java.security.PublicKey;
1113
import java.util.List;
1214

15+
@Api(value = "CRUD rest API for Comment resource")
1316
@RestController
1417
@RequestMapping("api/posts")
1518
public class CommentController {
@@ -19,19 +22,22 @@ public CommentController(CommentService commentService) {
1922
}
2023

2124
//create comment for post api
25+
@ApiOperation(value = "Rest API for creating a comment")
2226
@PostMapping("/{postId}/comments")
2327
public ResponseEntity<CommentDto> createComment(@Valid @RequestBody CommentDto commentDto,
2428
@PathVariable Long postId){
2529
return new ResponseEntity<>(commentService.createComment(commentDto, postId), HttpStatus.CREATED);
2630
}
2731

2832
//get comments by post id api
33+
@ApiOperation(value = "Rest API for getting all comments")
2934
@GetMapping("/{postId}/getComments")
3035
public List<CommentDto> getAllCommentsByPostId(@PathVariable Long postId){
3136
return commentService.getAllCommentsByPostId(postId);
3237
}
3338

3439
//get comment by post id and comment id
40+
@ApiOperation(value = "Rest API for getting a comment by id")
3541
@GetMapping("/{postId}/comment/{commentId}")
3642
public ResponseEntity<CommentDto> getCommentById(@PathVariable Long postId,
3743
@PathVariable Long commentId){
@@ -41,6 +47,7 @@ public ResponseEntity<CommentDto> getCommentById(@PathVariable Long postId,
4147
}
4248

4349
//update comment
50+
@ApiOperation(value = "Rest API for updating a comment")
4451
@PutMapping("/{postId}/updateComment/{commentId}")
4552
public ResponseEntity<CommentDto> updateComment(@PathVariable Long postId,
4653
@Valid @RequestBody CommentDto commentDto,
@@ -50,6 +57,7 @@ public ResponseEntity<CommentDto> updateComment(@PathVariable Long postId,
5057
}
5158

5259
//delete comment
60+
@ApiOperation(value = "Rest API for deleting a comment")
5361
@DeleteMapping("/{postId}/deleteComment/{commentId}")
5462
public ResponseEntity<String> deleteComment(@PathVariable Long postId,
5563
@PathVariable Long commentId){

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,17 @@
44
import com.springboot.blog.rest.api.dto.PostResponse;
55
import com.springboot.blog.rest.api.service.PostService;
66
import com.springboot.blog.rest.api.utils.Constant;
7+
import io.swagger.annotations.Api;
8+
import io.swagger.annotations.ApiOperation;
9+
import io.swagger.annotations.ApiResponse;
710
import org.springframework.http.HttpStatus;
811
import org.springframework.http.ResponseEntity;
912
import org.springframework.security.access.prepost.PreAuthorize;
1013
import org.springframework.web.bind.annotation.*;
1114

1215
import javax.validation.Valid;
1316

14-
17+
@Api(value = "CRUD rest API for Post resource")
1518
@RestController
1619
@RequestMapping(path = "api/posts")
1720
public class PostController {
@@ -23,6 +26,7 @@ public PostController(PostService postService) {
2326
}
2427

2528
//add post api
29+
@ApiOperation(value = "Rest API for creating a post")
2630
@PreAuthorize("hasRole('ADMIN')")
2731
@PostMapping("/createPost")
2832
public ResponseEntity<PostDto> createPost(@Valid @RequestBody PostDto postDto){
@@ -33,6 +37,7 @@ public ResponseEntity<PostDto> createPost(@Valid @RequestBody PostDto postDto){
3337
}
3438

3539
//get all posts api
40+
@ApiOperation(value = "Rest API for getting all posts")
3641
@GetMapping("/allPosts")
3742
public PostResponse getAllPosts(
3843
@RequestParam(value ="pageNo", defaultValue = Constant.DEFAULT_PAGE_NUMBER, required = false) int pageNo,
@@ -44,12 +49,14 @@ public PostResponse getAllPosts(
4449
}
4550

4651
//get post by id api
52+
@ApiOperation(value = "Rest API for getting a post by postId")
4753
@GetMapping("/getPost/{postId}")
4854
public ResponseEntity<PostDto> getPostById(@PathVariable Long postId){
4955
return ResponseEntity.ok(postService.getPostById(postId));
5056
}
5157

5258
//update post api
59+
@ApiOperation(value = "Rest API for updating a post")
5360
@PreAuthorize("hasRole('ADMIN')")
5461
@PutMapping("/update/{postId}")
5562
public ResponseEntity<PostDto> updatePost(@Valid @RequestBody PostDto postDto, @PathVariable Long postId){
@@ -58,6 +65,7 @@ public ResponseEntity<PostDto> updatePost(@Valid @RequestBody PostDto postDto, @
5865
}
5966

6067
//delete post api
68+
@ApiOperation(value = "Rest API for deleting a post")
6169
@PreAuthorize("hasRole('ADMIN')")
6270
@DeleteMapping("/delete/{postId}")
6371
public ResponseEntity<String> deletePost(@PathVariable Long postId){

src/main/java/com/springboot/blog/rest/api/dto/CommentDto.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,26 @@
11
package com.springboot.blog.rest.api.dto;
22

3+
import io.swagger.annotations.ApiModel;
4+
import io.swagger.annotations.ApiModelProperty;
35
import lombok.Data;
46

57
import javax.validation.constraints.Email;
68
import javax.validation.constraints.NotEmpty;
79
import javax.validation.constraints.Size;
810

11+
@ApiModel(value = "Comment resource information")
912
@Data
1013
public class CommentDto {
14+
@ApiModelProperty(value = "Blog comment id")
1115
private Long id;
16+
@ApiModelProperty(value = "Blog comment name")
1217
@NotEmpty(message = "Name should not be empty or null")
1318
private String name;
19+
@ApiModelProperty(value = "Blog comment email")
1420
@NotEmpty(message = "Email should not be empty or null")
1521
@Email
1622
private String email;
23+
@ApiModelProperty(value = "Blog comment body")
1724
@NotEmpty
1825
@Size(min = 5, message = "Body should be at least 5 characters")
1926
private String body;
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,30 @@
11
package com.springboot.blog.rest.api.dto;
22

3+
import io.swagger.annotations.ApiModel;
4+
import io.swagger.annotations.ApiModelProperty;
35
import lombok.Data;
46

57
import javax.validation.constraints.NotEmpty;
68
import javax.validation.constraints.Size;
79
import java.util.Set;
810

11+
@ApiModel(description = "Post resource information")
912
@Data
1013
public class PostDto {
1114

15+
@ApiModelProperty(value = "Blog post id")
1216
private Long id;
17+
@ApiModelProperty(value = "Blog post title")
1318
@NotEmpty
1419
@Size(min = 2, message = "Post title should have at least 2 characters")
1520
private String title;
21+
@ApiModelProperty(value = "Blog post description")
1622
@NotEmpty
1723
@Size(min = 5, message = "Post description should have at least 2 characters")
1824
private String description;
25+
@ApiModelProperty(value = "Blog post content")
1926
@NotEmpty
2027
private String content;
28+
@ApiModelProperty(value = "Blog post comments")
2129
private Set<CommentDto> comments;
2230
}

src/main/resources/application.properties

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,6 @@ logging.level.org.springframework.security=DEBUG
1313
app.jwt-secret = JWTSecretKey
1414
app.jwt-expiration-milliseconds = 604800000
1515

16+
#Swagger enabling
17+
spring.mvc.pathmatch.matching-strategy = ANT_PATH_MATCHER
18+

0 commit comments

Comments
 (0)