对于公开以JSONObject作为参数的方法的服务,REST url是什么

时间:2016-10-31 10:42:47

标签: json rest jax-rs

我要做的是公开一个以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"}}}}

1 个答案:

答案 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()方法