Jackson ClassCastException自定义对象映射器

时间:2015-12-09 11:03:37

标签: java json generics jersey jackson

我使用自定义对象映射器在Java对象中序列化/反序列化Json。 我还在Jax-RS应用程序接口(Jersey)上注册了自定义对象映射器。

public class AppConfiguration extends ResourceConfig {
public AppConfiguration() {
    /../
    register(ObjectMapperProvider.class);
    register(JacksonFeature.class);
    /../
}
}

@Provider
public class ObjectMapperProvider implements ContextResolver<ObjectMapper> {

private final ObjectMapper defaultObjectMapper;

public ObjectMapperProvider() {
    defaultObjectMapper = createDefaultMapper();
}

public ObjectMapper getContext(Class<?> type) {
    return defaultObjectMapper;
}

private static ObjectMapper createDefaultMapper() {
    final ObjectMapper result = new ObjectMapper();
    result.enable(SerializationFeature.INDENT_OUTPUT);
    result.registerModule(new Jdk8Module());
    result.registerModule(new JavaTimeModule());

    result.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
    return result;
}
}

当我将Json发送到Post WS方法时,Jackson无法转换为java对象并抛出

java.util.LinkedHashMap cannot be cast to fr.solaire.facturation.model.MatriceComplexite

当我使用普通ObjectMapper时,一切都按预期工作,但我不想在项目的任何地方复制此代码。

你有什么想法吗?

@POST
@Produces(APPLICATION_JSON) @Consumes(APPLICATION_JSON)
public MatriceComplexite saveEntity(MatriceComplexite entityJson) {
    return matriceComplexiteRepository.save(entityJson);
}

我有以下错误。

09-Dec-2015 11:46:32.369 SEVERE [557] org.apache.catalina.core.StandardWrapperValve.invoke Servlet.service() for servlet [SpringApplication] in context with path [/mediation-webapp] threw exception [java.lang.ClassCastException: java.util.LinkedHashMap cannot be cast to fr.solaire.facturation.model.MatriceComplexite] with root cause
 java.lang.ClassCastException: java.util.LinkedHashMap cannot be cast to fr.solaire.facturation.model.MatriceComplexite
    at fr.solaire.facturation.ws.impl.MatriceComplexiteServiceImpl.saveEntity(MatriceComplexiteServiceImpl.java:22)
    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.glassfish.jersey.server.model.internal.ResourceMethodInvocationHandlerFactory$1.invoke(ResourceMethodInvocationHandlerFactory.java:81)
    at org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher$1.run(AbstractJavaResourceMethodDispatcher.java:144)
    at org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher.invoke(AbstractJavaResourceMethodDispatcher.java:161)
    at org.glassfish.jersey.server.model.internal.JavaResourceMethodDispatcherProvider$TypeOutInvoker.doDispatch(JavaResourceMethodDispatcherProvider.java:205)
    at org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher.dispatch(AbstractJavaResourceMethodDispatcher.java:99)
    at org.glassfish.jersey.server.model.ResourceMethodInvoker.invoke(ResourceMethodInvoker.java:389)
    at org.glassfish.jersey.server.model.ResourceMethodInvoker.apply(ResourceMethodInvoker.java:347)
    at org.glassfish.jersey.server.model.ResourceMethodInvoker.apply(ResourceMethodInvoker.java:102)
    at org.glassfish.jersey.server.ServerRuntime$2.run(ServerRuntime.java:326)
    at org.glassfish.jersey.internal.Errors$1.call(Errors.java:271)
    at org.glassfish.jersey.internal.Errors$1.call(Errors.java:267)
    at org.glassfish.jersey.internal.Errors.process(Errors.java:315)
    at org.glassfish.jersey.internal.Errors.process(Errors.java:297)
    at org.glassfish.jersey.internal.Errors.process(Errors.java:267)
    at org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:317)
    at org.glassfish.jersey.server.ServerRuntime.process(ServerRuntime.java:305)
    at org.glassfish.jersey.server.ApplicationHandler.handle(ApplicationHandler.java:1154)
    at org.glassfish.jersey.servlet.WebComponent.serviceImpl(WebComponent.java:471)
    at org.glassfish.jersey.servlet.WebComponent.service(WebComponent.java:425)
    at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:383)
    at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:336)
    at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:223)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:291)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:217)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:142)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
    at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:616)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:518)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1091)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:673)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1500)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1456)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Unknown Source)

编辑: 我写了这个方法来确认序列化/反序列化是否运行良好。

@POST
@Path("/string")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
@ApiOperation(value = "Save an entity",
        notes = "Must provide an entity in Json Format",
        produces = MediaType.APPLICATION_JSON, consumes = MediaType.APPLICATION_JSON)
public String saveEntity(String entityJson) throws IOException {
    return new ObjectMapper().writeValueAsString(new ObjectMapper().readValue(entityJson, MatriceComplexite.class));
}

编辑2:我忘了删除ObjectMapperProvider上的@Produces和@Consumes。它只是在stackoverflow上测试其他主题。

编辑3:在我的pom.xml上,我有:

    <dependency>
        <groupId>org.glassfish.jersey.media</groupId>
        <artifactId>jersey-media-json-jackson</artifactId>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-core</artifactId>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.jaxrs</groupId>
        <artifactId>jackson-jaxrs-json-provider</artifactId>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.datatype</groupId>
        <artifactId>jackson-datatype-jsr310</artifactId>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.datatype</groupId>
        <artifactId>jackson-datatype-jdk8</artifactId>
        <version>2.6.3</version>
    </dependency>

编辑4:我设置了一个接口,我将saveEntity方法放在其中,并且它是通用的。为了简化这篇文章,我不发布这个界面,但它看起来像是主要问题。

此方法:

@POST
@Produces(APPLICATION_JSON) @Consumes(APPLICATION_JSON)
public MatriceComplexite saveEntity(MatriceComplexite entityJson) {
    return matriceComplexiteRepository.save(entityJson);
}

此方法继承自接口中定义的方法,我使用泛型。我没有把JAX RS注释放在实现的类上。 在界面中:

public interface GenericRepository<T> extends JpaRepository<T, ID extends Serializable>{
@POST
@Produces(APPLICATION_JSON) @Consumes(APPLICATION_JSON)
T saveEntity(T entityJson);
}

另一个界面我只有以下定义:

public interface MatriceComplexiteRepository extends GenericRepository<MatriceComplexite>{}

我在卡塔琳娜身上看到了这种警告:

警告1 saveEntity的参数T不是具体类

我将查看泽西岛的GenericType。

编辑5: 昨天,我登录了tomcat

WARNING: Parameter 1 of type T from public fr.solaire.facturation.model.MatriceComplexite fr.solaire.facturation.ws.impl.MatriceComplexiteServiceImpl.saveEntity(fr.solaire.facturation.model.MatriceComplexite) is not resolvable to a concrete type.
WARNING: Parameter 2 of type T from public fr.solaire.facturation.model.MatriceComplexite fr.solaire.facturation.ws.impl.MatriceComplexiteServiceImpl.updateEntity(java.lang.String,fr.solaire.facturation.model.MatriceComplexite) is not resolvable 

任何想法?

0 个答案:

没有答案