简体   繁体   中英

Java Jackson JSON without POJOs

I am currently converting a node project to DW. The node project currently stores JSON from the client as a string and then returns application/JSON when the client gets the same resource. This is basically a Key/value store reset endpoint.

I want to implement the same with dropwizard, but I do not want to map out the entire structure and it's nested structures in POJOs. Is there a way to allow for this? I just want to save the JSON as a string in the db with the key, I do not want to work with the data at all in the java application.

Since the APIs require that I return an object, what should the object be? Here is the function I have for DW, as you can see it is expecting to return an object.

@GET
@UnitOfWork
@Timed
@ApiOperation(value="Find value by ID", notes="Get a value object from the DAO using ID as the sole criterion")
@ApiResponses(value={
        @ApiResponse(code=400, message="Invalid ID"),
        @ApiResponse(code=404, message="No object found by specified ID")
})
public SomeJavaOjbectMustGoHere getSample(
            @ApiParam(value="id of object to get", required=true)
            @QueryParam("id")
            Long id
        ) throws WebApplicationException {

    SomeJavaOjbectMustGoHere returned = dao.get(id);
    if (returned == null) throw new WebApplicationException(Response.Status.NOT_FOUND);
    else return returned;
}

Here is most of the code from node:

 module.exports = function (server) { function createResponse(req, res, next) { var key = req.params.name; console.log("Creating Response for key: '" + req.params.name + "'"); // ...some validation... if (..blah..) { // fail out. res.send(403, "response can not be saved without a valid key and structure."); return next(); } var data = JSON.stringify(req.body); // ... store the stuff in the db... res.send(201, "Saved"); return next(); } function getResponse(req, res, next) { try { var payload = {}; // ... get the data matching the key... res.header("Content-Type", "application/json"); res.send(200, JSON.parse(payload.toString())); } catch(err) { console.log('Data not found.' + err); res.send(200, { "id": Math.random().toString(36).substr(3, 8), // ... default structure ... }); } return next(); } function send(req, res, next) { res.send('response ' + req.params.name); return next(); } server.post('/response/:name', createResponse); server.put('/response', send); server.get('/response/:name', getResponse); server.head('/response/:name', send); server.del('/response/:name', function rm(req, res, next) { res.send(204); return next(); }); } 

If you don't want to create your own Object model then Jackson has a JsonNode class .

If you don't want to do anything with the data you could just return a String and add the following annotation to your method:

@Produces(MediaType.APPLICATION_JSON) 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM