java consumes_@Consumes不适用于JSON

大家好,我是知秋君,一个会写博客吟诗的知秋码农。今天说一说java consumes_@Consumes不适用于JSON,希望能够帮助大家进步!!! 我有以下休息服务: @Path("/add") @POST @Consumes(MediaType.APPLICATION_JSON) public Response add(SomeEntity entity) { entity =

大家好,我是知秋君,一个会写博客吟诗的知秋码农。今天说一说java consumes_@Consumes不适用于JSON,希望能够帮助大家进步!!!

我有以下休息服务:

@Path("/add")

@POST

@Consumes(MediaType.APPLICATION_JSON)

public Response add(SomeEntity entity) {

entity = someService.addEntity(entity);

return Response.ok("Entity added with id=" + entity.getId()).build();

}

将我的应用程序部署到Wildfly后,我使用json发送HTTP请求: {"id":0,"param1":value1,"param2":value2} 并收到以下错误消息:

Bad arguments passed to org.jboss.resteasy.spi.metadata.ResourceMethod@4ab32ab7 ( java.util.LinkedHashMap {id=0, param1=value1, param2=value2} ) ,状态码为500 .

我该怎么办?如何使我的应用程序从json而不是 LinkedHashMap 消耗实体?

更新:实体类代码不包含任何有趣的内容,只有 long id 和两个带有一些jpa注释的 String 字段 .

UPDATE2:完整的堆栈跟踪:

WARN [org.jboss.resteasy.core.ExceptionHandler](默认任务-2)执行POST / rest / add失败:org.jboss.resteasy.spi.InternalServerErrorException:传递给org.jboss.resteasy.spi.metadata的错误参数.ResourceMethod @ 4ab32ab7(java.util.LinkedHashMap {id = 0,param1 = value1,param2 = value2})org.jboss.resteasy.core.MethodInjectorImpl.invoke(MethodInjectorImpl.java:177)[resteasy-jaxrs-3.0 . 8.Final.jar:] org.jboss.resteasy.core.ResourceMethodInvoker.invokeOnTarget(ResourceMethodInvoker.java:296)[resteasy-jaxrs-3.0.8.Final.jar:] org.jboss.resteasy.core.ResourceMethodInvoker .invoke(ResourceMethodInvoker.java:250)[resteasy-jaxrs-3.0.8.Final.jar:] org.jboss.resteasy.core.ResourceMethodInvoker.invoke(ResourceMethodInvoker.java:237)[resteasy-jaxrs-3.0.8 .Final.jar:] org.jboss.resteasy.core.SynchronousDispatcher.invoke(SynchronousDispatcher.java:356)[resteasy-jaxrs-3.0.8.Final.jar:] at org.jboss.resteasy.core.SynchronousDispatcher . invoke(SynchronousDispatcher.java:179)[r esteasy-jaxrs-3.0.8.Final.jar:] org.jboss.resteasy.plugins.server.servlet.ServletContainerDispatcher.service(ServletContainerDispatcher.java:220)[resteasy-jaxrs-3.0.8.Final.jar:]在org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher.service(HttpServletDispatcher.java:56)[resteasy-jaxrs-3.0.8.Final.jar:] at org.jboss.resteasy.plugins.server.servlet . HttpServletDispatcher.service(HttpServletDispatcher.java:51)[resteasy-jaxrs-3.0.8.Final.jar:]在javax.servlet.http.HttpServlet.service(HttpServlet.java:790)[jboss-servlet-api_3.1_spec- 1.0.0.Final.jar:1.0.0.Final] at io.undertow.servlet.handlers.ServletHandler.handleRequest(ServletHandler.java:85)[undertow-servlet-1.0.15.Final.jar:1.0.15 . 最终]在io.undertow.servlet.handlers的io.undertow.servlet.handlers.security.ServletSecurityRoleHandler.handleRequest(ServletSecurityRoleHandler.java:61)[undertow-servlet-1.0.15.Final.jar:1.0.15.Final] .ServletDispatchingHandler.handleRequest(ServletDispatchingHandler.java:36)[undero w-servlet-1.0.15.Final.jar:1.0.15.Final]在io.undertow.server.handlers.PredicateHandler的org.wildfly.extension.undertow.security.SecurityContextAssociationHandler.handleRequest(SecurityContextAssociationHandler.java:78) . handleRequest(PredicateHandler.java:25)[undertow-core-1.0.15.Final.jar:1.0.15.Final] at io.undertow.servlet.handlers.security.SSLInformationAssociationHandler.handleRequest(SSLInformationAssociationHandler.java:113)[下载] -servlet-1.0.15.Final.jar:1.0.15.Final] at io.undertow.servlet.handlers.security.ServletAuthenticationCallHandler.handleRequest(ServletAuthenticationCallHandler.java:56)[undertow-servlet-1.0.15.Final.jar :io.undertow的io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:25)[undertow-core-1.0.15.Final.jar:1.0.15.Final]:1.0.15.Final] . security.handlers.AbstractConfidentialityHandler.handleRequest(AbstractConfidentialityHandler.java:45)[undertow-core-1.0.15.Final.jar:1.0.15.Final] at io.undertow.servlet.handlers.se curity.ServletConfidentialityConstraintHandler.handleRequest(ServletConfidentialityConstraintHandler.java:61)[undertow-servlet-1.0.15.Final.jar:1.0.15.Final] at io.undertow.security.handlers.AuthenticationMechanismsHandler.handleRequest(AuthenticationMechanismsHandler.java:58) [undertow-core-1.0.15.Final.jar:1.0.15.Final] at io.undertow.servlet.handlers.security.CachedAuthenticatedSessionHandler.handleRequest(CachedAuthenticatedSessionHandler.java:70)[undertow-servlet-1.0.15.Final .jar:1.0.15.Final] at io.undertow.security.handlers.SecurityInitialHandler.handleRequest(SecurityInitialHandler.java:76)[undertow-core-1.0.15.Final.jar:1.0.15.Final] at io . 来自org.wildfly.extension.undertow.security.jacc.JACCContextIdHandler的undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:25)[undertow-core-1.0.15.Final.jar:1.0.15.Final] . handleRequest(JACCContextIdHandler.java:61)at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:25)[undertow-c ore-1.0.15.Final.jar:1.0.15.Final] at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:25)[undertow-core-1.0.15.Final.jar:1.0 . 15.Final] at io.undertow.servlet.handlers.ServletInitialHandler.handleFirstRequest(ServletInitialHandler.java:240)[undertow-servlet-1.0.15.Final.jar:1.0.15.Final] at io.undertow.servlet.handlers .ServletInitialHandler.dispatchRequest(ServletInitialHandler.java:227)[undertow-servlet-1.0.15.Final.jar:1.0.15.Final] at在io.undertow.servlet.handlers.ServletInitialHandler $ 1中的io.undertow.servlet.handlers.ServletInitialHandler.access $ 000(ServletInitialHandler.java:73)[undertow-servlet-1.0.15.Final.jar:1.0.15.Final] . handleRequest(ServletInitialHandler.java:146)[undertow-servlet-1.0.15.Final.jar:1.0.15.Final] at io.undertow.server.Connectors.executeRootHandler(Connectors.java:177)[undertow-core-1.0 .15.Final.jar:1.0.15.Final] at io.undertow.server.HttpServerExchange $ 1.run(HttpServerExchange.java:727)[undertow-core-1.0.15.Final.jar:1.0.15.Final]在java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)[rt.jar:1.7.0_45] java.util.concurrent.ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor.java:615)[rt.jar: 1.7.0_45]在java.lang.Thread.run(Thread.java:744)[rt.jar:1.7.0_45]引起:java.lang.IllegalArgumentException:参数类型不匹配sun.reflect.NativeMethodAccessorImpl.invoke0(Native方法)[rt.jar:1.7.0_45] at sun.reflect.NativeMethodAccessorImpl.invoke(NativeM ethodAccessorImpl.java:57)[rt.jar:1.7.0_45] at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)[rt.jar:1.7.0_45] at java.lang.reflect.Method.invoke(方法.java:606)[rt.jar:1.7.0_45]在org.jboss.resteasy.core.MethodInjectorImpl.invoke(MethodInjectorImpl.java:137)[resteasy-jaxrs-3.0.8.Final.jar:] ..还有35个

UPDATE3:我没有找到任何解决方案,所以我用 @FormParam 而不是json替换了方法参数和 @Consumes . 但无论如何都需要解决方案 .

知秋君
上一篇 2024-07-03 15:32
下一篇 2024-07-03 15:32

相关推荐