Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/351.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在JSON错误响应中包含异常消息_Java_Spring_Spring Boot_Jpa - Fatal编程技术网

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

Java 在JSON错误响应中包含异常消息,java,spring,spring-boot,jpa,Java,Spring,Spring Boot,Jpa,如果电子邮件地址已存在,则抛出一条异常消息(“消息:“具有“+tempEmailId+”的用户已存在”)。我在邮递员中测试时未收到异常消息。您能帮助我吗?问题在哪里 控制器类: @RestController public class RegistrationController { @Autowired private RegistrationService service; @PostMapping("/registeruser")

如果电子邮件地址已存在,则抛出一条异常消息(“消息:“具有“+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;

    }
}
存储库:

public interface RegistrationRepository extends JpaRepository<User, Integer> {

    public User findByEmailId(String emailId);  // Here we declare 
}  

如果您使用的是Spring Boot 2.3或更高版本,则必须将属性
server.error.include message
设置为始终

引自:

对默认错误页面内容的更改

默认情况下,错误消息和任何绑定错误不再包含在默认错误页中。这降低了向客户端泄漏信息的风险。
server.error.include message
server.error.include binding errors
可分别用于控制消息和绑定错误的包含。支持ted值为
始终
在参数上
,以及
从不


如果您使用的是Spring Boot 2.3或更高版本,则必须将属性
server.error.include message
设置为始终

引自:

对默认错误页面内容的更改

默认情况下,错误消息和任何绑定错误不再包含在默认错误页中。这降低了向客户端泄漏信息的风险。
server.error.include message
server.error.include binding errors
可分别用于控制消息和绑定错误的包含。支持ted值为
始终
在参数上
,以及
从不


您可以将控制器响应包装为
ResponseEntity

@PostMapping("/registeruser")
public  ResponseEntity<Object> registerUser(@RequestBody User user) throws Exception {
        String tempEmailId = user.getEmailId();
        if(tempEmailId != null && !tempEmailId.isEmpty()) {
            User userObject = service.fetchUserByEmailId(tempEmailId);
            if(userObject!=null) {
                return new ResponseEntity<>("User with "+tempEmailId+" is already exist", HttpStatus.BAD_REQUEST);
            }
        }
        return new ResponseEntity<>(service.saveUser(user), HttpStatus.OK);
}

您可以返回任何可序列化为JSON的数据结构,而不是字符串。

您可以将控制器响应包装为
ResponseEntity

@PostMapping("/registeruser")
public  ResponseEntity<Object> registerUser(@RequestBody User user) throws Exception {
        String tempEmailId = user.getEmailId();
        if(tempEmailId != null && !tempEmailId.isEmpty()) {
            User userObject = service.fetchUserByEmailId(tempEmailId);
            if(userObject!=null) {
                return new ResponseEntity<>("User with "+tempEmailId+" is already exist", HttpStatus.BAD_REQUEST);
            }
        }
        return new ResponseEntity<>(service.saveUser(user), HttpStatus.OK);
}

您可以返回任何可序列化为JSON的数据结构,而不是字符串。

请从日志中发布stacktrace。此处给出的正确HTTP响应代码为400(错误请求)当您试图创建一个已存在电子邮件的用户时。因此,此代码应为您提供此情况所需的响应:
抛出新的BadRequestException().setErrorMessage(“已存在“+tempEmailId+”的用户”);
它不起作用。在日志中,我可以获得此消息(java.lang.Exception:使用eha的用户。hoo@gmail.com已存在)。我希望在postman中以JSON格式发送相同的消息。我想如果您将注册业务逻辑从controller提取到
RegistrationService
,并将
!“”.equals(tempaEmailId)
替换为
!tempaEmailId.isEmpty()
或者您可以将javax.validation.constraints包中的验证注释添加到class
User
中。这不会解决您的问题,但会使代码更干净。请从日志中发布stacktrace。此处给出的正确HTTP响应代码为400(错误请求)当您试图创建一个已存在电子邮件的用户时。因此,此代码应为您提供此情况所需的响应:
抛出新的BadRequestException().setErrorMessage(“已存在“+tempEmailId+”的用户”);
它不起作用。在日志中,我可以获得此消息(java.lang.Exception:使用eha的用户。hoo@gmail.com已存在)。我希望在postman中以JSON格式发送相同的消息。我想如果您将注册业务逻辑从controller提取到
RegistrationService
,并将
!“”.equals(tempaEmailId)
替换为
!tempaEmailId.isEmpty()
或者您可以将javax.validation.constraints包中的验证注释添加到class
User
。这不会解决您的问题,但会使代码更干净。我添加了server.error.include message=始终在application.properties文件中。现在它正在工作。我添加了server.error.include message=始终在application.properties文件中。现在我t在工作
@RestControllerAdvice
public class RegistrationControllerAdvice {

    @ExceptionHandler({UserAlreadyExistException.class})
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    public String userAlreadyExist(UserAlreadyExistException ex, WebRequest req) {
        return ex.getMessage();
    }
}