// Return pre-selected MediaType.
if (usePreSelectedMediaType(selectedMethod, acceptableMediaTypes)) {
return selectedMethod.produces.getCombinedMediaType();
}
final ResourceMethod resourceMethod = selectedMethod.methodAcceptorPair.model;
final Invocable invocable = resourceMethod.getInvocable();
// Entity class can be null when considering HEAD method || empty entity.
final Class<?> responseEntityClass = entityClass == null ? invocable.getRawRoutingResponseType() : entityClass;
final Method handlingMethod = invocable.getHandlingMethod();
CombinedClientServerMediaType selected = null;
for (final MediaType acceptableMediaType : acceptableMediaTypes) {
// Use writers suitable for entity class to determine the media type.
for (final MessageBodyWriter writer : workers.getMessageBodyWritersForType(responseEntityClass)) {
for (final MediaType writerProduces : MediaTypes.createFrom(writer.getClass().getAnnotation(Produces.class))) {
if (writerProduces.isCompatible(acceptableMediaType)) {
// Media types producible by method.
final List<MediaType> methodProducesTypes = !resourceMethod.getProducedTypes().isEmpty() ?
resourceMethod.getProducedTypes() : Lists.newArrayList(MediaType.WILDCARD_TYPE);
for (final MediaType methodProducesType : methodProducesTypes) {
if (methodProducesType.isCompatible(writerProduces)) {
final CombinedClientServerMediaType.EffectiveMediaType effectiveProduces = new