Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/355.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/14.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 正在引发状态异常_Java_Spring_Spring Mvc_Spring Boot - Fatal编程技术网

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
),并让控制器层(在您的例子中是网络控制器层)优雅地处理异常。

Status
404 NOT FOUND
基本上是一个HTTP状态代码,如果您想要这样的东西,但是没有
org.springframework.web
,那么您使用什么样的框架来处理servlet?
compile('org.springframework.boot:spring-boot-starter-web')