How to extract the ObjectMapper used by RestEasy's JacksonProvider to parse a .json file?

In my RestEasy (with Spring) app, I have extended RestEasyJacksonProvider (provided by RestEasy), to customize the configuration of the ObjectMapper a little bit like below

    public class JacksonProvider extends ResteasyJacksonProvider{

    public JacksonProvider(){
        super();
        ObjectMapper mapper = locateMapper(Object.class, MediaType.APPLICATION_JSON_TYPE);

        mapper.configure(SerializationConfig.Feature.WRITE_DATES_AS_TIMESTAMPS, false);

        mapper.getDeserializationConfig().addHandler(new DeserializationProblemHandler() {
            @Override
            public boolean handleUnknownProperty(DeserializationContext ctxt, JsonDeserializer<?> deserializer, Object beanOrClass, String propertyName) throws IOException, JsonProcessingException {
                logger.warn(String.format("Could not deserialize property with name '%s' on object of type '%s'", propertyName, beanOrClass.getClass().getName()));
                return true;
            }
        });

    }
}

The app has a service which with methods for reading json data as well as importing .json files.

While reading json data, I specify the applicaion/json in @Consumes and JacksonProvider takes care of parsing it to object. I want to use the same JacksonProvider while reading the .json file so that my custom mapper properties are applied while import also. Something like this:

Object dto = jacksonProvider.readFrom(Object.class, Object.class, null, MediaType.APPLICATION_JSON_TYPE , null , new FileInputStream("input.json"));

However, when I do this, I get an exception, stacktrace below

    JSONImportTest
com.test.JSONImportTest
unmarshal(com.test.JSONImportTest)
java.lang.ExceptionInInitializerError

    at com.test.JSONImportTest.unmarshal(JSONImportTest.java:19)

    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)

    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)

    at java.lang.reflect.Method.invoke(Unknown Source)

    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:45)

    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)

    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42)

    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)

    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263)

    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:68)

    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47)

    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)

    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)

    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)

    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)

    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)

    at org.junit.runners.ParentRunner.run(ParentRunner.java:300)

    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)

    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)

    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)

    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)

    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)

    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)

Caused by: java.lang.RuntimeException: java.lang.ClassNotFoundException: com.sun.ws.rs.ext.RuntimeDelegateImpl

    at javax.ws.rs.ext.RuntimeDelegate.findDelegate(RuntimeDelegate.java:134)

    at javax.ws.rs.ext.RuntimeDelegate.getInstance(RuntimeDelegate.java:96)

    at javax.ws.rs.core.MediaType.<clinit>(MediaType.java:46)

    ... 24 more

Caused by: java.lang.ClassNotFoundException: com.sun.ws.rs.ext.RuntimeDelegateImpl

    at java.net.URLClassLoader$1.run(Unknown Source)

    at java.net.URLClassLoader$1.run(Unknown Source)

    at java.security.AccessController.doPrivileged(Native Method)

    at java.net.URLClassLoader.findClass(Unknown Source)

    at java.lang.ClassLoader.loadClass(Unknown Source)

    at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)

    at java.lang.ClassLoader.loadClass(Unknown Source)

    at java.lang.Class.forName0(Native Method)

    at java.lang.Class.forName(Unknown Source)

    at javax.ws.rs.ext.FactoryFinder.newInstance(FactoryFinder.java:77)

    at javax.ws.rs.ext.FactoryFinder.find(FactoryFinder.java:201)

    at javax.ws.rs.ext.RuntimeDelegate.findDelegate(RuntimeDelegate.java:113)

    ... 26 more

Is there a way to use the JacksonProvider to parse json outside of the resteasy context?

Answers


Ok I answer myself. There are basically two options:

1/ Get the ObjectMapper from the custom provider using

ObjectMapper mapper = jacksonProvider.locateMapper(Object.class, MediaType.APPLICATION_JSON_TYPE)

and then use this mapper to read json

mapper.readValue(new File("src/test/resources/input.json"), Object.class);

2/ Create a custom ObjectMapper (like mentioned here) and use that mapper to read json from both JacksonProvider as well as while reading the .json file.

I went with the first option.


Need Your Help

TLB invlpg instruction has long latency

x86 virtual-memory tlb

So I'm working on this kernel module that does some page table manipulation and I noticed that flushing a TLB entry is slow. How slow you ask? Over 100 ns per call to invlpg! That's 280 cycles or m...

pyobjc installation failure: xcode-select: error: tool 'xcodebuild' requires Xcode

python xcode python-2.7 pip pyobjc

I need pyobcj in order to have pyttsx work. When I attempt to install pyobjc with pip, I get the following: