Код: Выделить всё
public class EmailAlreadyExistsException extends RuntimeException {
public EmailAlreadyExistsException() {
}
public EmailAlreadyExistsException(String msg) {
super(msg);
}
}
Код: Выделить всё
import com.bitanbasak.microservices.exception.EmailAlreadyExistsException;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
@ControllerAdvice
public class GlobalExceptionHandler {
@ExceptionHandler(EmailAlreadyExistsException.class)
private ResponseEntity handleEmailAlreadyExistsException(EmailAlreadyExistsException e) {
return new ResponseEntity(e.getMessage(), HttpStatus.CONFLICT);
}
}
Код: Выделить всё
import java.util.Optional;
import java.util.List;
import com.bitanbasak.microservices.exception.EmailAlreadyExistsException;
import com.bitanbasak.microservices.exception.RequestedDataNotFoundException;
import org.modelmapper.ModelMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.stereotype.Service;
import com.bitanbasak.microservices.entity.User;
import com.bitanbasak.microservices.repository.UserRepository;
@Service
public class UserService {
@Autowired
private UserRepository userRepository;
@Autowired
private ModelMapper modelMapper;
public User getUserById(Integer id) {
Optional user = this.userRepository.findById(id);
if (!user.isPresent()) {
throw new RequestedDataNotFoundException("User with id " + id + " not found");
}
User userResponse = this.modelMapper.map(user, User.class);
return userResponse;
}
public List getAllUsers() {
List users = this.userRepository.findAll();
return users;
}
public User createUser(User user) {
Optional existingEmailUser = this.userRepository.findByEmail(user.getEmail());
User newuser = null;
try {
newuser = this.userRepository.save(user);
} catch (DataIntegrityViolationException e) {
throw new EmailAlreadyExistsException("User with email " + user.getEmail() + " already exists");
}
return newuser;
}
}

Но когда я тестируя ту же конечную точку из моего углового интерфейса, полученный ответ не содержит идентификатора электронной почты, добавленного к строке ошибки, как вы можете видеть ниже:

Я не смог найти в Интернете какой-либо ресурс, который мог бы помочь мне разобраться в этой путанице. Может ли кто-нибудь дать мне некоторые разъяснения о том, почему это происходит, и, возможно, предоставить мне какой-нибудь ресурс, который я мог бы прочитать?
Подробнее здесь: https://stackoverflow.com/questions/790 ... ception-me