Skip to content

Commit 0f4471c

Browse files
committed
decoder example gives an error fix #1547
1 parent 3838ec6 commit 0f4471c

File tree

4 files changed

+10
-13
lines changed

4 files changed

+10
-13
lines changed

jooby/src/main/java/io/jooby/DefaultContext.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -396,7 +396,7 @@ public interface DefaultContext extends Context {
396396
return result;
397397
}
398398
}
399-
return decoder(contentType).decode(this, type);
399+
return (T) decoder(contentType).decode(this, type);
400400
} catch (Exception x) {
401401
throw SneakyThrows.propagate(x);
402402
}

jooby/src/main/java/io/jooby/MessageDecoder.java

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,20 +21,17 @@ public interface MessageDecoder {
2121
/**
2222
* Resolve parsing as {@link StatusCode#UNSUPPORTED_MEDIA_TYPE}.
2323
*/
24-
MessageDecoder UNSUPPORTED_MEDIA_TYPE = new MessageDecoder() {
25-
@Override public <T> T decode(Context ctx, Type type) {
26-
throw new UnsupportedMediaType(ctx.header("Content-Type").valueOrNull());
27-
}
24+
MessageDecoder UNSUPPORTED_MEDIA_TYPE = (ctx, type) -> {
25+
throw new UnsupportedMediaType(ctx.header("Content-Type").valueOrNull());
2826
};
2927

3028
/**
3129
* Parse HTTP body into the given type.
3230
*
3331
* @param ctx Web context.
3432
* @param type Target/expected type.
35-
* @param <T> Dynamic binding of the target type.
3633
* @return An instance of the target type.
3734
* @throws Exception Is something goes wrong.
3835
*/
39-
@Nonnull <T> T decode(@Nonnull Context ctx, @Nonnull Type type) throws Exception;
36+
@Nonnull Object decode(@Nonnull Context ctx, @Nonnull Type type) throws Exception;
4037
}

modules/jooby-jackson/src/main/java/io/jooby/json/JacksonModule.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -132,20 +132,20 @@ public JacksonModule module(Class<? extends Module> module) {
132132
return mapper.writer().writeValueAsBytes(value);
133133
}
134134

135-
@Override public <T> T decode(Context ctx, Type type) throws Exception {
135+
@Override public Object decode(Context ctx, Type type) throws Exception {
136136
Body body = ctx.body();
137137
if (body.isInMemory()) {
138138
if (type == JsonNode.class) {
139-
return (T) mapper.readTree(body.bytes());
139+
return mapper.readTree(body.bytes());
140140
} else {
141-
return (T) mapper.readValue(body.bytes(), typeFactory.constructType(type));
141+
return mapper.readValue(body.bytes(), typeFactory.constructType(type));
142142
}
143143
} else {
144144
try (InputStream stream = body.stream()) {
145145
if (type == JsonNode.class) {
146-
return (T) mapper.readTree(stream);
146+
return mapper.readTree(stream);
147147
} else {
148-
return (T) mapper.readValue(stream, typeFactory.constructType(type));
148+
return mapper.readValue(stream, typeFactory.constructType(type));
149149
}
150150
}
151151
}

modules/jooby-jackson/src/test/java/io/jooby/json/JacksonModuleTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public void parse() throws Exception {
4040

4141
JacksonModule jackson = new JacksonModule();
4242

43-
Map<String, String> result = jackson.decode(ctx, Map.class);
43+
Map<String, String> result = (Map<String, String>) jackson.decode(ctx, Map.class);
4444
assertEquals(mapOf("k", "v"), result);
4545
}
4646

0 commit comments

Comments
 (0)