File tree Expand file tree Collapse file tree 8 files changed +130
-5
lines changed
src/main/java/com/code/food Expand file tree Collapse file tree 8 files changed +130
-5
lines changed Original file line number Diff line number Diff line change 5
5
<parent >
6
6
<groupId >org.springframework.boot</groupId >
7
7
<artifactId >spring-boot-starter-parent</artifactId >
8
- <version >3.4 .2</version >
8
+ <version >3.3 .2</version >
9
9
<relativePath /> <!-- lookup parent from repository -->
10
10
</parent >
11
11
<groupId >com.code</groupId >
45
45
<artifactId >h2</artifactId >
46
46
<scope >runtime</scope >
47
47
</dependency >
48
+ <dependency >
49
+ <groupId >org.springdoc</groupId >
50
+ <artifactId >springdoc-openapi-starter-webmvc-ui</artifactId >
51
+ <version >2.2.0</version >
52
+ </dependency >
53
+ <dependency >
54
+ <groupId >io.swagger.core.v3</groupId >
55
+ <artifactId >swagger-models-jakarta</artifactId >
56
+ <version >2.2.9</version >
57
+ </dependency >
48
58
<dependency >
49
59
<groupId >org.springframework.boot</groupId >
50
60
<artifactId >spring-boot-starter-test</artifactId >
Original file line number Diff line number Diff line change
1
+ package com .code .food .config ;
2
+
3
+ import io .swagger .v3 .oas .models .Components ;
4
+ import io .swagger .v3 .oas .models .OpenAPI ;
5
+ import io .swagger .v3 .oas .models .info .Info ;
6
+ import org .springframework .context .annotation .Bean ;
7
+ import org .springframework .context .annotation .Configuration ;
8
+
9
+ @ Configuration
10
+ public class SwaggerConfiguration {
11
+
12
+ @ Bean
13
+ public OpenAPI customOpenAPI () {
14
+ return new OpenAPI ().components (new Components ())
15
+ .info (new Info ().title ("Swagger" ).description ("Documentação Assembleia" ));
16
+ }
17
+ }
Original file line number Diff line number Diff line change
1
+ package com .code .food .exception ;
2
+
3
+ import java .time .LocalDateTime ;
4
+
5
+ public class ApiError {
6
+
7
+ private int statusCode ;
8
+ private String message ;
9
+ private LocalDateTime date ;
10
+
11
+ public ApiError (int statusCode , String message , LocalDateTime date ) {
12
+ this .statusCode = statusCode ;
13
+ this .message = message ;
14
+ this .date = date ;
15
+ }
16
+
17
+ public int getStatusCode () {
18
+ return statusCode ;
19
+ }
20
+
21
+ public void setStatusCode (int statusCode ) {
22
+ this .statusCode = statusCode ;
23
+ }
24
+
25
+ public String getMessage () {
26
+ return message ;
27
+ }
28
+
29
+ public void setMessage (String message ) {
30
+ this .message = message ;
31
+ }
32
+
33
+ public LocalDateTime getDate () {
34
+ return date ;
35
+ }
36
+
37
+ public void setDate (LocalDateTime date ) {
38
+ this .date = date ;
39
+ }
40
+ }
Original file line number Diff line number Diff line change
1
+ package com .code .food .exception ;
2
+
3
+ public class BadRequestException extends RuntimeException {
4
+
5
+ public BadRequestException (String message ) {
6
+ super (message );
7
+ }
8
+ }
Original file line number Diff line number Diff line change
1
+ package com .code .food .exception ;
2
+
3
+ public class ForbiddenException extends RuntimeException {
4
+
5
+ public ForbiddenException (String message ) {
6
+ super (message );
7
+ }
8
+ }
Original file line number Diff line number Diff line change
1
+ package com .code .food .exception ;
2
+
3
+ import org .springframework .http .HttpStatus ;
4
+ import org .springframework .http .ResponseEntity ;
5
+ import org .springframework .web .bind .annotation .ControllerAdvice ;
6
+ import org .springframework .web .bind .annotation .ExceptionHandler ;
7
+
8
+ import java .time .LocalDateTime ;
9
+
10
+ @ ControllerAdvice
11
+ public class HttpErrorExceptionHandler {
12
+
13
+ private ResponseEntity <ApiError > buildErrorResponse (HttpStatus statusCode , String message ) {
14
+ var error = new ApiError (statusCode .value (), message , LocalDateTime .now ());
15
+ return ResponseEntity .status (statusCode ).body (error );
16
+ }
17
+
18
+ @ ExceptionHandler (BadRequestException .class )
19
+ public ResponseEntity <ApiError > badRequest (BadRequestException badRequestException ) {
20
+ return buildErrorResponse (HttpStatus .BAD_REQUEST , badRequestException .getMessage ());
21
+ }
22
+
23
+ //TODO: Criar tratamento de erro para usuários não encontrados, esse tratamento vai ser para o 404 NotFoundException
24
+ @ ExceptionHandler (NotFoundException .class )
25
+ public ResponseEntity <ApiError > notFound (NotFoundException notFoundException ) {
26
+ return buildErrorResponse (HttpStatus .NOT_FOUND , notFoundException .getMessage ());
27
+ }
28
+
29
+ @ ExceptionHandler (ForbiddenException .class )
30
+ public ResponseEntity <ApiError > forbidden (ForbiddenException forbiddenException ) {
31
+ return buildErrorResponse (HttpStatus .FORBIDDEN , forbiddenException .getMessage ());
32
+ }
33
+ }
Original file line number Diff line number Diff line change
1
+ package com .code .food .exception ;
2
+
3
+ public class NotFoundException extends RuntimeException {
4
+
5
+ public NotFoundException (String message ) {
6
+ super (message );
7
+ }
8
+ }
Original file line number Diff line number Diff line change 1
1
package com .code .food .validation ;
2
2
3
3
import com .code .food .entity .UserEntity ;
4
+ import com .code .food .exception .BadRequestException ;
4
5
5
6
public class UserValidator {
6
7
@@ -13,28 +14,28 @@ public static void validateFields(UserEntity userEntity) {
13
14
14
15
public static String validateName (String name ) {
15
16
if (name .isEmpty ()) {
16
- throw new RuntimeException ("O nome não pode ser vazio" );
17
+ throw new BadRequestException ("O nome não pode ser vazio" );
17
18
}
18
19
return name ;
19
20
}
20
21
21
22
public static String validateEmail (String email ) {
22
23
if (email .isEmpty ()) {
23
- throw new RuntimeException ("O email não pode ser vazio" );
24
+ throw new BadRequestException ("O e-mail não pode ser vazio" );
24
25
}
25
26
return email ;
26
27
}
27
28
28
29
public static String validatePassword (String password ) {
29
30
if (password .isEmpty ()) {
30
- throw new RuntimeException ("A senha não pode ser vazia" );
31
+ throw new BadRequestException ("A senha não pode ser vazia" );
31
32
}
32
33
return password ;
33
34
}
34
35
35
36
public static String validatePhone (String phone ) {
36
37
if (phone .isEmpty ()) {
37
- throw new RuntimeException ("O telefone não pode ser vazio" );
38
+ throw new BadRequestException ("O telefone não pode ser vazio" );
38
39
}
39
40
return phone ;
40
41
}
You can’t perform that action at this time.
0 commit comments