im not sure how to get a json object and output it in jersey using rest GET from a ajax json post, im using grizzly server, server has been set, heres the code that supposed to get the json, correct me please, thanks!
import java.io.IOException;
import java.io.InputStream;
import net.sf.json.JSONObject;
import net.sf.json.JSONSerializer;
import org.apache.commons.io.IOUtils;
import javax.ws.rs.*;
@Path("/helloworld")
public class GetData {
@GET
@Consumes("application/json")
public String getResource(JSONObject obj) throws IOException {
InputStream in = (InputStream) obj.values();
String data = IOUtils.toString(in);
JSONObject out = (JSONObject) JSONSerializer.toJSON(data);
String result = out.getString("name");
return result;
}
}
You need to find out, what is your JSON object should be deserialized to. If it's just a JSONObject and you want to parse it manually:
@Consumes("application/json")
public String getResource(JSONObject obj) {
...
}
If it is some kind of custom object:
@Consumes("application/json")
public String getResource(CustomObj customObj) {
...
}
But then you should take care about marshalling/unmarshalling of that object to JSON by Jackson.
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.