Ignore Failed Data From The Given List When Global Spring Exception Handler is Enabled2019 Community Moderator ElectionReading httprequest content from spring exception handlerException handler in Spring MVCHow to POST JSON data with Curl from Terminal/Commandline to Test Spring REST?Spring MVC: global exception handlerHow to write a proper global error handler with Spring MVC / Spring BootSpring global data bindingSpring Boot REST service exception handlingSpring @Controller exception handler and global exception handler. How to invoke bothSpring Global Exception Handler - Not MVCSpring: ResponseBodyEmitter.CompleteWithError() and global exception handler

Is it true that real estate prices mainly go up?

A three room house but a three headED dog

Should I tell my boss the work he did was worthless

Is Gradient Descent central to every optimizer?

Does splitting a potentially monolithic application into several smaller ones help prevent bugs?

How do I deal with a powergamer in a game full of beginners in a school club?

Why doesn't this Google Translate ad use the word "Translation" instead of "Translate"?

Why would one plane in this picture not have gear down yet?

Word for a person who has no opinion about whether god exists

Does a Catoblepas statblock appear in an official D&D 5e product?

They call me Inspector Morse

Should QA ask requirements to developers?

What wound would be of little consequence to a biped but terrible for a quadruped?

If the Captain's screens are out, does he switch seats with the co-pilot?

Offered promotion but I'm leaving. Should I tell?

How could our ancestors have domesticated a solitary predator?

Accountant/ lawyer will not return my call

Why does Captain Marvel assume the planet where she lands would recognize her credentials?

Is having access to past exams cheating and, if yes, could it be proven just by a good grade?

Why does the negative sign arise in this thermodynamic relation?

Could a cubesat be propelled to the moon?

Placing subfig vertically

What Happens when Passenger Refuses to Fly Boeing 737 Max?

What to do when during a meeting client people start to fight (even physically) with each others?



Ignore Failed Data From The Given List When Global Spring Exception Handler is Enabled



2019 Community Moderator ElectionReading httprequest content from spring exception handlerException handler in Spring MVCHow to POST JSON data with Curl from Terminal/Commandline to Test Spring REST?Spring MVC: global exception handlerHow to write a proper global error handler with Spring MVC / Spring BootSpring global data bindingSpring Boot REST service exception handlingSpring @Controller exception handler and global exception handler. How to invoke bothSpring Global Exception Handler - Not MVCSpring: ResponseBodyEmitter.CompleteWithError() and global exception handler










0















Consider a scenario where we have to run a loop with 10 messages. During the iteration if the 5th message fails because of some exception, request routed to Global Exception Handler. Because of this the rest of the messages are not processed.



How to handle the exception is this scenario, as well as skip the ones which are failing because of some exception and process rest of the messages.




Global Exception Handler Using Spring




@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;




Our Controller




@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 );



Consider inside persistInteractionService I am iterating a list which has 10 messages. But 5th message threw some exception, control goes to global exception handler. I am not able to run the rest of the messages (i.e.., from 6 to 10 ).










share|improve this question
























  • Can you provide some code for reference?

    – noiaverbale
    Mar 7 at 7:59











  • @noiaverbale provided some code

    – Ganesh
    Mar 7 at 13:42















0















Consider a scenario where we have to run a loop with 10 messages. During the iteration if the 5th message fails because of some exception, request routed to Global Exception Handler. Because of this the rest of the messages are not processed.



How to handle the exception is this scenario, as well as skip the ones which are failing because of some exception and process rest of the messages.




Global Exception Handler Using Spring




@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;




Our Controller




@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 );



Consider inside persistInteractionService I am iterating a list which has 10 messages. But 5th message threw some exception, control goes to global exception handler. I am not able to run the rest of the messages (i.e.., from 6 to 10 ).










share|improve this question
























  • Can you provide some code for reference?

    – noiaverbale
    Mar 7 at 7:59











  • @noiaverbale provided some code

    – Ganesh
    Mar 7 at 13:42













0












0








0








Consider a scenario where we have to run a loop with 10 messages. During the iteration if the 5th message fails because of some exception, request routed to Global Exception Handler. Because of this the rest of the messages are not processed.



How to handle the exception is this scenario, as well as skip the ones which are failing because of some exception and process rest of the messages.




Global Exception Handler Using Spring




@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;




Our Controller




@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 );



Consider inside persistInteractionService I am iterating a list which has 10 messages. But 5th message threw some exception, control goes to global exception handler. I am not able to run the rest of the messages (i.e.., from 6 to 10 ).










share|improve this question
















Consider a scenario where we have to run a loop with 10 messages. During the iteration if the 5th message fails because of some exception, request routed to Global Exception Handler. Because of this the rest of the messages are not processed.



How to handle the exception is this scenario, as well as skip the ones which are failing because of some exception and process rest of the messages.




Global Exception Handler Using Spring




@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;




Our Controller




@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 );



Consider inside persistInteractionService I am iterating a list which has 10 messages. But 5th message threw some exception, control goes to global exception handler. I am not able to run the rest of the messages (i.e.., from 6 to 10 ).







spring spring-mvc






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 7 at 13:41







Ganesh

















asked Mar 7 at 7:53









GaneshGanesh

1716




1716












  • Can you provide some code for reference?

    – noiaverbale
    Mar 7 at 7:59











  • @noiaverbale provided some code

    – Ganesh
    Mar 7 at 13:42

















  • Can you provide some code for reference?

    – noiaverbale
    Mar 7 at 7:59











  • @noiaverbale provided some code

    – Ganesh
    Mar 7 at 13:42
















Can you provide some code for reference?

– noiaverbale
Mar 7 at 7:59





Can you provide some code for reference?

– noiaverbale
Mar 7 at 7:59













@noiaverbale provided some code

– Ganesh
Mar 7 at 13:42





@noiaverbale provided some code

– Ganesh
Mar 7 at 13:42












1 Answer
1






active

oldest

votes


















0














Consider to collect those exception in your service method.



I would define a custom exception able to collect any exception inside your for loop and then throw it only if it has collected any.



A sample implementation could be somthing as follow:



IteractionMultiException.java



public class IteractionMultiException extends Exception 
private final Map<IntegrationMessage, Exception> exceptions = new HashMap<>();

public void addException(IntegrationMessage message, Exception exception)
exceptions.put(message, exception);


public boolean hasCatched()
return exceptions.isEmpty();




persistInteractionService method



public InteractionResponseDTO persistInteractionService(String interactionJSON , HttpServletRequest request) 
IteractionMultiException multiException = new IteractionMultiException();
InteractionResponseDTO response = null;

Collection<IntegrationMessage> integrationMessages = getIntegrationMessages(interactionJSON);
for (IntegrationMessage message : integrationMessages)
try
// your logic here
catch(Exception e)
multiException.addException(message, e);



if (multiException.hasCatched())
throw multiException;


return response;



This solution may vary a lot depending on the logic of your service, but I hope it's what you're looking for.






share|improve this answer






















    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55038656%2fignore-failed-data-from-the-given-list-when-global-spring-exception-handler-is-e%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    Consider to collect those exception in your service method.



    I would define a custom exception able to collect any exception inside your for loop and then throw it only if it has collected any.



    A sample implementation could be somthing as follow:



    IteractionMultiException.java



    public class IteractionMultiException extends Exception 
    private final Map<IntegrationMessage, Exception> exceptions = new HashMap<>();

    public void addException(IntegrationMessage message, Exception exception)
    exceptions.put(message, exception);


    public boolean hasCatched()
    return exceptions.isEmpty();




    persistInteractionService method



    public InteractionResponseDTO persistInteractionService(String interactionJSON , HttpServletRequest request) 
    IteractionMultiException multiException = new IteractionMultiException();
    InteractionResponseDTO response = null;

    Collection<IntegrationMessage> integrationMessages = getIntegrationMessages(interactionJSON);
    for (IntegrationMessage message : integrationMessages)
    try
    // your logic here
    catch(Exception e)
    multiException.addException(message, e);



    if (multiException.hasCatched())
    throw multiException;


    return response;



    This solution may vary a lot depending on the logic of your service, but I hope it's what you're looking for.






    share|improve this answer



























      0














      Consider to collect those exception in your service method.



      I would define a custom exception able to collect any exception inside your for loop and then throw it only if it has collected any.



      A sample implementation could be somthing as follow:



      IteractionMultiException.java



      public class IteractionMultiException extends Exception 
      private final Map<IntegrationMessage, Exception> exceptions = new HashMap<>();

      public void addException(IntegrationMessage message, Exception exception)
      exceptions.put(message, exception);


      public boolean hasCatched()
      return exceptions.isEmpty();




      persistInteractionService method



      public InteractionResponseDTO persistInteractionService(String interactionJSON , HttpServletRequest request) 
      IteractionMultiException multiException = new IteractionMultiException();
      InteractionResponseDTO response = null;

      Collection<IntegrationMessage> integrationMessages = getIntegrationMessages(interactionJSON);
      for (IntegrationMessage message : integrationMessages)
      try
      // your logic here
      catch(Exception e)
      multiException.addException(message, e);



      if (multiException.hasCatched())
      throw multiException;


      return response;



      This solution may vary a lot depending on the logic of your service, but I hope it's what you're looking for.






      share|improve this answer

























        0












        0








        0







        Consider to collect those exception in your service method.



        I would define a custom exception able to collect any exception inside your for loop and then throw it only if it has collected any.



        A sample implementation could be somthing as follow:



        IteractionMultiException.java



        public class IteractionMultiException extends Exception 
        private final Map<IntegrationMessage, Exception> exceptions = new HashMap<>();

        public void addException(IntegrationMessage message, Exception exception)
        exceptions.put(message, exception);


        public boolean hasCatched()
        return exceptions.isEmpty();




        persistInteractionService method



        public InteractionResponseDTO persistInteractionService(String interactionJSON , HttpServletRequest request) 
        IteractionMultiException multiException = new IteractionMultiException();
        InteractionResponseDTO response = null;

        Collection<IntegrationMessage> integrationMessages = getIntegrationMessages(interactionJSON);
        for (IntegrationMessage message : integrationMessages)
        try
        // your logic here
        catch(Exception e)
        multiException.addException(message, e);



        if (multiException.hasCatched())
        throw multiException;


        return response;



        This solution may vary a lot depending on the logic of your service, but I hope it's what you're looking for.






        share|improve this answer













        Consider to collect those exception in your service method.



        I would define a custom exception able to collect any exception inside your for loop and then throw it only if it has collected any.



        A sample implementation could be somthing as follow:



        IteractionMultiException.java



        public class IteractionMultiException extends Exception 
        private final Map<IntegrationMessage, Exception> exceptions = new HashMap<>();

        public void addException(IntegrationMessage message, Exception exception)
        exceptions.put(message, exception);


        public boolean hasCatched()
        return exceptions.isEmpty();




        persistInteractionService method



        public InteractionResponseDTO persistInteractionService(String interactionJSON , HttpServletRequest request) 
        IteractionMultiException multiException = new IteractionMultiException();
        InteractionResponseDTO response = null;

        Collection<IntegrationMessage> integrationMessages = getIntegrationMessages(interactionJSON);
        for (IntegrationMessage message : integrationMessages)
        try
        // your logic here
        catch(Exception e)
        multiException.addException(message, e);



        if (multiException.hasCatched())
        throw multiException;


        return response;



        This solution may vary a lot depending on the logic of your service, but I hope it's what you're looking for.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Mar 7 at 14:16









        noiaverbalenoiaverbale

        605518




        605518





























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55038656%2fignore-failed-data-from-the-given-list-when-global-spring-exception-handler-is-e%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Identity Server 4 is not redirecting to Angular app after login2019 Community Moderator ElectionIdentity Server 4 and dockerIdentityserver implicit flow unauthorized_clientIdentityServer Hybrid Flow - Access Token is null after user successful loginIdentity Server to MVC client : Page Redirect After loginLogin with Steam OpenId(oidc-client-js)Identity Server 4+.NET Core 2.0 + IdentityIdentityServer4 post-login redirect not working in Edge browserCall to IdentityServer4 generates System.NullReferenceException: Object reference not set to an instance of an objectIdentityServer4 without HTTPS not workingHow to get Authorization code from identity server without login form

            2005 Ahvaz unrest Contents Background Causes Casualties Aftermath See also References Navigation menue"At Least 10 Are Killed by Bombs in Iran""Iran"Archived"Arab-Iranians in Iran to make April 15 'Day of Fury'"State of Mind, State of Order: Reactions to Ethnic Unrest in the Islamic Republic of Iran.10.1111/j.1754-9469.2008.00028.x"Iran hangs Arab separatists"Iran Overview from ArchivedConstitution of the Islamic Republic of Iran"Tehran puzzled by forged 'riots' letter""Iran and its minorities: Down in the second class""Iran: Handling Of Ahvaz Unrest Could End With Televised Confessions""Bombings Rock Iran Ahead of Election""Five die in Iran ethnic clashes""Iran: Need for restraint as anniversary of unrest in Khuzestan approaches"Archived"Iranian Sunni protesters killed in clashes with security forces"Archived

            Can't initialize raids on a new ASUS Prime B360M-A motherboard2019 Community Moderator ElectionSimilar to RAID config yet more like mirroring solution?Can't get motherboard serial numberWhy does the BIOS entry point start with a WBINVD instruction?UEFI performance Asus Maximus V Extreme