How to manually map Enum fields in JAX-RS

后端 未结 2 961
遥遥无期
遥遥无期 2021-02-13 09:29

How can I map a simple JSON object {\"status\" : \"successful\"} automaticly map to my Java Enum within JAX-RS?

public enum Status {
    SUCESSFUL          


        
相关标签:
2条回答
  • 2021-02-13 09:54

    This might help you

    @Entity
    public class Process {
    
      private State state;
    
      public enum State {
        RUNNING("running"), STOPPED("stopped"), PAUSED("paused");
    
        private String value;
    
        private State(String value) {
          this.value = value;
        }
    
        @JsonValue
        public String getValue() {
          return this.value;
        }
    
        @JsonCreator
        public static State create(String val) {
          State[] states = State.values();
          for (State state : states) {
            if (state.getValue().equalsIgnoreCase(val)) {
              return state;
            }
          }
          return STOPPED;
        }
      }
    }
    
    0 讨论(0)
  • 2021-02-13 09:59

    The following JAXB annotations should do it. (I tested using Jettison but I've not tried other providers):

    @XmlType(name = "status")
    @XmlEnum
    public enum Status {
        @XmlEnumValue(value = "successful")
        SUCESSFUL, 
        @XmlEnumValue(value = "error")
        ERROR;
    }
    
    0 讨论(0)
提交回复
热议问题