Com.ctc.stax.WstxOutputFactory cannot be cast

Hi, I am trying to use -

final XMLOutputFactory2 xmlOutputFactory = (XMLOutputFactory2) XMLOutputFactory2.newInstance();//XMLOutputFactory.newInstance();
final XMLStreamWriter2 xmlStreamWriter = (XMLStreamWriter2) xmlOutputFactory.createXMLStreamWriter(new FileWriter(file));

Error I am getting -

Caused by: java.lang.ClassCastException: com.ctc.wstx.stax.WstxOutputFactory cannot be cast to

on line 1.

I have gone through -

did not really understand any of them or what I am supposed to change.

Specific condition when I am getting this error -
When I am running my code in Http thread(i.e) the main thread code works fine, but when I am using a worker thread this error is thrown.

My pom.xml -


As I have told earlier that I only get error when I run this code asynchronously (i.e worker thread) and it was working fine on main thread. I worked around the code using class loader from main thread. I am not sure if that is a good thing or not. Can anyone tell me if the workaround is not messing something else.

        final ClassLoader original = Thread.currentThread().getContextClassLoader();
        final XMLOutputFactory2 xmlOutputFactory = (XMLOutputFactory2) XMLOutputFactory.newInstance();