2

I have a following rest service:

@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();
}

After deploying my app to Wildfly I send HTTP request with json: {"id":0,"param1":value1,"param2":value2} and get following error message:

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

What could I do wrong? How to make my app consume entity from json instead of LinkedHashMap?

UPDATE: entity class code contains nothing interesting, only long id and two String fields with some jpa annotations.

UPDATE2: full stacktrace:

WARN [org.jboss.resteasy.core.ExceptionHandler] (default task-2) Failed executing POST /rest/add: org.jboss.resteasy.spi.InternalServerErrorException: Bad arguments passed to org.jboss.resteasy.spi.metadata.ResourceMethod@4ab32ab7 ( java.util.LinkedHashMap {id=0, param1=value1, param2=value2} ) at org.jboss.resteasy.core.MethodInjectorImpl.invoke(MethodInjectorImpl.java:177) [resteasy-jaxrs-3.0.8.Final.jar:] at org.jboss.resteasy.core.ResourceMethodInvoker.invokeOnTarget(ResourceMethodInvoker.java:296) [resteasy-jaxrs-3.0.8.Final.jar:] at org.jboss.resteasy.core.ResourceMethodInvoker.invoke(ResourceMethodInvoker.java:250) [resteasy-jaxrs-3.0.8.Final.jar:] at org.jboss.resteasy.core.ResourceMethodInvoker.invoke(ResourceMethodInvoker.java:237) [resteasy-jaxrs-3.0.8.Final.jar:] at 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) [resteasy-jaxrs-3.0.8.Final.jar:] at org.jboss.resteasy.plugins.server.servlet.ServletContainerDispatcher.service(ServletContainerDispatcher.java:220) [resteasy-jaxrs-3.0.8.Final.jar:] at 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:] at 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.Final] at io.undertow.servlet.handlers.security.ServletSecurityRoleHandler.handleRequest(ServletSecurityRoleHandler.java:61) [undertow-servlet-1.0.15.Final.jar:1.0.15.Final] at io.undertow.servlet.handlers.ServletDispatchingHandler.handleRequest(ServletDispatchingHandler.java:36) [undertow-servlet-1.0.15.Final.jar:1.0.15.Final] at org.wildfly.extension.undertow.security.SecurityContextAssociationHandler.handleRequest(SecurityContextAssociationHandler.java:78) 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.security.SSLInformationAssociationHandler.handleRequest(SSLInformationAssociationHandler.java:113) [undertow-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: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.security.handlers.AbstractConfidentialityHandler.handleRequest(AbstractConfidentialityHandler.java:45) [undertow-core-1.0.15.Final.jar:1.0.15.Final] at io.undertow.servlet.handlers.security.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.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:25) [undertow-core-1.0.15.Final.jar:1.0.15.Final] at org.wildfly.extension.undertow.security.jacc.JACCContextIdHandler.handleRequest(JACCContextIdHandler.java:61) at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:25) [undertow-core-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.access$000(ServletInitialHandler.java:73) [undertow-servlet-1.0.15.Final.jar:1.0.15.Final] at io.undertow.servlet.handlers.ServletInitialHandler$1.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] at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145) [rt.jar:1.7.0_45] at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615) [rt.jar:1.7.0_45] at java.lang.Thread.run(Thread.java:744) [rt.jar:1.7.0_45] Caused by: java.lang.IllegalArgumentException: argument type mismatch at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) [rt.jar:1.7.0_45] at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.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(Method.java:606) [rt.jar:1.7.0_45] at org.jboss.resteasy.core.MethodInjectorImpl.invoke(MethodInjectorImpl.java:137) [resteasy-jaxrs-3.0.8.Final.jar:] ... 35 more

UPDATE3: I didn't find any solution, so I replaced method argument and @Consumes by using @FormParams instead of json. But the solution is required anyway.

Everv0id
  • 1,862
  • 3
  • 25
  • 47
  • You need make sure your SomeEntity is indeed JSON serializable (say,via JAXB impl) – Shmil The Cat Dec 10 '14 at 21:31
  • Could you please specify your comment? Do i need to add some annotations to my entity bean? – Everv0id Dec 10 '14 at 21:45
  • I know you use resteasy but the ideas from https://jersey.java.net/documentation/1.18/json.html also apply, you need means taking the json from the request body and serializing it to your entity object, most of the JAX-RS impl use JAXB magic doing so – Shmil The Cat Dec 10 '14 at 21:56
  • post your SomeEntity class and list your jars that you have used here for JSON conversion. – Jimmy Dec 10 '14 at 22:22
  • @Jimmy entity class code contains nothing interesting, only `long id` and two `String` fields with some jpa annotations, getters and setters, nothing more. Also, I dont use libraries for JSON conversion, actually I thought that my Wildfly application server will do it automatically. – Everv0id Dec 10 '14 at 22:33
  • in that case, make sure the json attribute matches the name of your property of `SomeEntity` class. like you have `id`, `param1` , `param2` in `JSON`. Make sure you have `private int id, private String param1, private String param2` in your SomeEntity class. these properties name should match. Also make sure you are sending JSON, provide this info in header. – Jimmy Dec 10 '14 at 22:37
  • I checked entity field names and didnt find any errors. HTTP request has attribute Accept:application/json in header. I use IDEA REST plugin to send requests on REST services. – Everv0id Dec 10 '14 at 22:58
  • can you show the HTTP body you're sending to the server? I also wonder if there's more of a stack trace you can share. – John Ament Dec 11 '14 at 02:04
  • @JohnAment http request body contains only json, nothing more. Also, added stacktrace to a post. – Everv0id Dec 11 '14 at 17:16

2 Answers2

1

I had exactly same error using REST Client from IDEA IDE, I missed to put proper Content-Type: application/json in the request and then RESTeasy can translate a java.util.LinkedHashMap as my object in the POST method.

Hope this helps or it can give some hint for your investigation.

  • Thanks for reply! I used IDEA REST plugin too, but I set `Accept:application/json` in request param, so I think that was not a problem. Hope I understood your reply correctly. – Everv0id Dec 23 '14 at 18:38
  • Accept parameter is used for the response, but if you are sending a POST I think you should also put Content-Type to help identify the body. Anyway, hope it can give your some hint. – Lucas Ponce Dec 29 '14 at 15:14
  • Thanks, it's clear now. I thought `Accept` is the same to `Content-Type`. If these are different things, that could explain the problem. – Everv0id Dec 29 '14 at 18:53
0

The JSON that you sent is not valid. You cannot use = (use : instead) and string values must be quoted.

Try a tool such as JSONLint to validate your request body.

matsev
  • 32,104
  • 16
  • 121
  • 156