I have the following enum from a Spring Boot application and I have a problem with parsing probably.
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonValue;
@JsonFormat(shape = JsonFormat.Shape.OBJECT)
public enum Month {
JAN(0),
FEB(1),
MAR(2),
APR(3),
MAY(4),
JUN(5),
JUL(6),
AUG(7),
SEP(8),
OCT(9),
NOV(10),
DEC(11);
@JsonValue
private final int value;
Month(final int value) {
this.value = value;
}
public int toInt() {
return value;
}
public static Month fromInt(final int value) {
switch (value) {
case 0:
return JAN;
case 1:
return FEB;
case 2:
return MAR;
case 3:
return APR;
case 4:
return MAY;
case 5:
return JUN;
case 6:
return JUL;
case 7:
return AUG;
case 8:
return SEP;
case 9:
return OCT;
case 10:
return NOV;
case 11:
return DEC;
default:
return null;
}
}
public String toString() {
switch (this) {
case JAN:
return "JAN";
case FEB:
return "FEB";
case MAR:
return "MAR";
case APR:
return "APR";
case MAY:
return "MAY";
case JUN:
return "JUN";
case JUL:
return "JUL";
case AUG:
return "AUG";
case SEP:
return "SEP";
case OCT:
return "OCT";
case NOV:
return "NOV";
case DEC:
return "DEC";
default:
return null;
}
}
}
Here is entity which holding the enum:
public class Trip implements java.io.Serializable {
@JsonProperty("k1Month")
private Month k1Month;
...
}
Here is when I'm doing the save in the Controller:
@PutMapping("/Trip/Update")
public void updateTrip(@RequestBody Trip trip) {
tripService.updateTrip(trip);
}
I'm calling the Controller from Spring Boot webclient:
public static Trip saveTrip(Trip trip) {
String url = TripcalculationsServiceApplication.Trip_APP_BASE_URL;
WebClient client = WebClient.create(url);
return client.put()
.uri("Trips/Trip/Update")
.body(Mono.just(trip), Trip.class)
.retrieve()
.bodyToMono(Trip.class)
.block();
}
I receive a warning but I think it causing the webclient call to fail, here is the warning:
Resolved [org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Cannot deserialize value of type
com.mycompany.tripservice.entity.Monthfrom String "JAN": not one of the values accepted for Enum class: [11, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 0]; nested exception is com.fasterxml.jackson.databind.exc.InvalidFormatException:
The calling service throw an error 400 bad request (I'm thinking it is related to the parsing but cause only when I select the Month value the service failed).
org.springframework.web.reactive.function.client.WebClientResponseException$BadRequest: 400 Bad Request from PUT