Skip to content
This repository was archived by the owner on May 28, 2018. It is now read-only.
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -602,16 +602,15 @@ private MediaType determineResponseMediaType(final Class<?> entityClass,
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) {
// Media types producible by method.
final List<MediaType> methodProducesTypes = !resourceMethod.getProducedTypes().isEmpty() ?
resourceMethod.getProducedTypes() : Lists.newArrayList(MediaType.WILDCARD_TYPE);

for (final MediaType methodProducesType : methodProducesTypes) {
// 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)) {
if (methodProducesType.isCompatible(writerProduces)) {

final CombinedClientServerMediaType.EffectiveMediaType effectiveProduces = new
Expand All @@ -627,7 +626,6 @@ private MediaType determineResponseMediaType(final Class<?> entityClass,
|| CombinedClientServerMediaType.COMPARATOR.compare(candidate, selected) > 0) {
if (writer.isWriteable(responseEntityClass, entityType,
handlingMethod.getDeclaredAnnotations(), candidate.getCombinedMediaType())) {

selected = candidate;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ public static final class Builder {
private List<String> names;
private String path;

private final Set<ResourceMethod.Builder> methodBuilders;
private final List<ResourceMethod.Builder> methodBuilders;
private final Set<Resource.Builder> childResourceBuilders;
private final List<Resource.Data> childResources;

Expand All @@ -219,7 +219,7 @@ public static final class Builder {


private Builder(final Resource.Builder parentResource) {
this.methodBuilders = Sets.newIdentityHashSet();
this.methodBuilders = Lists.newLinkedList();
this.childResourceBuilders = Sets.newIdentityHashSet();
this.childResources = Lists.newLinkedList();
this.resourceMethods = Lists.newLinkedList();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,52 +67,59 @@ public class ContentNegotiationTest extends JerseyTest {

@Path("persons")
public static class MyResource {
private static final Person[] LIST = new Person[] {
new Person("Penny", 1),
new Person("Howard", 2),
new Person("Sheldon", 3)
};

@GET
@Produces({"application/xml;qs=0.75", "application/json;qs=1.0"})
public Person[] getList() {
Person[] list = new Person[3];
list[0] = new Person("Penny", 1);
list[1] = new Person("Howard", 2);
list[2] = new Person("Sheldon", 3);

return list;
return LIST;
}

@GET
@Produces({"application/json;qs=1", "application/xml;qs=0.75"})
@Path("reordered")
public Person[] getListReordered() {
Person[] list = new Person[3];
list[0] = new Person("Penny", 1);
list[1] = new Person("Howard", 2);
list[2] = new Person("Sheldon", 3);

return list;
return LIST;
}

@GET
@Produces({"application/json;qs=0.75", "application/xml;qs=1"})
@Path("inverted")
public Person[] getListInverted() {
Person[] list = new Person[3];
list[0] = new Person("Penny", 1);
list[1] = new Person("Howard", 2);
list[2] = new Person("Sheldon", 3);

return list;
return LIST;
}


@GET
@Produces({"application/xml;qs=0.75", "application/json;qs=0.9", "unknown/hello;qs=1.0"})
@Path("unkownMT")
public Person[] getListWithUnkownType() {
Person[] list = new Person[3];
list[0] = new Person("Penny", 1);
list[1] = new Person("Howard", 2);
list[2] = new Person("Sheldon", 3);
return LIST;
}

@GET
@Produces({"application/json", "application/xml", "text/plain"})
@Path("shouldPickFirst")
public Person[] getJsonArrayUnlessOtherwiseSpecified() {
return LIST;
}

@GET
@Produces("application/json")
@Path("twoMethodsOneEndpoint")
public Person[] getJsonArray() {
return LIST;
}

return list;
@GET
@Produces("application/xml")
@Path("twoMethodsOneEndpoint")
public Person[] getXmlArray() {
return LIST;
}
}

Expand Down Expand Up @@ -173,6 +180,20 @@ public void testWithoutDefinedRequestedMediaType() {
Assert.assertEquals(MediaType.APPLICATION_JSON_TYPE, response.getMediaType());
}

@Test
public void testWithoutDefinedRequestedMediaTypeAndTwoMethods() {
Response response = target().path("/persons/twoMethodsOneEndpoint").request().get();
Assert.assertEquals(200, response.getStatus());
Assert.assertEquals(MediaType.APPLICATION_JSON_TYPE, response.getMediaType());
}

@Test
public void testWithoutDefinedRequestedMediaTypeOrQualityModifiers() {
Response response = target().path("/persons/shouldPickFirst").request().get();
Assert.assertEquals(200, response.getStatus());
Assert.assertEquals(MediaType.APPLICATION_JSON_TYPE, response.getMediaType());
}

@Test
public void test() {
WebTarget target = target().path("/persons");
Expand Down