Jackson will not serialize JSONObject object

2

I have a service like this:

@Path("/test")
public class TestEndPoint {

    @GET
    @Produces("application/json")
    public Response get(){
        POJO pojo = new POJO();
        pojo.setName("Rafael");

        return Response.ok(pojo).build();
    }

}

and works perfectly

but with JSONObject, it does not work

@Path("/test")
public class TestEndPoint {

    @GET
    @Produces("application/json")
    public Response get(){
        org.json.JSONObject json = new org.json.JSONObject();
        json.put("name", "Rafael");

        return Response.ok(json).build();
    }

}

I use JBossAS 7.1 with Jackson provider.

ps - I have a web service with JERSEY and it works fine with JSONObject, but with RESTEASY it does not. I need to do .toString so that it can generate the answer without exception.

    
asked by anonymous 06.01.2016 / 18:26

1 answer

2

Hello,

Build using JsonObjectBuilder:

@GET
@Produces({MediaType.APPLICATION_JSON})
@Path("getnamejson")
public Response getnamejson() {
    JsonObjectBuilder jsonObjectBuilder = Json.createObjectBuilder();
    jsonObjectBuilder.add("name", "Rafael");

    return Response.ok(jsonObjectBuilder.build()).build();
}
    
07.01.2016 / 15:36