在我的 RestEasy(带有 Spring)应用程序中,我扩展了 RestEasyJacksonProvider(由 RestEasy 提供),以自定义 ObjectMapper 的配置,如下所示
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;
}
});
}
}
该应用程序具有读取 json 数据以及导入 .json 文件的方法的服务。
在读取 json 数据时,我在 @Consumes 中指定了 applicaion/json,JacksonProvider 负责将其解析为对象。我想在读取 .json 文件时使用相同的 JacksonProvider,以便在导入时也应用我的自定义映射器属性。像这样的东西:
Object dto = jacksonProvider.readFrom(Object.class, Object.class, null, MediaType.APPLICATION_JSON_TYPE , null , new FileInputStream("input.json"));
但是,当我这样做时,我得到一个异常,下面的堆栈跟踪
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
有没有办法使用 JacksonProvider 在 resteasy 上下文之外解析 json?