Java 正在引发状态异常
我有一个Java 正在引发状态异常,java,spring,spring-mvc,spring-boot,Java,Spring,Spring Mvc,Spring Boot,我有一个核心模块,它有这样的关系 compile('org.springframework.boot:spring-boot-starter-data-jpa') compile('org.springframework.boot:spring-boot-starter-mail') 在serviceinmethod中,如果找不到用户,我想从状态代码(例如,notfound404)抛出一个异常(或其他) getById(Long id) { //if the user with sp
核心
模块,它有这样的关系
compile('org.springframework.boot:spring-boot-starter-data-jpa')
compile('org.springframework.boot:spring-boot-starter-mail')
在serviceinmethod中,如果找不到用户,我想从状态代码(例如,notfound404)抛出一个异常(或其他)
getById(Long id) {
//if the user with specified id does not exist
//for example, cast an exception throw new UserNotFoundException(new List<FieldError>); with a list of error fields
}
并且不能使用诸如ResponseEntity
或HttpStatus
之类的对象
我想在这里实现这样的结果,但是没有org.springframework.web
可以建议我如何抛出一个异常,并将status和error list对象作为响应?让我们练习一下好的老方法 服务层不关心HTTP状态。从服务层抛出某种类型的
HttpStatus404Exception
不是一个好选择。如果有人想在直接连接到数据库的批处理程序中使用服务层,该怎么办?在这种情况下,HttpStatus404Exception
完全不合适
抛出一个
UserNotFoundException
(它可以扩展NotFoundException
),并让控制器层(在您的例子中是网络控制器层)优雅地处理异常。Status404 NOT FOUND
基本上是一个HTTP状态代码,如果您想要这样的东西,但是没有org.springframework.web
,那么您使用什么样的框架来处理servlet?
compile('org.springframework.boot:spring-boot-starter-web')