Error when trying to return an integer as an object
I noticed that when I used Response.status(201).entity(id).build
, it returns the following error:
Heavy: 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();
}
+3
source to share
2 answers
One object Integer
cannot be converted to JSON because JSON it is like a map (key-value pairs). You have options:
1) Change the return type to text
@Produces({"text/plain"})
2) Create a class that represents your single value as JSON, for example:
class IntValue {
private Integer value;
public IntValue(int value) {
this.value = value;
}
// getter, setter
}
and then do the following
return Response.status(201).entity(new IntValue(id)).build();
+1
source to share