I noticed that when I used Response.status(201).entity(id).build
, it returns the following error :
Severe: MessageBodyWriter not found for media type=application/json, type=class java.lang.Integer, genericType=class java.lang.Integer.
@POST
@Produces({"application/json"})
public Response createUser(
@NotNull @FormParam("username") String username,
@NotNull @FormParam("password") String password,
@NotNull @FormParam("role") String role) {
int id = 12;
return Response.status(201).entity(id).build();
}
Single Integer
object can't be converted to JSON, because JSON it's like map (key-value pairs). You have to options:
1) Change returned type to text
@Produces({"text/plain"})
2) Create a class which represents your single value as JSON, like:
class IntValue {
private Integer value;
public IntValue(int value) {
this.value = value;
}
// getter, setter
}
and then do following
return Response.status(201).entity(new IntValue(id)).build();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With