下面列出了怎么用javax.ws.rs.core.NoContentException的API类实例代码及写法,或者点击链接到github查看源代码。
@Override
public JsonObject readFrom(Class<JsonObject> type, Type genericType, Annotation[] annotations, MediaType mediaType,
MultivaluedMap<String, String> httpHeaders, InputStream entityStream) throws IOException, WebApplicationException {
byte[] bytes = getBytes(entityStream);
if (bytes.length == 0) {
throw new NoContentException("Cannot create JsonObject");
}
return new JsonObject(Buffer.buffer(bytes));
}
@Override
public JsonArray readFrom(Class<JsonArray> type, Type genericType, Annotation[] annotations, MediaType mediaType,
MultivaluedMap<String, String> httpHeaders, InputStream entityStream) throws IOException, WebApplicationException {
byte[] bytes = getBytes(entityStream);
if (bytes.length == 0) {
throw new NoContentException("Cannot create JsonArray");
}
return new JsonArray(Buffer.buffer(bytes));
}
@Override public Object readFrom(Class<Object> type, Type genericType, Annotation[] annotations,
MediaType mediaType, MultivaluedMap<String, String> httpHeaders, InputStream entityStream)
throws IOException, WebApplicationException {
JsonAdapter<Object> adapter = moshi.adapter(genericType);
BufferedSource source = Okio.buffer(Okio.source(entityStream));
if (!source.request(1)) {
throw new NoContentException("Stream is empty");
}
return adapter.fromJson(source);
// Note: we do not close the InputStream per the interface documentation.
}
@Test public void emptyReadThrows() throws IOException {
Buffer data = new Buffer();
Class<Object> type = (Class) String.class;
try {
reader.readFrom(type, type, new Annotation[0], APPLICATION_JSON_TYPE,
new MultivaluedHashMap<>(),
data.inputStream());
fail();
} catch (NoContentException ignored) {
}
}
@SuppressWarnings({
"unchecked", "rawtypes"
})
@Test(expected = NoContentException.class)
public void testReadEmptyByte() throws Exception {
MessageBodyReader<?> p = new PrimitiveTextProvider<>();
p.readFrom((Class)Byte.TYPE, null, null,
null,
null,
new ByteArrayInputStream("".getBytes()));
}
protected void reportEmptyContentLength() throws NoContentException {
String message = new org.apache.cxf.common.i18n.Message("EMPTY_BODY", BUNDLE).toString();
LOG.warning(message);
throw new NoContentException(message);
}