Now this is possible when registering a custom Converter.Factory , which overrides the stringConverter method, which is called when parameters are resolved. the Github issue that @William mentioned 2 years ago does not seem to have been updated since the support was added.
Javadoc Method:
Returns the converter for converting the type to String, or null if the type cannot be processed by this factory. This is used to create converters for the types specified by @Field, @FieldMap, @Header, @HeaderMap, @Path, @Query and @QueryMap.
In the example below, the delegates are for Gson, but you can also apply any type of conversion to the parameters.
Example: GsonStringConverterFactory
class GsonStringConverterFactory extends Converter.Factory { private final transient Gson gson; GsonStringConverterFactory(final Gson gson) { this.gson = gson; } @Override public Converter<?, String> stringConverter(final Type type, final Annotation[] annotations, final Retrofit retrofit) { final TypeAdapter typeAdapter; typeAdapter = gson.getAdapter(TypeToken.get(type)); return new StringConverter<>(typeAdapter); } private static class StringConverter<T> implements Converter<T, String> { private final TypeAdapter<T> typeAdapter; private StringConverter(final TypeAdapter<T> typeAdapter) { this.typeAdapter = typeAdapter; } @Override public String convert(final T value) throws IOException { final String jsonValue; jsonValue = typeAdapter.toJson(value)); if (jsonValue.startsWith("\"") && jsonValue.endsWith("\"") { return jsonValue.substring(1, jsonValue.length() - 1); } else { return jsonValue; } } } }
Converter registration:
To register a custom converter, your Retrofit constructor might look something like this:
new Retrofit.Builder().baseUrl(BASE_URL) .addConverterFactory(GsonConverterFactory.create(gson)) .addConverterFactory(new GsonStringConverterFactory(gson)) .build();
source share