3

I'm getting an issue in my service. Below is my Service

@POST
@Path("/config")
@Consumes(MediaType.APPLICATION_JSON)
public Response saveConfiguration(String name, MultivaluedMap<String,
  Object> properties) {
     return Response.ok().build();
}

And my test case is:

String payload = "{"name": "CRJ001", 
  "properties": {"expression": ["a + b"], 
  "baseClass": ["org.carlspring.strongbox.crontask.test.MyTask"]}}";

WebTarget resource = client.getClientInstance().target(path);
Response response = resource.request(MediaType.APPLICATION_JSON).
   post(Entity.entity(payload, MediaType.APPLICATION_JSON));

int status = response.getStatus();
assertEquals("Failed to save!", Response.ok().build().getStatus(), status);

But I'm getting:

[[FATAL] Method public javax.ws.rs.core.Response org.abc.rest.ConfigurationRestlet.
  saveConfiguration(java.lang.String,javax.ws.rs.core.MultivaluedMap) 
  on resource class org.abc.rest.ConfigurationRestlet contains multiple
  parameters with no annotation. Unable to resolve the injection source.;

Please help me this out

Yougesh
  • 315
  • 3
  • 15
  • What is `org.abc.rest.ConfigurationRestlet`? –  May 31 '16 at 06:43
  • It is rest service class – Yougesh May 31 '16 at 07:40
  • You should use a JAX-RS annotation on your parameter `name`. What part of the request is mapped on it? –  May 31 '16 at 07:48
  • can you please elaborate – Yougesh May 31 '16 at 07:54
  • 1
    You have an argument `String name` in your method `saveConfiguration`. Restlet is trying to assign *something* from the HTTP request to this argument but it has no idea what. You must give a hint, for example use a `@PathParam` annotation on `name`. –  May 31 '16 at 07:57
  • I'm using post call, and i think it will post is auto identifying via fields name, [name, properties] – Yougesh May 31 '16 at 08:37
  • Why do you think this? Usually the *whole* request body is mapped to *one* method argument. all other arguments are mapped from path, URL, header information. –  May 31 '16 at 08:39
  • So what should I use for post call param? `@PathParam` use for Path variables, `@QueryParam` use for URL query param, what for post? – Yougesh May 31 '16 at 08:54

1 Answers1

2

What you are trying to achieve is to map multiple parameters to your REST API from JSON payload of POST method, This is not possible, please look at this answer for detail.

I understand that you want to pass list of properties; the above answer doesn't quite suit your scenario,

what I suggest here is : convert your name parameter to a path parameter or to a query parameter depending on if it is optional or not.

so your should enhance it like this :

path parameter (if name parameter is mandatory)

@POST
@Path("/config")
@Consumes(MediaType.APPLICATION_JSON)
public Response saveConfiguration(@PathParam("name") String name, MultivaluedMap<String,
  Object> properties) {
     return Response.ok().build();
}

query parameter(if name parameter is optional)

@POST
@Path("/config/{name}")
@Consumes(MediaType.APPLICATION_JSON)
public Response saveConfiguration(@QueryParam("name") String name, MultivaluedMap<String,
  Object> properties) {
     return Response.ok().build();
}

in this case name will be passed as query parameter of your request :

POST .../config?name=name1

and obviously in both cases, you should not pass the "name" parameter in your JSON payload.

Community
  • 1
  • 1
LeTex
  • 1,242
  • 1
  • 12
  • 27
  • I'm sorry to say your answer also doesn't suit my scenario, my issue regarding MultiValueMap object, not name property, But I tried your suggested solution but its breaking my code more – Yougesh Jun 01 '16 at 06:12
  • I also did some thing like that: `@POST @Path("/config") @Consumes(MediaType.APPLICATION_JSON) public Response saveConfiguration(MyClass myClass) { return Response.ok().build(); }` And my class is `class MyClass { private String name; private Map properties; /* setter, getter*/ }` but in rest service I'm receiving payload this `"{"name": "CRJ001", "properties": {}}"` empty properties – Yougesh Jun 01 '16 at 06:17
  • 1
    @Y-PKhatri what got broken? how did you apply the change I suggested ? what is the erro you get (if any)? did you adapt your JSON payload accordingly? if you decide to put both name and your list of properties in the same object that should work as well, remember to annotate your 'MyClass' class with '@XmlRootElement' – LeTex Jun 01 '16 at 09:05
  • Im getting 415: unknown Media Type error, im sending name property seperatly wuth path param, and yes I already annotate with JAXB @XmlRootElement, but didn't work – Yougesh Jun 01 '16 at 10:01
  • well, unknown Media Type error is not related to the change you made to your code; in fact it means that your path was found but the media type was not the same. make sure that what you are sending is JSON, if you are using tool like postman, make sure the media type selected is JSON (application/json). – LeTex Jun 01 '16 at 10:23
  • I'm not using POSTMAN, in my post I mentioned the test case, I'm getting same error in my test case – Yougesh Jun 03 '16 at 07:24