当涉及到通用响应时,总是很难让 Feign 返回通用响应,您唯一的选择是定义字符串响应,然后使用 mapstruct 映射它:
@RequestLine("GET /objects/{type}/{model_id}")
String getObject(@Param("type") String theObjectType, @Param("model_id") String theModelId);
然后在一个实用程序类中定义,如:
public final class JsonUtils {
private JsonUtils() {
}
@SneakyThrows
public static <T> T jsonToObject(final String jsonString, final Class<T> clazz) {
final ObjectMapper objectMapper = buildObjectMapper();
return objectMapper.readValue(jsonString, clazz);
}
public static ObjectMapper buildObjectMapper() {
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
objectMapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
objectMapper.configure(DeserializationFeature.FAIL_ON_IGNORED_PROPERTIES, false);
objectMapper.registerModule(new ParameterNamesModule());
objectMapper.registerModule(new Jdk8Module());
objectMapper.registerModule(new JavaTimeModule());
return objectMapper;
}
}