下面列出了javax.ws.rs.core.GenericEntity#getEntity() 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private void doTestPostGetCollectionGenericEntityAndType(WebClient wc, MediaType mediaType) throws Exception {
wc.accept(mediaType).type(mediaType);
GenericEntity<List<Book>> collectionEntity = createGenericEntity();
InvocationCallback<List<Book>> callback = new ListBookInvocationCallback();
Future<List<Book>> future = wc.async().post(Entity.entity(collectionEntity, mediaType),
callback);
List<Book> books2 = future.get();
assertNotNull(books2);
List<Book> books = collectionEntity.getEntity();
assertNotSame(books, books2);
assertEquals(2, books2.size());
Book b11 = books.get(0);
assertEquals(123L, b11.getId());
assertEquals("CXF in Action", b11.getName());
Book b22 = books.get(1);
assertEquals(124L, b22.getId());
assertEquals("CXF Rocks", b22.getName());
assertEquals(200, wc.getResponse().getStatus());
}
@Test
public void testPostGetCollectionGenericEntityAndType2() throws Exception {
String endpointAddress =
"http://localhost:" + PORT + "/bookstore/collections";
WebClient wc = WebClient.create(endpointAddress);
wc.accept("application/xml").type("application/xml");
GenericEntity<List<Book>> collectionEntity = createGenericEntity();
GenericType<List<Book>> genericResponseType = new GenericType<List<Book>>() {
};
Future<List<Book>> future = wc.async().post(Entity.entity(collectionEntity, "application/xml"),
genericResponseType);
List<Book> books2 = future.get();
assertNotNull(books2);
List<Book> books = collectionEntity.getEntity();
assertNotSame(books, books2);
assertEquals(2, books2.size());
Book b11 = books.get(0);
assertEquals(123L, b11.getId());
assertEquals("CXF in Action", b11.getName());
Book b22 = books.get(1);
assertEquals(124L, b22.getId());
assertEquals("CXF Rocks", b22.getName());
assertEquals(200, wc.getResponse().getStatus());
}
@Test
public void testPostGetCollectionGenericEntityAndType3() throws Exception {
String endpointAddress =
"http://localhost:" + PORT + "/bookstore/collections";
WebClient wc = WebClient.create(endpointAddress);
wc.accept("application/xml").type("application/xml");
GenericEntity<List<Book>> collectionEntity = createGenericEntity();
GenericType<List<Book>> genericResponseType = new GenericType<List<Book>>() {
};
Future<Response> future = wc.async().post(Entity.entity(collectionEntity, "application/xml"));
Response r = future.get();
List<Book> books2 = r.readEntity(genericResponseType);
assertNotNull(books2);
List<Book> books = collectionEntity.getEntity();
assertNotSame(books, books2);
assertEquals(2, books2.size());
Book b11 = books.get(0);
assertEquals(123L, b11.getId());
assertEquals("CXF in Action", b11.getName());
Book b22 = books.get(1);
assertEquals(124L, b22.getId());
assertEquals("CXF Rocks", b22.getName());
assertEquals(200, wc.getResponse().getStatus());
}
protected Response doInvoke(String httpMethod,
Object body,
Class<?> requestClass,
Type inGenericType,
Class<?> responseClass,
Type outGenericType) {
Annotation[] inAnns = null;
if (body instanceof Entity) {
Entity<?> entity = (Entity<?>)body;
setEntityHeaders(entity);
body = entity.getEntity();
requestClass = body.getClass();
inGenericType = body.getClass();
inAnns = entity.getAnnotations();
}
if (body instanceof GenericEntity) {
GenericEntity<?> genericEntity = (GenericEntity<?>)body;
body = genericEntity.getEntity();
requestClass = genericEntity.getRawType();
inGenericType = genericEntity.getType();
}
MultivaluedMap<String, String> headers = prepareHeaders(responseClass, body);
resetResponse();
Response r = null;
try {
r = doChainedInvocation(httpMethod, headers, body, requestClass, inGenericType,
inAnns, responseClass, outGenericType, null, null);
} finally {
resetResponseStateImmediatelyIfNeeded();
}
int status = r.getStatus();
if (status != 304 && status >= 300 && responseClass != Response.class) {
throw convertToWebApplicationException(r);
}
return r;
}
protected void prepareAsyncClient(String httpMethod,
Object body,
Class<?> requestClass,
Type inType,
Class<?> respClass,
Type outType,
JaxrsClientCallback<?> cb) {
Annotation[] inAnns = null;
if (body instanceof Entity) {
Entity<?> entity = (Entity<?>)body;
setEntityHeaders(entity);
body = entity.getEntity();
requestClass = body.getClass();
inType = body.getClass();
inAnns = entity.getAnnotations();
}
if (body instanceof GenericEntity) {
GenericEntity<?> genericEntity = (GenericEntity<?>)body;
body = genericEntity.getEntity();
requestClass = genericEntity.getRawType();
inType = genericEntity.getType();
}
MultivaluedMap<String, String> headers = prepareHeaders(respClass, body);
resetResponse();
Message m = finalizeMessage(httpMethod, headers, body, requestClass, inType,
inAnns, respClass, outType, null, null);
m.getExchange().setSynchronous(false);
setAsyncMessageObserverIfNeeded(m.getExchange());
m.getExchange().put(JaxrsClientCallback.class, cb);
doRunInterceptorChain(m);
}
/***
* Build a Response, optionally wrapped in a JSONP callback.
* <p>The response will be wrapped in a JSONP callback if any of the following apply:
* <ul>
* <li> The requested media type is <em>application/javascript</em>
* <li> The callback is not null or empty
* </ul>
* @param request The request
* @param response The response to wrap
* @param callback The callback
* @return A Response object, wrapped if necessary.
*/
public static Object wrapJsonp(Request request, GenericEntity<?> response, @Nullable String callback) {
if (JaxRsUtil.isVariant(request, CustomMediaTypes.APPLICATION_JSONP_TYPE) || !isNullOrEmpty(callback)) {
callback = Optional.ofNullable(callback).orElse(DEFAULT_JSONP_CALLBACK);
return new JSONWrappedObject(format("%s(", callback), ");", response.getEntity());
} else {
return Response.ok(response).build();
}
}