Reputation: 609
i would like to send a single String in payload, like this:
{
"value":"myvalue"
}
In my method i will receive a single String value, not a object.
Is possible to resteasy take only the "myvalue", with no mapping?
Thanks !
Upvotes: 1
Views: 2346
Reputation: 209002
Use the JSON Processing API. Something like:
@POST
@Consumes(MediaType.APPLICATION_JSON)
public Response postJson(String json) {
String value = null;
try (JsonReader reader = Json.createReader(new StringReader(json))) {
JsonObject object = reader.readObject();
JsonValue jsonValue = object.get("value");
value = jsonValue.toString();
System.out.println(value);
}
return Response.created(newUri).build();
}
Create a model class and let the JAX-RS framework handle the binding for you. It will read Json into your object and write it back out as Json
@XmlRootElement
public class Demo {
// Should be same same as key (or we need annotations)
private String value;
public String getValue() { return value; }
public void setValue(String value) {this.value = value;}
}
@POST
@Consumes(MediaType.APPLICATION_JSON)
public Response postJson(Demo demo) {
System.out.println(demo.getValue());
return Response.created(newUri).build();
}
Looking at your comment, you say you want to PUT just a field. Generally, you GET a representation, and PUT the entire representation back to the server. But if this is too cumbersome (i.e. large representations), you may want to look into PATCH. Though not directly supported by JAX-RS, we are allowed to create our own Http method annotations
@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@HttpMethod("PATCH")
public @interface PATCH{}
Just annotation your resource method with this, and you can send requests through the PATCH method. An example Client API request might look something like
client.target(location).request().method("PATCH",Entity.xml(patchObject));
Upvotes: 2