本文整理汇总了Java中javax.ws.rs.ext.MessageBodyReader类的典型用法代码示例。如果您正苦于以下问题:Java MessageBodyReader类的具体用法?Java MessageBodyReader怎么用?Java MessageBodyReader使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
MessageBodyReader类属于javax.ws.rs.ext包,在下文中一共展示了MessageBodyReader类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: JerseyApplication
import javax.ws.rs.ext.MessageBodyReader; //导入依赖的package包/类
@Inject
public JerseyApplication(ServiceLocator serviceLocator) {
GuiceBridge.getGuiceBridge().initializeGuiceBridge(serviceLocator);
GuiceIntoHK2Bridge guiceBridge = serviceLocator.getService(GuiceIntoHK2Bridge.class);
guiceBridge.bridgeGuiceInjector(AppLoader.injector);
String disableMoxy = PropertiesHelper.getPropertyNameForRuntime(
CommonProperties.MOXY_JSON_FEATURE_DISABLE,
getConfiguration().getRuntimeType());
property(disableMoxy, true);
property(ServerProperties.BV_SEND_ERROR_IN_RESPONSE, true);
// add the default Jackson exception mappers
register(JsonParseExceptionMapper.class);
register(JsonMappingExceptionMapper.class);
register(JacksonJsonProvider.class, MessageBodyReader.class, MessageBodyWriter.class);
packages(JerseyApplication.class.getPackage().getName());
for (JerseyConfigurator configurator: AppLoader.getExtensions(JerseyConfigurator.class)) {
configurator.configure(this);
}
}
示例2: shouldRegisterAMultiTypedProviderInstanceWithPriorities
import javax.ws.rs.ext.MessageBodyReader; //导入依赖的package包/类
@Test
public void shouldRegisterAMultiTypedProviderInstanceWithPriorities() {
MultiTypedProvider provider = new MultiTypedProvider();
Map<Class<?>, Integer> priorities = new HashMap<>();
priorities.put(ClientRequestFilter.class, 500);
priorities.put(ClientResponseFilter.class, 501);
priorities.put(MessageBodyReader.class, 502);
priorities.put(MessageBodyWriter.class, 503);
priorities.put(ReaderInterceptor.class, 504);
priorities.put(WriterInterceptor.class, 505);
priorities.put(ResponseExceptionMapper.class, 506);
priorities.put(ParamConverterProvider.class, 507);
RestClientBuilder builder = RestClientBuilder.newBuilder().register(provider, priorities);
Configuration configuration = builder.getConfiguration();
assertTrue(configuration.isRegistered(MultiTypedProvider.class), MultiTypedProvider.class + " should be registered");
assertTrue(configuration.isRegistered(provider), MultiTypedProvider.class + " should be registered");
Map<Class<?>, Integer> contracts = configuration.getContracts(MultiTypedProvider.class);
assertEquals(contracts.size(), priorities.size(),
"There should be "+priorities.size()+" provider types registered");
for(Map.Entry<Class<?>, Integer> priority : priorities.entrySet()) {
Integer contractPriority = contracts.get(priority.getKey());
assertEquals(contractPriority, priority.getValue(), "The priority for "+priority.getKey()+" should be "+priority.getValue());
}
}
示例3: configure
import javax.ws.rs.ext.MessageBodyReader; //导入依赖的package包/类
@Override
public boolean configure(final FeatureContext context) {
PluginLoader.INSTANCE.plugins.get().stream()
.filter(module -> module.jacksonFeatureProperties()!=null)
.map(Plugin::jacksonFeatureProperties)
.map(fn->fn.apply(context))
.forEach(map -> {
addAll(map,context);
});
JacksonJaxbJsonProvider provider = new JacksonJaxbJsonProvider();
provider.setMapper(JacksonUtil.getMapper());
context.register(provider, new Class[]{MessageBodyReader.class, MessageBodyWriter.class});
return true;
}
示例4: configure
import javax.ws.rs.ext.MessageBodyReader; //导入依赖的package包/类
public boolean configure(final FeatureContext context) {
final Configuration config = context.getConfiguration();
final String jsonFeature = CommonProperties.getValue(config.getProperties(), config.getRuntimeType(), InternalProperties.JSON_FEATURE, JSON_FEATURE,
String.class);
// Other JSON providers registered.
if (!JSON_FEATURE.equalsIgnoreCase(jsonFeature)) {
return false;
}
// Disable other JSON providers.
context.property(PropertiesHelper.getPropertyNameForRuntime(InternalProperties.JSON_FEATURE, config.getRuntimeType()), JSON_FEATURE);
// Register FastJson.
if (!config.isRegistered(FastJsonProvider.class)) {
//DisableCircularReferenceDetect
FastJsonProvider fastJsonProvider = new FastJsonProvider();
FastJsonConfig fastJsonConfig = new FastJsonConfig();
//fastJsonConfig.setSerializerFeatures(SerializerFeature.DisableCircularReferenceDetect,SerializerFeature.BrowserSecure);
fastJsonConfig.setSerializerFeatures(SerializerFeature.DisableCircularReferenceDetect);
fastJsonProvider.setFastJsonConfig(fastJsonConfig);
context.register(fastJsonProvider, MessageBodyReader.class, MessageBodyWriter.class);
}
return true;
}
示例5: getImpl
import javax.ws.rs.ext.MessageBodyReader; //导入依赖的package包/类
@SuppressWarnings("unchecked")
private T getImpl(final MinijaxRequestContext context, final InputStream entityStream) throws IOException {
if (entityClass == String.class) {
return (T) IOUtils.toString(entityStream, StandardCharsets.UTF_8);
}
if (entityClass == MultivaluedMap.class) {
return (T) context.getForm().asForm().asMap();
}
final MediaType mediaType = consumesTypes != null && !consumesTypes.isEmpty() ? consumesTypes.get(0) : null;
final MessageBodyReader<T> reader = context.getApplication().getProviders().getMessageBodyReader(entityClass, genericType, annotations, mediaType);
if (reader != null) {
final MultivaluedMap<String, String> httpHeaders = context.getHeaders();
return reader.readFrom(entityClass, genericType, annotations, mediaType, httpHeaders, entityStream);
}
throw new InjectException("Unknown entity type (" + entityClass + ")");
}
示例6: register
import javax.ws.rs.ext.MessageBodyReader; //导入依赖的package包/类
@SuppressWarnings("unchecked")
public void register(final Class<?> c) {
if (MessageBodyReader.class.isAssignableFrom(c)) {
readers.add((Class<MessageBodyReader<?>>) c, MediaTypeUtils.parseMediaTypes(c.getAnnotation(Consumes.class)));
}
if (MessageBodyWriter.class.isAssignableFrom(c)) {
writers.add((Class<MessageBodyWriter<?>>) c, MediaTypeUtils.parseMediaTypes(c.getAnnotation(Produces.class)));
}
if (ExceptionMapper.class.isAssignableFrom(c)) {
exceptionMappers.add((Class<ExceptionMapper<?>>) c, MediaTypeUtils.parseMediaTypes(c.getAnnotation(Produces.class)));
}
if (ParamConverterProvider.class.isAssignableFrom(c)) {
paramConverterProviders.add((ParamConverterProvider) application.get(c));
}
}
示例7: getMessageBodyReader
import javax.ws.rs.ext.MessageBodyReader; //导入依赖的package包/类
@Override
@SuppressWarnings({ "rawtypes", "unchecked" })
public <T> MessageBodyReader<T> getMessageBodyReader(
final Class<T> type,
final Type genericType,
final Annotation[] annotations,
final MediaType mediaType) {
for (final Class<? extends MessageBodyReader<?>> readerClass : readers.get(mediaType)) {
final MessageBodyReader reader = application.get(readerClass);
if (reader.isReadable(type, genericType, annotations, mediaType)) {
return reader;
}
}
return null;
}
示例8: shouldRegisterAMultiTypedProviderClassWithPriorities
import javax.ws.rs.ext.MessageBodyReader; //导入依赖的package包/类
@Test
public void shouldRegisterAMultiTypedProviderClassWithPriorities() {
Map<Class<?>, Integer> priorities = new HashMap<>();
priorities.put(ClientRequestFilter.class, 500);
priorities.put(ClientResponseFilter.class, 501);
priorities.put(MessageBodyReader.class, 502);
priorities.put(MessageBodyWriter.class, 503);
priorities.put(ReaderInterceptor.class, 504);
priorities.put(WriterInterceptor.class, 505);
priorities.put(ResponseExceptionMapper.class, 506);
priorities.put(ParamConverterProvider.class, 507);
RestClientBuilder builder = RestClientBuilder.newBuilder().register(MultiTypedProvider.class, priorities);
Configuration configuration = builder.getConfiguration();
assertTrue(configuration.isRegistered(MultiTypedProvider.class), MultiTypedProvider.class + " should be registered");
Map<Class<?>, Integer> contracts = configuration.getContracts(MultiTypedProvider.class);
assertEquals(contracts.size(), priorities.size(),
"There should be "+priorities.size()+" provider types registered");
for(Map.Entry<Class<?>, Integer> priority : priorities.entrySet()) {
Integer contractPriority = contracts.get(priority.getKey());
assertEquals(contractPriority, priority.getValue(), "The priority for "+priority.getKey()+" should be "+priority.getValue());
}
}
示例9: configure
import javax.ws.rs.ext.MessageBodyReader; //导入依赖的package包/类
public boolean configure(final FeatureContext context) {
final Configuration config = context.getConfiguration();
final String jsonFeature = CommonProperties.getValue(config.getProperties(), config.getRuntimeType(), InternalProperties.JSON_FEATURE, JSON_FEATURE,
String.class);
// Other JSON providers registered.
if (!JSON_FEATURE.equalsIgnoreCase(jsonFeature)) {
return false;
}
// Disable other JSON providers.
context.property(PropertiesHelper.getPropertyNameForRuntime(InternalProperties.JSON_FEATURE, config.getRuntimeType()), JSON_FEATURE);
// Register FastJson.
if (!config.isRegistered(FastJsonProvider.class)) {
context.register(FastJsonProvider.class, MessageBodyReader.class, MessageBodyWriter.class);
}
return true;
}
示例10: testLocateMostRelevantConstructor
import javax.ws.rs.ext.MessageBodyReader; //导入依赖的package包/类
@Test
public void testLocateMostRelevantConstructor() {
ReflectionProvider rp = instance(ReflectionProvider.class);
Providers providers = instance(Providers.class);
ProviderFilter providerFilter = instance(ProviderFilter.class);
Field f = rp.getField(JaxRsResolverTest.class, "genericField3");
RuntimeDelegate.setInstance(new DefaultRuntimeDelegate());
MessageBodyReader messageBodyReader = providers.getMessageBodyReader(f.getType(), f.getGenericType(), f.getAnnotations(), MediaType.APPLICATION_XML_TYPE);
assertNotNull(messageBodyReader);
assertEquals(PersonMessageBodyReader.class, messageBodyReader.getClass());
Constructor<?> c = providerFilter.mostRelevantConstructor(messageBodyReader.getClass());
assertEquals(4, c.getParameterCount());
assertEquals("public com.geemvc.jaxrs.PersonMessageBodyReader(javax.ws.rs.core.Application,javax.ws.rs.core.UriInfo,javax.ws.rs.core.HttpHeaders,javax.ws.rs.ext.Providers)", c.toString());
}
示例11: getClasses
import javax.ws.rs.ext.MessageBodyReader; //导入依赖的package包/类
@Override
public Set<Class<?>> getClasses() {
Set<Class<?>> s = new HashSet<Class<?>>();
// rest Resources
Set<Class<? extends RESTResource>> restResourceTypes = this.getRESTResourceTypes();
if (CollectionUtils.hasData(restResourceTypes)) s.addAll(restResourceTypes);
// Request received objects mappers: transforms Java->XML for REST methods param types
Set<Class<? extends MessageBodyReader<?>>> reqReceivedTypesMappers = this.getRequestReceivedTypesMappers();
if (CollectionUtils.hasData(reqReceivedTypesMappers)) s.addAll(reqReceivedTypesMappers);
// Response sent objects mappers: transforms Java->XML for REST methods return types
Set<Class<? extends MessageBodyWriter<?>>> respSentTypesMappers = this.getResponseSentTypesMappers();
if (CollectionUtils.hasData(respSentTypesMappers)) s.addAll(respSentTypesMappers);
// Exception Mappers
Set<Class<? extends ExceptionMapper<?>>> expsMappers = this.getExceptionsMappers();
if (CollectionUtils.hasData(expsMappers)) s.addAll(expsMappers);
return s;
}
示例12: readFrom
import javax.ws.rs.ext.MessageBodyReader; //导入依赖的package包/类
@Override
public JsonWebKeySet readFrom(final Class<JsonWebKeySet> type,
final Type genericType,
final Annotation[] annotations,
final MediaType mediaType,
final MultivaluedMap<String, String> httpHeaders,
final InputStream inputStream) throws IOException,
WebApplicationException {
final JsonArray keysArray = Json.createReader(inputStream)
.readObject()
.getJsonArray("keys");
final JsonWebKeySet keySet = new JsonWebKeySet();
final MessageBodyReader<JsonWebKey> reader = providers.getMessageBodyReader(JsonWebKey.class, JsonWebKey.class, annotations, mediaType);
for (final JsonValue key : keysArray) {
final InputStream keyStream = new ByteArrayInputStream(key.toString()
.getBytes(CharSets.UTF8));
final JsonWebKey jsonWebKey = reader.readFrom(JsonWebKey.class, null, annotations, mediaType, null, keyStream);
keySet.add(jsonWebKey);
}
return keySet;
}
示例13: shouldGetContentAsList
import javax.ws.rs.ext.MessageBodyReader; //导入依赖的package包/类
@Test
public void shouldGetContentAsList() throws Exception {
List<String> list = Lists.newArrayList("test1", "test2", "test3");
byte[] bytes = new byte[10];
MediaType mediaType = mock(MediaType.class);
HttpHeaders httpHeaders = mock(HttpHeaders.class);
when(httpHeaders.getMediaType()).thenReturn(mediaType);
MessageBodyReader reader = mock(MessageBodyReader.class);
when(reader.readFrom(eq(String.class), eq(String.class), eq(new Annotation[]{}), eq(mediaType), isNull(MultivaluedMap.class), any(InputStream.class))).thenThrow(IOException.class);
when(reader.readFrom(eq(List.class), any(Type.class), eq(new Annotation[]{}), eq(mediaType), isNull(MultivaluedMap.class), any(InputStream.class))).thenReturn(list);
Providers providers = mock(Providers.class);
when(providers.getMessageBodyReader(String.class, String.class, new Annotation[]{}, mediaType)).thenReturn(reader);
when(providers.getMessageBodyReader(List.class, ResourceUtil.listType(String.class).getType(), new Annotation[]{}, mediaType)).thenReturn(reader);
Object content = ResourceUtil.getContent(bytes, providers, httpHeaders, String.class);
assertTrue(content instanceof List);
assertEquals(content, list);
}
示例14: shouldGetContentAsMap
import javax.ws.rs.ext.MessageBodyReader; //导入依赖的package包/类
@Test
public void shouldGetContentAsMap() throws Exception {
Map<String, Object> map = new HashMap<String, Object>();
map.put("key1", "value1");
byte[] bytes = new byte[10];
MediaType mediaType = mock(MediaType.class);
HttpHeaders httpHeaders = mock(HttpHeaders.class);
when(httpHeaders.getMediaType()).thenReturn(mediaType);
MessageBodyReader reader = mock(MessageBodyReader.class);
when(reader.readFrom(eq(Map.class), eq(Map.class), eq(new Annotation[]{}), eq(mediaType), isNull(MultivaluedMap.class), any(InputStream.class))).thenReturn(map);
Providers providers = mock(Providers.class);
when(providers.getMessageBodyReader(Map.class, Map.class, new Annotation[]{}, mediaType)).thenReturn(reader);
Object content = ResourceUtil.getContent(bytes, providers, httpHeaders, Map.class);
assertTrue(content instanceof Map);
assertEquals(content, map);
}
示例15: shouldInvokeMethodWithArguments
import javax.ws.rs.ext.MessageBodyReader; //导入依赖的package包/类
@Test
public void shouldInvokeMethodWithArguments() throws Throwable {
Map<String, Object> content = new HashMap<String, Object>();
content.put("value", "test123");
content.put("someNumber", 1L);
Map<String, Object> values = new HashMap<String, Object>();
DummyModel model = new DummyModel();
byte[] bytes = new byte[10];
MediaType mediaType = mock(MediaType.class);
HttpHeaders httpHeaders = mock(HttpHeaders.class);
when(httpHeaders.getMediaType()).thenReturn(mediaType);
MessageBodyReader reader = mock(MessageBodyReader.class);
when(reader.readFrom(eq(Map.class), eq(Map.class), isNull(Annotation[].class), eq(mediaType), isNull(MultivaluedMap.class), any(InputStream.class))).thenReturn(content);
Providers providers = mock(Providers.class);
when(providers.getMessageBodyReader(Map.class, Map.class, null, mediaType)).thenReturn(reader);
assertEquals(ResourceUtil.invokeAction(model, "dummyActionWithArguments", Lists.newArrayList(new ParameterMetaData("value", "value", String.class),
new ParameterMetaData("someNumber", "someNumber", Long.class)), bytes, providers, httpHeaders, values), "dummyActionWithArguments");
}