public interface ParamConverter
Jooby.param(ParamConverter)
methodMultibinder<ParamConverter> pcb = Multibinder .newSetBinder(binder, ParamConverter.class); pcb.addBinding().to(MyParamConverter.class);
Date
: It parses a date using the application.dateFormat
property.LocalDate
: It parses a date using the application.dateFormat
property.Locale
valueOf
fromName
fromString
String
argumentJooby.param(ParamConverter)
Modifier and Type | Interface and Description |
---|---|
static interface |
ParamConverter.Context
Param execution context.
|
Modifier and Type | Method and Description |
---|---|
Object |
convert(com.google.inject.TypeLiteral<?> type,
Object[] values,
ParamConverter.Context ctx)
Convert one or more values to the required type.
|
Object convert(com.google.inject.TypeLiteral<?> type, Object[] values, ParamConverter.Context ctx) throws Exception
Convert one or more values to the required type. If the converter doesn't support the
required type a call to ParamConverter.Context.convert(TypeLiteral, Object[])
must be done.
ParamConverter converter = (type, values, next) -> { if (type.getRawType() == MyType.class) { // convert to MyType return ...; } // no luck! move next return next.convert(type, values); }It's also possible to create generic/parameterized types too:
public class MyType<T> {} ParamConverter converter = (type, values, ctx) -> { if (type.getRawType() == MyType.class) { // Creates a new type from current generic type TypeLiterale<?> paramType = TypeLiteral .get(((ParameterizedType) toType.getType()).getActualTypeArguments()[0]); // Ask param converter to resolve the new type. Object result = next.convert(paramType, values); return new MyType(result); } // no luck! move next return ctx.convert(type, values); }
type
- Requested type.values
- Raw values. Types is one of two: String
or Upload
.ctx
- Execution context.Exception
- If conversion fails.Copyright © 2015. All rights reserved.