Created
September 6, 2013 11:29
-
-
Save mallim/6462601 to your computer and use it in GitHub Desktop.
Jersey + Jackson can map to a String and a Map<String,Object>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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