jax rs+json无法解析mediatype字符串错误

vhmi4jdf  于 2021-07-03  发布在  Java
关注(0)|答案(1)|浏览(397)

我正在用jax-rs学习json,我的jdk是javase1.8,所以我使用json libjar进行解析。
我编写了一个简单的测试get方法。

@GET
@Path("/all")
@Produces(MediaType.APPLICATION_JSON)
public List<String> getAllCustomers() {
    JSONArray jsonArray = JSONArray.fromObject( "['json','is','easy']" );  
    System.out.println( jsonArray );  
return jsonArray;
}

它有错误:

15:04:00,308 ERROR [org.jboss.msc.service.fail] (ServerService Thread Pool -- 68) MSC000001: Failed to start service jboss.undertow.deployment.default-server.default-host./RSexample: org.jboss.msc.service.StartException in service jboss.undertow.deployment.default-server.default-host./RSexample: java.lang.IllegalArgumentException: RESTEASY003340: Failure parsing MediaType string: 
    at org.wildfly.extension.undertow.deployment.UndertowDeploymentService$1.run(UndertowDeploymentService.java:84)
    at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source)
    at java.util.concurrent.FutureTask.run(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)
    at org.jboss.threads.JBossThread.run(JBossThread.java:320)
Caused by: java.lang.IllegalArgumentException: RESTEASY003340: Failure parsing MediaType string: 
    at org.jboss.resteasy.plugins.delegates.MediaTypeHeaderDelegate.parse(MediaTypeHeaderDelegate.java:115)
    at org.jboss.resteasy.plugins.delegates.MediaTypeHeaderDelegate.fromString(MediaTypeHeaderDelegate.java:20)
    at javax.ws.rs.core.MediaType.valueOf(MediaType.java:179)
    at org.jboss.resteasy.spi.metadata.ResourceBuilder$ResourceMethodBuilder.parseMediaTypes(ResourceBuilder.java:604)
    at org.jboss.resteasy.spi.metadata.ResourceBuilder$ResourceMethodBuilder.produces(ResourceBuilder.java:579)
    at org.jboss.resteasy.spi.metadata.ResourceBuilder.processMethod(ResourceBuilder.java:992)
    at org.jboss.resteasy.spi.metadata.ResourceBuilder.fromAnnotations(ResourceBuilder.java:780)
    at org.jboss.resteasy.spi.metadata.ResourceBuilder.rootResourceFromAnnotations(ResourceBuilder.java:730)
    at org.jboss.resteasy.plugins.server.resourcefactory.POJOResourceFactory.<init>(POJOResourceFactory.java:30)
    at org.jboss.resteasy.core.ResourceMethodRegistry.addPerRequestResource(ResourceMethodRegistry.java:79)
    at org.jboss.resteasy.spi.ResteasyDeployment.registration(ResteasyDeployment.java:445)
    at org.jboss.resteasy.spi.ResteasyDeployment.start(ResteasyDeployment.java:258)
    at org.jboss.resteasy.plugins.server.servlet.ServletContainerDispatcher.init(ServletContainerDispatcher.java:120)
    at org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher.init(HttpServletDispatcher.java:36)
    at io.undertow.servlet.core.LifecyleInterceptorInvocation.proceed(LifecyleInterceptorInvocation.java:117)
    at org.wildfly.extension.undertow.security.RunAsLifecycleInterceptor.init(RunAsLifecycleInterceptor.java:78)
    at io.undertow.servlet.core.LifecyleInterceptorInvocation.proceed(LifecyleInterceptorInvocation.java:103)
    at io.undertow.servlet.core.ManagedServlet$DefaultInstanceStrategy.start(ManagedServlet.java:250)
    at io.undertow.servlet.core.ManagedServlet.createServlet(ManagedServlet.java:133)
    at io.undertow.servlet.core.DeploymentManagerImpl$2.call(DeploymentManagerImpl.java:565)
    at io.undertow.servlet.core.DeploymentManagerImpl$2.call(DeploymentManagerImpl.java:536)
    at io.undertow.servlet.core.ServletRequestContextThreadSetupAction$1.call(ServletRequestContextThreadSetupAction.java:42)
    at io.undertow.servlet.core.ContextClassLoaderSetupAction$1.call(ContextClassLoaderSetupAction.java:43)
    at org.wildfly.extension.undertow.security.SecurityContextThreadSetupAction.lambda$create$0(SecurityContextThreadSetupAction.java:105)
    at org.wildfly.extension.undertow.deployment.UndertowDeploymentInfoService$UndertowThreadSetupAction.lambda$create$0(UndertowDeploymentInfoService.java:1508)
    at org.wildfly.extension.undertow.deployment.UndertowDeploymentInfoService$UndertowThreadSetupAction.lambda$create$0(UndertowDeploymentInfoService.java:1508)
    at org.wildfly.extension.undertow.deployment.UndertowDeploymentInfoService$UndertowThreadSetupAction.lambda$create$0(UndertowDeploymentInfoService.java:1508)
    at org.wildfly.extension.undertow.deployment.UndertowDeploymentInfoService$UndertowThreadSetupAction.lambda$create$0(UndertowDeploymentInfoService.java:1508)
    at io.undertow.servlet.core.DeploymentManagerImpl.start(DeploymentManagerImpl.java:578)
    at org.wildfly.extension.undertow.deployment.UndertowDeploymentService.startContext(UndertowDeploymentService.java:100)
    at org.wildfly.extension.undertow.deployment.UndertowDeploymentService$1.run(UndertowDeploymentService.java:81)
    ... 6 more

当我将返回类型改为jsonarray时,出现了下面的错误。

WFLYCTL0186:   Services which failed to start:      service jboss.undertow.deployment.default-server.default-host./RSexample: java.lang.NoClassDefFoundError: net/sf/json/JSONArray

我已经在构建路径中包含了jar。那还需要做什么呢?有什么帮助吗?谢谢。

jljoyd4f

jljoyd4f1#

首先,我认为您应该从rest端点返回javax.ws.rs.core.response,但您可以尝试以下方法:

@GET
    @Path("/all")
    @Produces(MediaType.APPLICATION_JSON)
    public List<String> getAllCustomers() {
        JSONArray jsonArray = new JSONArray("['json','is','easy']");
        List<String> list = new ArrayList<>();
        for(int i = 0; i < jsonArray.length(); i++){
            list.add(jsonArray.get(i).toString());
        }
        return list;
    }

相关问题