我要做的是公开一个以JSONObject
为参数的服务。喜欢:
public interface GenericJsonInputProcessing {
@POST
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public JSONObject processGenericURI(JSONObject parameter) throws Exceptions;
}
我需要在请求URI中发送的JSON是:
{"ROOT": "XYZ", "number": "001", "params": { "FIRST_NAME": "memberFirstName","LAST_NAME ": "memberLastName", "EMAIL_ADDRESS": "memberEmail","REQUEST": "request","OTHER": { "OTHER_ID":"Id","TEST_DETAILS": {"Test_id": "testId"}}}}
答案 0 :(得分:0)
您需要实现 MessageBodyReader ,以便将请求主体解析为JSONObject。
@Consumes("application/json")
public class JSONReader implements MessageBodyReader<JSONObject> {
@Override
public boolean isReadable(Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) {
return type == JSONObject.class;
}
@Override
public JSONObject readFrom(Class<JSONObject> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, String> httpHeaders, InputStream entityStream) throws IOException, WebApplicationException {
Scanner s = new Scanner(entityStream).useDelimiter("\\A");
String jsonString = s.hasNext() ? s.next() : "{}";
JSONObject object=parseJSON(jsonString);
return object;
}
JSONObject parseJSON(String jsonString){
//parse jsonString and return JSONObject
}
}
根据您使用的JSON库,您必须实现parseJSON()
方法