Skip to content

Commit

Permalink
read class-level @Produces/@consumes
Browse files Browse the repository at this point in the history
  • Loading branch information
pnepywoda committed Sep 10, 2015
1 parent 69a8779 commit 995ef2a
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 12 deletions.
36 changes: 26 additions & 10 deletions jaxrs/src/main/java/feign/jaxrs/JAXRSContract.java
Expand Up @@ -65,6 +65,14 @@ protected void processAnnotationOnClass(MethodMetadata data, Class<?> clz) {
}
data.template().insert(0, pathValue);
}
Consumes consumes = clz.getAnnotation(Consumes.class);
if (consumes != null) {
handleConsumesAnnotation(data, consumes, clz.getName());
}
Produces produces = clz.getAnnotation(Produces.class);
if (produces != null) {
handleProducesAnnotation(data, produces, clz.getName());
}
}

@Override
Expand All @@ -90,20 +98,28 @@ protected void processAnnotationOnMethod(MethodMetadata data, Annotation methodA
methodAnnotationValue = methodAnnotationValue.replaceAll("\\{\\s*(.+?)\\s*(:.+?)?\\}", "\\{$1\\}");
data.template().append(methodAnnotationValue);
} else if (annotationType == Produces.class) {
String[] serverProduces = ((Produces) methodAnnotation).value();
String clientAccepts = serverProduces.length == 0 ? null : emptyToNull(serverProduces[0]);
checkState(clientAccepts != null, "Produces.value() was empty on method %s",
method.getName());
data.template().header(ACCEPT, clientAccepts);
handleProducesAnnotation(data, (Produces) methodAnnotation, "method " + method.getName());
} else if (annotationType == Consumes.class) {
String[] serverConsumes = ((Consumes) methodAnnotation).value();
String clientProduces = serverConsumes.length == 0 ? null : emptyToNull(serverConsumes[0]);
checkState(clientProduces != null, "Consumes.value() was empty on method %s",
method.getName());
data.template().header(CONTENT_TYPE, clientProduces);
handleConsumesAnnotation(data, (Consumes) methodAnnotation, "method " + method.getName());
}
}

private void handleProducesAnnotation(MethodMetadata data, Produces produces, String name) {
String[] serverProduces = produces.value();
String clientAccepts = serverProduces.length == 0 ? null : emptyToNull(serverProduces[0]);
checkState(clientAccepts != null, "Produces.value() was empty on %s", name);
data.template().header(ACCEPT, (String) null); // remove any previous produces
data.template().header(ACCEPT, clientAccepts);
}

private void handleConsumesAnnotation(MethodMetadata data, Consumes consumes, String name) {
String[] serverConsumes = consumes.value();
String clientProduces = serverConsumes.length == 0 ? null : emptyToNull(serverConsumes[0]);
checkState(clientProduces != null, "Consumes.value() was empty on %s", name);
data.template().header(CONTENT_TYPE, (String) null); // remove any previous consumes
data.template().header(CONTENT_TYPE, clientProduces);
}

@Override
protected boolean processAnnotationsOnParameter(MethodMetadata data, Annotation[] annotations,
int paramIndex) {
Expand Down
20 changes: 18 additions & 2 deletions jaxrs/src/test/java/feign/jaxrs/JAXRSContractTest.java
Expand Up @@ -38,6 +38,7 @@
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;

import feign.MethodMetadata;
import feign.Response;
Expand Down Expand Up @@ -120,7 +121,9 @@ public void producesAddsAcceptHeader() throws Exception {
MethodMetadata md = parseAndValidateMetadata(ProducesAndConsumes.class, "produces");

assertThat(md.template())
.hasHeaders(entry("Accept", asList("application/xml")));
.hasHeaders(
entry("Content-Type", asList("application/json")),
entry("Accept", asList("application/xml")));
}

@Test
Expand All @@ -144,7 +147,7 @@ public void consumesAddsContentTypeHeader() throws Exception {
MethodMetadata md = parseAndValidateMetadata(ProducesAndConsumes.class, "consumes");

assertThat(md.template())
.hasHeaders(entry("Content-Type", asList("application/xml")));
.hasHeaders(entry("Accept", asList("text/html")), entry("Content-Type", asList("application/xml")));
}

@Test
Expand All @@ -163,6 +166,14 @@ public void consumesEmpty() throws Exception {
parseAndValidateMetadata(ProducesAndConsumes.class, "consumesEmpty");
}

@Test
public void producesAndConsumesOnClassAddsHeader() throws Exception {
MethodMetadata md = parseAndValidateMetadata(ProducesAndConsumes.class, "producesAndConsumes");

assertThat(md.template())
.hasHeaders(entry("Content-Type", asList("application/json")), entry("Accept", asList("text/html")));
}

@Test
public void bodyParamIsGeneric() throws Exception {
MethodMetadata md = parseAndValidateMetadata(BodyParams.class, "post", List.class);
Expand Down Expand Up @@ -401,6 +412,8 @@ interface WithQueryParamsInPath {
Response empty();
}

@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.TEXT_HTML)
interface ProducesAndConsumes {

@GET
Expand All @@ -426,6 +439,9 @@ interface ProducesAndConsumes {
@POST
@Consumes({""})
Response consumesEmpty();

@POST
Response producesAndConsumes();
}

interface BodyParams {
Expand Down

0 comments on commit 995ef2a

Please sign in to comment.