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
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
add a comment |
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
Can you provide some code for reference?
– noiaverbale
Mar 7 at 7:59
@noiaverbale provided some code
– Ganesh
Mar 7 at 13:42
add a comment |
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
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
spring spring-mvc
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
add a comment |
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
add a comment |
1 Answer
1
active
oldest
votes
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.
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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.
add a comment |
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.
add a comment |
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.
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.
answered Mar 7 at 14:16
noiaverbalenoiaverbale
605518
605518
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
Can you provide some code for reference?
– noiaverbale
Mar 7 at 7:59
@noiaverbale provided some code
– Ganesh
Mar 7 at 13:42