Java 控制器级别上的两个try-catch块

Java 控制器级别上的两个try-catch块,java,exception-handling,jersey,try-catch,throw,Java,Exception Handling,Jersey,Try Catch,Throw,我有一个案例,我需要在控制器级别上复制try..catch块。 让我提供此问题的示例代码: List<String> loadedList = engine.findSomething(param); //At this point we can obtain Exception, so this code block we should move to existing try block or create another.. but the problem if we move

我有一个案例,我需要在控制器级别上复制try..catch块。 让我提供此问题的示例代码:

List<String> loadedList =
engine.findSomething(param); //At this point we can obtain Exception, so this code block we should move to existing try block or create another.. but the problem if we move this to try block below, the WebApplicationException that throws in condition below will be catched in existing try block..

// if not found - return NOT_FOUND.
if (CollectionUtils.isEmpty(loadedList)) {
    log.info(errorMessage);
    throw new WebApplicationException(Response.status(Status.NOT_FOUND).entity(errorMessage).type("text/plain").build()); //exception from Jersey lib
}

try {
    for (String item : loadedList) {
        //some business logic
        //I know that on controller layer we should avoid business logic but it is not my code and I can not change it..
    }
    return Response.ok().build();

} catch (Exception e) {
    throw processException(e); //Helper method that avoids code duplication when preparing webException
}
列表加载列表=
引擎查找某些内容(参数)//此时我们可以获得异常,所以我们应该将此代码块移动到现有的try块或创建另一个。。但问题是,如果我们将其移动到下面的try块,则在下面的条件中抛出的WebApplicationException将在现有try块中捕获。。
//如果未找到-返回未找到。
if(CollectionUtils.isEmpty(loadedList)){
日志信息(错误消息);
抛出新的WebApplicationException(Response.status(status.NOT_FOUND).entity(errorMessage).type(“text/plain”).build();//来自Jersey库的异常
}
试一试{
用于(字符串项:loadedList){
//一些商业逻辑

//我知道在控制器层,我们应该避免业务逻辑,但这不是我的代码,我不能更改它。。 } 返回Response.ok().build(); }捕获(例外e){ 抛出processException(e);//在准备webException时避免代码重复的帮助器方法 }
如何重构此代码


谢谢

您只需为

List<String> loadedList = engine.findSomething(param);

您只需为

List<String> loadedList = engine.findSomething(param);

您可以受益于@ControllerAdvice并实现全局控制器异常处理程序

@ControllerAdvice
public class GlobalExceptionHandler {

@ExceptionHandler(Exception.class)
public String handleException(HttpServletRequest request, Exception ex){
    //handle error here
    return "error";
}

为了使GlobalExceptionHandler工作,必须从控制器中抛出异常。

您可以从@ControllerAdvice中获益并实现全局控制器异常处理程序

@ControllerAdvice
public class GlobalExceptionHandler {

@ExceptionHandler(Exception.class)
public String handleException(HttpServletRequest request, Exception ex){
    //handle error here
    return "error";
}
try 
{
    List<String> loadedList = engine.findSomething(param); 

    // if not found - return NOT_FOUND.
    if (CollectionUtils.isEmpty(loadedList)) {
       log.info(errorMessage);
       throw new WebApplicationException(Response.status(Status.NOT_FOUND).entity(errorMessage).type("text/plain").build()); //exception from Jersey lib
    }

    for (String item : loadedList) {
        //some business logic
        //I know that on controller layer we should avoid business logic but it is not my code and I can not change it..
    }
    return Response.ok().build();

}
//You can first catch WebApplicationException before the Exception and 
//redirect the throw to the parent class
catch(WebApplicationException we) 
{
    throw we;
}
catch(Exception e)
{
    throw processException(e);
}
为了使GlobalExceptionHandler正常工作,必须从控制器中抛出异常。

try
try 
{
    List<String> loadedList = engine.findSomething(param); 

    // if not found - return NOT_FOUND.
    if (CollectionUtils.isEmpty(loadedList)) {
       log.info(errorMessage);
       throw new WebApplicationException(Response.status(Status.NOT_FOUND).entity(errorMessage).type("text/plain").build()); //exception from Jersey lib
    }

    for (String item : loadedList) {
        //some business logic
        //I know that on controller layer we should avoid business logic but it is not my code and I can not change it..
    }
    return Response.ok().build();

}
//You can first catch WebApplicationException before the Exception and 
//redirect the throw to the parent class
catch(WebApplicationException we) 
{
    throw we;
}
catch(Exception e)
{
    throw processException(e);
}
{ List loadedList=engine.findSomething(参数); //如果未找到-返回未找到。 if(CollectionUtils.isEmpty(loadedList)){ 日志信息(错误消息); 抛出新的WebApplicationException(Response.status(status.NOT_FOUND).entity(errorMessage).type(“text/plain”).build();//来自Jersey库的异常 } 用于(字符串项:loadedList){ //一些商业逻辑
//我知道在控制器层,我们应该避免业务逻辑,但这不是我的代码,我不能更改它。。 } 返回Response.ok().build(); } //您可以先捕获WebApplicationException,然后再捕获异常和 //将抛出重定向到父类 捕获(WebApplicationException we) { 扔给我们; } 捕获(例外e) { 抛出进程异常(e); }
试试看
{
List loadedList=engine.findSomething(参数);
//如果未找到-返回未找到。
if(CollectionUtils.isEmpty(loadedList)){
日志信息(错误消息);
抛出新的WebApplicationException(Response.status(status.NOT_FOUND).entity(errorMessage).type(“text/plain”).build();//来自Jersey库的异常
}
用于(字符串项:loadedList){
//一些商业逻辑

//我知道在控制器层,我们应该避免业务逻辑,但这不是我的代码,我不能更改它。。 } 返回Response.ok().build(); } //您可以先捕获WebApplicationException,然后再捕获异常和 //将抛出重定向到父类 捕获(WebApplicationException we) { 扔给我们; } 捕获(例外e) { 抛出进程异常(e); }
另一种不同于davidxxx的方法

将代码移到
try..catch
子句中。如果它碰巧抛出了
WebApplicationException
,则捕获它,对其执行任何需要执行的操作,然后再次抛出它

try {

List<String> loadedList =
engine.findSomething(param); 

// if not found - return NOT_FOUND.
if (CollectionUtils.isEmpty(loadedList)) {
    log.info(errorMessage);
    throw new WebApplicationException(Response.status(Status.NOT_FOUND).entity(errorMessage).type("text/plain").build()); //exception from Jersey lib
}

    for (String item : loadedList) {
        //some business logic
        //I know that on controller layer we should avoid business logic but it is not my code and I can not change it..
    }
    return Response.ok().build();

} catch (WebApplicationException e1){
  //log the exception
  //throw it again using throw e1
} catch (Exception e2) {
    throw processException(e2); //Helper method that avoids code duplication when preparing webException
}
试试看{
列表加载列表=
引擎查找某些内容(参数);
//如果未找到-返回未找到。
if(CollectionUtils.isEmpty(loadedList)){
日志信息(错误消息);
抛出新的WebApplicationException(Response.status(status.NOT_FOUND).entity(errorMessage).type(“text/plain”).build();//来自Jersey库的异常
}
用于(字符串项:loadedList){
//一些商业逻辑

//我知道在控制器层,我们应该避免业务逻辑,但这不是我的代码,我不能更改它。。 } 返回Response.ok().build(); }捕获(WebApplicationException e1){ //记录异常 //使用抛出e1再次抛出它 }捕获(异常e2){ 抛出processException(e2);//在准备webException时避免代码重复的帮助器方法 }
另一种不同于davidxxx的方法

将代码移到
try..catch
子句中。如果它碰巧抛出了
WebApplicationException
,则捕获它,对其执行任何需要执行的操作,然后再次抛出它

try {

List<String> loadedList =
engine.findSomething(param); 

// if not found - return NOT_FOUND.
if (CollectionUtils.isEmpty(loadedList)) {
    log.info(errorMessage);
    throw new WebApplicationException(Response.status(Status.NOT_FOUND).entity(errorMessage).type("text/plain").build()); //exception from Jersey lib
}

    for (String item : loadedList) {
        //some business logic
        //I know that on controller layer we should avoid business logic but it is not my code and I can not change it..
    }
    return Response.ok().build();

} catch (WebApplicationException e1){
  //log the exception
  //throw it again using throw e1
} catch (Exception e2) {
    throw processException(e2); //Helper method that avoids code duplication when preparing webException
}
试试看{
列表加载列表=
引擎查找某些内容(参数);
//如果未找到-返回未找到。
if(CollectionUtils.isEmpty(loadedList)){
日志信息(错误消息);
抛出新的WebApplicationException(Response.status(status.NOT_FOUND).entity(errorMessage).type(“text/plain”).build();//来自Jersey库的异常
}
用于(字符串项:loadedList){
//一些商业逻辑

//我知道在控制器层,我们应该避免业务逻辑,但这不是我的代码,我不能更改它。。 } 返回Response.ok().build(); }捕获(WebApplicationException e1){ //记录异常 //使用抛出e1再次抛出它 }捕获(异常e2){ 抛出processException(e2);//在准备webException时避免代码重复的帮助器方法 }
这是一个Spring控制器吗?“但它不是我的代码,我无法更改它…”但您想重构它?是的,它是Spring控制器,我无法移动业务逻辑,因为它不在当前任务的范围内:)您可以捕获异常,检查它是否是WebApplicationException,然后再次抛出。这是一个Spring控制器吗?“但它不是我的代码,我无法更改它…”但您想重构它?是的,它是Spring控制器,我无法移动业务逻辑,因为它不在当前任务的范围内:)您可以捕获异常,检查它是否为WebApplicationException,以及