0

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.Month from 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

1
  • The jackson test that tests for integer mapping uses Integer instead of int. Try changing the declaration of value from int to Integer and change the signature of toInt(). See github.com/limengning/jackson-databind/commit/… Commented Sep 20, 2021 at 14:39

1 Answer 1

1

Try the following (removing @JsonFormat, adding @JsonCreator and moving @JsonValue).

import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonValue;

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);
  
    private final int value;

    Month(final int value) {
        this.value = value;
    }

    @JsonValue
    public int toInt() {
        return value;
    }

    @JsonCreator
    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;
        }
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Just saw your answer, I updated but need to do testing, I'll probably accept cause I don't get exception but didn't use the month for now. will update tomorrow so tired after long day at work, this is my home project!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.