Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

error when trying to return an integer as the entity

Tags:

java

jersey

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

    }
like image 396
pothios Avatar asked Oct 15 '25 14:10

pothios


1 Answers

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();
like image 132
Mykola Yashchenko Avatar answered Oct 18 '25 04:10

Mykola Yashchenko



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!