Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.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
启用全局Spring异常处理程序时忽略给定列表中的失败数据_Spring_Spring Mvc - Fatal编程技术网

启用全局Spring异常处理程序时忽略给定列表中的失败数据

启用全局Spring异常处理程序时忽略给定列表中的失败数据,spring,spring-mvc,Spring,Spring Mvc,考虑一个场景,我们必须运行一个包含10条消息的循环。在迭代过程中,如果第5条消息由于某些异常而失败,请求将路由到全局异常处理程序。因此,其余的消息不会被处理 在这种情况下,如何处理异常,以及跳过由于某些异常而失败的异常,并处理其余的消息 使用Spring的全局异常处理程序 我们的管制员 考虑一下在persistInteractionService中,我正在迭代一个包含10条消息的列表。但第5条消息引发了一些异常,控制转到全局异常处理程序。我无法运行其余消息(即,从6到10)。请考虑在您的服务方法

考虑一个场景,我们必须运行一个包含10条消息的循环。在迭代过程中,如果第5条消息由于某些异常而失败,请求将路由到全局异常处理程序。因此,其余的消息不会被处理

在这种情况下,如何处理异常,以及跳过由于某些异常而失败的异常,并处理其余的消息

使用Spring的全局异常处理程序

我们的管制员

考虑一下在persistInteractionService中,我正在迭代一个包含10条消息的列表。但第5条消息引发了一些异常,控制转到全局异常处理程序。我无法运行其余消息(即,从6到10)。请考虑在您的服务方法中收集这些异常

我将定义一个自定义异常,该异常能够收集for循环中的任何异常,然后仅当它收集了任何异常时才抛出它

示例实现可能如下所示:

IteractionMultiException.java

public类IteractionMultiException扩展异常{
private final Map exceptions=新HashMap();
public void addException(集成消息、异常){
异常。put(消息、异常);
}
公共布尔hasCatched(){
返回异常;
}
}
持续交互服务方法

公共交互响应到persistInteractionService(字符串interactionJSON,HttpServletRequest){ IteractionMultiException multiException=新的IteractionMultiException(); InteractionResponsedToResponse=null; Collection integrationMessages=getIntegrationMessages(interactionJSON); for(IntegrationMessage消息:integrationMessages){ 试一试{ //你的逻辑在这里 }捕获(例外e){ 多重异常。addException(消息,e); } } if(multiException.hasCatched()){ 抛出多重异常; } 返回响应; }
根据您服务的逻辑,此解决方案可能会有很大差异,但我希望这正是您想要的。

您能提供一些代码供参考吗?@noiaverbale提供了一些代码
@EnableWebMvc
@ControllerAdvice
public class ExceptionHandlerUtil
{

    public static final Logger LOGGER = Logger.getLogger( ExceptionHandlerUtil.class.getPackage().getName() );

    @ExceptionHandler( Exception.class )
    public @ResponseBody ExceptionDTO commonExceptionHandler( Exception e , HttpServletRequest request , HttpServletResponse response ) throws IOException, JSONException
        {
            LOGGER.log( Level.SEVERE , FullHistoryUtil.printException( e ) );
            ExceptionDTO exceptionDTO = new ExceptionDTO();
            exceptionDTO.setStatus( 500 );
            exceptionDTO.setMessage( e.toString() );
            response.setStatus( 500 );
            NotificationServiceImplementation notificationService = new NotificationServiceImplementation();
            notificationService.sendExceptionNotification( e , request , 86400 , true );
            return exceptionDTO;
        }
@RequestMapping( value = “/messages/bulk” , method = RequestMethod.POST , consumes = "application/json; charset=utf-8" )
    public @ResponseBody InteractionResponseDTO persistInteraction( @RequestBody String interactionJSON , HttpServletRequest request )  throws JSONException , IOException , IllegalAccessException , InvocationTargetException , UnprocessableEntityException
        {
            return interaction.persistInteractionService( interactionJSON , request );
        }