Skip to content

Commit f622e16

Browse files
committed
exception: done
1 parent 78b23dc commit f622e16

6 files changed

Lines changed: 55 additions & 3 deletions

File tree

src/main/java/com/example/firstAPI/controller/UserController.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
import com.example.firstAPI.dto.response.ResponseError;
88
import com.example.firstAPI.dto.response.ResponseFailure;
99
import com.example.firstAPI.dto.response.ResponseSuccess;
10+
import com.example.firstAPI.exception.ResourceNotFoundException;
11+
import com.example.firstAPI.services.UserService;
12+
import com.example.firstAPI.services.impl.UserServiceEmpl;
1013
import jakarta.validation.Valid;
1114
import org.springframework.http.HttpStatus;
1215
import org.springframework.validation.annotation.Validated;
@@ -17,13 +20,15 @@
1720
@RestController
1821
@RequestMapping("/user")
1922
public class UserController {
23+
private UserService userService;
2024
@PostMapping("/")
2125
public ResponData<?> createUser(@Valid @RequestBody UserRequestDTO user){
2226
System.out.println("Request add user: " + user.getFirstName());
2327
try{
28+
userService.addUser(user);
2429
return new ResponData<>(HttpStatus.CREATED.value(), "User added successfully y");
2530
} catch (Exception e) {
26-
return new ResponseError<>(HttpStatus.BAD_REQUEST.value(), e.getMessage());
31+
throw new ResourceNotFoundException(e.getMessage());
2732
}
2833
}
2934
@GetMapping("/{id}")

src/main/java/com/example/firstAPI/dto/request/UserRequestDTO.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
public class UserRequestDTO {
88
@NotNull(message = "lastName must be not null")
99
private String firstName;
10-
@NotEmpty(message = "addresses can not empty")
10+
@NotEmpty(message = "lassName can not empty")
1111
private String lastName;
1212
private String email;
1313
private Integer age;

src/main/java/com/example/firstAPI/exception/GlobalExceptionHandler.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515

1616
@RestControllerAdvice
1717
public class GlobalExceptionHandler {
18-
// request: request từ api
1918
@ExceptionHandler({MethodArgumentNotValidException.class, MissingServletRequestParameterException.class, ConstraintViolationException.class})
2019
@ResponseStatus(HttpStatus.BAD_REQUEST)
2120

@@ -44,6 +43,19 @@ public ErrorResponse handlerValidationException(Exception e, WebRequest request)
4443
errorResponse.setMessage(message);
4544
}
4645
return errorResponse;
46+
}
47+
48+
49+
@ExceptionHandler({ResourceNotFoundException.class})
50+
@ResponseStatus(HttpStatus.NOT_FOUND)
51+
public ErrorResponse handleResourceNotFoundException(ResourceNotFoundException e, WebRequest request){
52+
ErrorResponse errorResponse = new ErrorResponse();
53+
errorResponse.setTimestamp(new Date());
54+
errorResponse.setPath(request.getDescription(false).replace("uri=", ""));
55+
errorResponse.setStatus(HttpStatus.NOT_FOUND.value());
56+
errorResponse.setError(HttpStatus.NOT_FOUND.getReasonPhrase());
57+
errorResponse.setMessage(e.getMessage());
4758

59+
return errorResponse;
4860
}
4961
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.example.firstAPI.exception;
2+
3+
public class ResourceNotFoundException extends RuntimeException{
4+
public ResourceNotFoundException(String message) {
5+
super(message);
6+
}
7+
8+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.example.firstAPI.services;
2+
3+
import com.example.firstAPI.dto.request.UserRequestDTO;
4+
5+
public interface UserService {
6+
int addUser(UserRequestDTO requestDTO);
7+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.example.firstAPI.services.impl;
2+
3+
import com.example.firstAPI.dto.request.UserRequestDTO;
4+
import com.example.firstAPI.exception.ResourceNotFoundException;
5+
import com.example.firstAPI.services.UserService;
6+
import org.springframework.boot.context.config.ConfigDataResourceNotFoundException;
7+
import org.springframework.stereotype.Service;
8+
9+
@Service
10+
public class UserServiceEmpl implements UserService {
11+
12+
@Override
13+
public int addUser(UserRequestDTO requestDTO) {
14+
System.out.println("Save user to db");
15+
if(requestDTO.getFirstName().equals("Son")){
16+
throw new ResourceNotFoundException("Son da ton tai");
17+
}
18+
return 0;
19+
}
20+
}

0 commit comments

Comments
 (0)