Skip to content

Commit 995ef2a

Browse files
committed
read class-level @Produces/@consumes
1 parent 69a8779 commit 995ef2a

2 files changed

Lines changed: 44 additions & 12 deletions

File tree

‎jaxrs/src/main/java/feign/jaxrs/JAXRSContract.java‎

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,14 @@ protected void processAnnotationOnClass(MethodMetadata data, Class<?> clz) {
6565
}
6666
data.template().insert(0, pathValue);
6767
}
68+
Consumes consumes = clz.getAnnotation(Consumes.class);
69+
if (consumes != null) {
70+
handleConsumesAnnotation(data, consumes, clz.getName());
71+
}
72+
Produces produces = clz.getAnnotation(Produces.class);
73+
if (produces != null) {
74+
handleProducesAnnotation(data, produces, clz.getName());
75+
}
6876
}
6977

7078
@Override
@@ -90,20 +98,28 @@ protected void processAnnotationOnMethod(MethodMetadata data, Annotation methodA
9098
methodAnnotationValue = methodAnnotationValue.replaceAll("\\{\\s*(.+?)\\s*(:.+?)?\\}", "\\{$1\\}");
9199
data.template().append(methodAnnotationValue);
92100
} else if (annotationType == Produces.class) {
93-
String[] serverProduces = ((Produces) methodAnnotation).value();
94-
String clientAccepts = serverProduces.length == 0 ? null : emptyToNull(serverProduces[0]);
95-
checkState(clientAccepts != null, "Produces.value() was empty on method %s",
96-
method.getName());
97-
data.template().header(ACCEPT, clientAccepts);
101+
handleProducesAnnotation(data, (Produces) methodAnnotation, "method " + method.getName());
98102
} else if (annotationType == Consumes.class) {
99-
String[] serverConsumes = ((Consumes) methodAnnotation).value();
100-
String clientProduces = serverConsumes.length == 0 ? null : emptyToNull(serverConsumes[0]);
101-
checkState(clientProduces != null, "Consumes.value() was empty on method %s",
102-
method.getName());
103-
data.template().header(CONTENT_TYPE, clientProduces);
103+
handleConsumesAnnotation(data, (Consumes) methodAnnotation, "method " + method.getName());
104104
}
105105
}
106106

107+
private void handleProducesAnnotation(MethodMetadata data, Produces produces, String name) {
108+
String[] serverProduces = produces.value();
109+
String clientAccepts = serverProduces.length == 0 ? null : emptyToNull(serverProduces[0]);
110+
checkState(clientAccepts != null, "Produces.value() was empty on %s", name);
111+
data.template().header(ACCEPT, (String) null); // remove any previous produces
112+
data.template().header(ACCEPT, clientAccepts);
113+
}
114+
115+
private void handleConsumesAnnotation(MethodMetadata data, Consumes consumes, String name) {
116+
String[] serverConsumes = consumes.value();
117+
String clientProduces = serverConsumes.length == 0 ? null : emptyToNull(serverConsumes[0]);
118+
checkState(clientProduces != null, "Consumes.value() was empty on %s", name);
119+
data.template().header(CONTENT_TYPE, (String) null); // remove any previous consumes
120+
data.template().header(CONTENT_TYPE, clientProduces);
121+
}
122+
107123
@Override
108124
protected boolean processAnnotationsOnParameter(MethodMetadata data, Annotation[] annotations,
109125
int paramIndex) {

‎jaxrs/src/test/java/feign/jaxrs/JAXRSContractTest.java‎

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
import javax.ws.rs.PathParam;
3939
import javax.ws.rs.Produces;
4040
import javax.ws.rs.QueryParam;
41+
import javax.ws.rs.core.MediaType;
4142

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

122123
assertThat(md.template())
123-
.hasHeaders(entry("Accept", asList("application/xml")));
124+
.hasHeaders(
125+
entry("Content-Type", asList("application/json")),
126+
entry("Accept", asList("application/xml")));
124127
}
125128

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

146149
assertThat(md.template())
147-
.hasHeaders(entry("Content-Type", asList("application/xml")));
150+
.hasHeaders(entry("Accept", asList("text/html")), entry("Content-Type", asList("application/xml")));
148151
}
149152

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

169+
@Test
170+
public void producesAndConsumesOnClassAddsHeader() throws Exception {
171+
MethodMetadata md = parseAndValidateMetadata(ProducesAndConsumes.class, "producesAndConsumes");
172+
173+
assertThat(md.template())
174+
.hasHeaders(entry("Content-Type", asList("application/json")), entry("Accept", asList("text/html")));
175+
}
176+
166177
@Test
167178
public void bodyParamIsGeneric() throws Exception {
168179
MethodMetadata md = parseAndValidateMetadata(BodyParams.class, "post", List.class);
@@ -401,6 +412,8 @@ interface WithQueryParamsInPath {
401412
Response empty();
402413
}
403414

415+
@Consumes(MediaType.APPLICATION_JSON)
416+
@Produces(MediaType.TEXT_HTML)
404417
interface ProducesAndConsumes {
405418

406419
@GET
@@ -426,6 +439,9 @@ interface ProducesAndConsumes {
426439
@POST
427440
@Consumes({""})
428441
Response consumesEmpty();
442+
443+
@POST
444+
Response producesAndConsumes();
429445
}
430446

431447
interface BodyParams {

0 commit comments

Comments
 (0)