当前位置: 代码迷 >> java >> resteasy @Consume json失败,出现java.util.Map
  详细解决方案

resteasy @Consume json失败,出现java.util.Map

热度:56   发布时间:2023-07-27 09:37:07.0

我有以下json结构:

{
    "foo" : {
        "foo1" : {
            "txt" : "val",
            "txt1" : "val1",
            "txt2" : "val2",
        },
        "foo2" : {
            "txt" : "val",
            "txt1" : "val1",
            "txt2" : "val2",
        }
    },
    "bar" : {
        "bar1": {
            "txt" : "val",
            "txt1" : "val1",
            "txt2" : "val2",
        },
        "bar2": {
            "txt" : "val",
            "txt1" : "val1",
            "txt2" : "val2",
        }
    }
}

虽然我有以下pojo:

class Pojo {
  String txt;
  String txt1;
  String txt2;
}

我的resteasy方法如下所示:

@POST
@Produces({ MediaType.APPLICATION_JSON })
@Consumes({ MediaType.APPLICATION_JSON })
@Path("/foo")
public void bar(@QueryParam("jsonObj") Map<String,Map<String,Pojo>> jsonObj);

但是,在启动服务器时出现以下错误:

java.lang.RuntimeException:无法找到在公共抽象void com.foo.FooBar.bar上使用javax.ws.rs.QueryParam(“ jsonObj”)的String参数或valueOf()或fromString()方法的构造函数(java.util.Map)为基本类型: org.jboss.resteasy.core.StringParameterInjector.initialize(StringParameterInjector.java:217)上的java.util.Map? [resteasy-jaxrs-3.0.9.Final.jar:na]在org.jboss.resteasy.core.QueryParamInjector。(QueryParamInjector.java:(org.jboss.resteasy.core.StringParameterInjector。(StringParameterInjector.java:61)?[resteasy-jaxrs-3.0.9.Final.jar:na] 28)?[resteasy-jaxrs-3.0.9.Final.jar:na]在org.jboss.resteasy.core.InjectorFactoryImpl.createParameterExtractor(InjectorFactoryImpl.java:85)?[resteasy-jaxrs-3.0.9.Final.jar :na]在org.jboss.resteasy.core.InjectorFactoryImpl.createMethodInjector((MethodInjectorImpl.java:42)?[resteasy-jaxrs-3.0.9.Final.jar:na]在org.jboss.resteasy.core.InjectorFactoryImpl.createMethodInjector( InjectorFactoryImpl.java:76)?[resteasy-jaxrs-3.0.9.Final.jar:na]在o rg.jboss.resteasy.core.ResourceMethodInvoker。(ResourceMethodInvoker.java:100)?[resteasy-jaxrs-3.0.9.Final.jar:na]位于org.jboss.resteasy.core.ResourceMethodRegistry.processMethod(ResourceMethodRegistry.java: 280)?[resteasy-jaxrs-3.0.9.Final.jar:na]在org.jboss.resteasy.core.ResourceMethodRegistry.register(ResourceMethodRegistry.java:251)?[resteasy-jaxrs-3.0.9.Final.jar :na]在org.jboss.resteasy.core.ResourceMethodRegistry.addResourceFactory(ResourceMethodRegistry.java:221)?[resteasy-jaxrs-3.0.9.Final.jar:na]在org.jboss.resteasy.core.ResourceMethodRegistry.addResourceFactory (ResourceMethodRegistry.java:193)?[resteasy-jaxrs-3.0.9.Final.jar:na]在org.jboss.resteasy.core.ResourceMethodRegistry.addResourceFactory(ResourceMethodRegistry.java:179)?[resteasy-jaxrs-3.0。 9.org.jboss.resteasy.plugins.spring.SpringBeanProcessor.onApplicationEvent(SpringBeanProcessor.java:486)的[Final.jar:na]?org.springframework的[resteasy-spring-3.0.9.Final.jar:na]。 context.event.SimpleApplicationEventMu lticaster.multicastEvent(SimpleApplicationEventMulticaster.java:96)?[spring-context-3.2.8.RELEASE.jar:3.2.8.RELEASE]在org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:334)? [spring-context-3.2.8.RELEASE.jar:3.2.8.RELEASE]在org.springframework.context.support.AbstractApplicationContext.finishRefresh(AbstractApplicationContext.java:948)?[spring-context-3.2.8.RELEASE。 jar:3.2.8.RELEASE]在org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:482)?[spring-context-3.2.8.RELEASE.jar:3.2.8.RELEASE]在org。 springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:410)?[spring-web-3.2.8.RELEASE.jar:3.2.8.RELEASE] at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader .java:306)?[spring-web-3.2.8.RELEASE.jar:3.2.8.RELEASE]在org.springframework.web.context.ContextLoaderListener.contextInitialized(Context LoaderListener.java:112)[spring-web-3.2.8.RELEASE.jar:3.2.8.RELEASE]位于org.jboss.resteasy.plugins.spring.SpringContextLoaderListener.contextInitialized(SpringContextLoaderListener.java:48)[resteasy-spring -3.0.9.Final.jar:na]位于org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4939)[catalina.jar:7.0.47]位于org.apache.catalina.core.StandardContext。在org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)上的startInternal(StandardContext.java:5434)[catalina.jar:7.0.47]在org.apache上的[catalina.jar:7.0.47]。 catalina.core.ContainerBase $ StartChild.call(ContainerBase.java:1559)[catalina.jar:7.0.47] at org.apache.catalina.core.ContainerBase $ StartChild.call(ContainerBase.java:1549)[catalina.jar :7.0.47],位于java.util.concurrent.FutureTask.run(FutureTask.java:262)[na:1.7.0-45],位于java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)[na :1.7.0-45],位于java.util.concurrent.ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor.java:615) [na:1.7.0-45]位于java.lang.Thread.run(Thread.java:744)[na:1.7.0-45]

我遍历了并为复杂对象说它使用StringConvertor,但不应该为诸如List,Map等的简单类型提供支持...

可以使用InputStream而不是采用Map> jsonObj格式的JSON,然后可以处理输入流以从中获取对象。

  相关解决方案