Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@mallim
Created September 6, 2013 11:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mallim/6462601 to your computer and use it in GitHub Desktop.
Save mallim/6462601 to your computer and use it in GitHub Desktop.
Jersey + Jackson can map to a String and a Map<String,Object>
import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
@Path("/some_path")
public class AnExampleResource {
@POST
@Path("/sub_path")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public String saveMethod( String inputJsonObj ) throws IOException {
// These steps seems to be SOP
ObjectMapper mapper = new ObjectMapper();
JsonFactory factory = mapper.getFactory();
JsonParser jp = factory.createJsonParser( inputJsonObj );
JsonNode actualObj = mapper.readTree( jp );
// If you want a value
String the_associated_value = actualObj.get( "a_key_in_the_input_json").textValue();
return inputJsonObj;
}
@POST
@Path("/sub_path2")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public Map<String,Object> saveMethod( Map<String,Object> params ) throws IOException {
// Processing steps
return params;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment