小编ehs*_*ini的帖子

在 JSON 错误响应中包含异常消息

如果电子邮件地址已存在,则抛出一条异常消息(“消息:”具有“+tempEmailId+”的用户已存在”)。当我在邮递员中测试时,我没有收到异常消息。你能帮我吗?在哪里问题?在此输入图像描述

控制器类:

@RestController
public class RegistrationController {

    @Autowired
    private RegistrationService service;
    
    @PostMapping("/registeruser")
    public  User registerUser(@RequestBody User user) throws Exception {
        
        String tempEmailId = user.getEmailId();
        if(tempEmailId !=null && !"".equals(tempEmailId)) {
            User userObject = service.fetchUserByEmailId(tempEmailId);
            if(userObject!=null) {
                throw new Exception("User with "+tempEmailId+" is already exist");
            }
        }
        User userObject = null;
        userObject = service.saveUser(user);
        return userObject;

    }
}
Run Code Online (Sandbox Code Playgroud)

存储库:

public interface RegistrationRepository extends JpaRepository<User, Integer> {

    public User findByEmailId(String emailId);  // Here we declare 
}  
Run Code Online (Sandbox Code Playgroud)

服务:

@Service

public class RegistrationService {

    @Autowired …
Run Code Online (Sandbox Code Playgroud)

java spring jpa spring-boot

3
推荐指数
1
解决办法
3235
查看次数

标签 统计

java ×1

jpa ×1

spring ×1

spring-boot ×1