I'm refactoring a legacy codebase to use Feign and I have some problems:
No one Feign log statements are printed to console
Feign is not encoding my reservationSearch parameter
POST
the toString
of the Object
Feign definition:
public interface EPCReservationClient {
@RequestLine("POST v1/searchReservations")
@Headers({"clientId: {clientId}", "Content-Type: application/json",
"Authentication: {authentication}"})
@Body("{reservationSearch}")
ReservationSearchResponse reservationSearch(
@Param("authentication") String authentication, //apiToken
@Param("clientId") String clientId,
@Param("reservationSearch")
ReservationSearch<ReservationSearchParameters> reservationSearch);
}
Feign instantiation
Feign.builder()
.logger(new Logger.JavaLogger())
.decoder(new JacksonDecoder())
.encoder(new JacksonEncoder(new ObjectMapper().findAndRegisterModules()))
.logLevel(Logger.Level.FULL)
.target(EPCReservationClient.class, restUrl);
Param type (don't kill me, not my code):
public class ReservationSearch<T> {
@JsonProperty("hotelID")
private final int hotelID;
private final String languageId;
@JsonProperty("reservationSearchParameters")
private final T parameters;
@JsonProperty("reservationID")
private final List<Long> reservationID;
public ReservationSearch(int hotelID, T parameters, final List<Long> reservationID) {
this.hotelID = hotelID;
this.languageId = "1033";
this.parameters = parameters;
this.reservationID = Optional.ofNullable(reservationID)
.orElseGet(Collections::emptyList);
}
public static ReservationSearch<ReservationSearchParameters> forLastName(
int maxRecords, int hotelId, String lastName) {
return new ReservationSearch<>(hotelId, new ReservationSearchParameters(maxRecords, lastName, null), null);
}
public static ReservationSearch<ReservationSearchParameters> forConfirmationNumber(
int maxRecords, int hotelId, String number) {
return new ReservationSearch<>(hotelId, new ReservationSearchParameters(maxRecords, null, number), null);
}
public static ReservationSearch<ReservationSearchParameters> forReservationId(
final int maxRecords,
final int hotelId,
final String reservationNumber) {
ReservationSearch<ReservationSearchParameters> reservationSearchParametersReservationSearch = new ReservationSearch<>(
hotelId,
new ReservationSearchParameters(
maxRecords,
null,
null),
Collections.singletonList(Long.valueOf(reservationNumber)));
return reservationSearchParametersReservationSearch;
}
public int getHotelID() {
return hotelID;
}
public String getLanguageId() {
return languageId;
}
public T getParameters() {
return parameters;
}
public List<Long> getReservationID() {
return reservationID;
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
final ReservationSearch that = (ReservationSearch) o;
return new EqualsBuilder()
.append(hotelID, that.hotelID)
.append(languageId, that.languageId)
.append(parameters, that.parameters)
.append(reservationID, that.reservationID)
.isEquals();
}
@Override
public int hashCode() {
return new HashCodeBuilder(17, 37)
.append(hotelID)
.append(languageId)
.append(parameters)
.append(reservationID)
.toHashCode();
}
}
ReservationSearchParameters :
public class ReservationSearchParameters {
private final String travelerLastName;
private final String confirmationNumber;
private final int maxRecordsLimit;
public ReservationSearchParameters(int maxRecordsLimit, String travelerLastName, String confirmationNumber) {
this.maxRecordsLimit = maxRecordsLimit;
this.travelerLastName = travelerLastName;
this.confirmationNumber = confirmationNumber;
}
public String getTravelerLastName() { return travelerLastName; }
public String getConfirmationNumber() { return confirmationNumber; }
public int getMaxRecordsLimit() { return maxRecordsLimit; }
}
Header (@Headers
and @HeaderMap
) and body template (@Body
) params are always considered pre-encoded. Only query params (@QueryMap
and @Param
s that reference a query parameter) will be URL encoded.
If you want your body object to be encoded via the Encoder
, then don't supply any @Body
template, and don't annotate your body parameter with @Param
(that is only intended to be used for template parameters). The un-annotated body parameter will then be fed to the Encoder
specified in your Feign.builder().encoder(new FooEncoder())
line.