简体   繁体   中英

Access Java Enum fields in javascript

I'm trying to access the facility String of this enum in java script

public enum FacilityEnum {

    CAR_VALET("carValet"),
    INDOOR("indoorPark"),
    DISABLED_ACCESS("disabledAccess"),
    EV_CHARGE("evCharge"),

    private String facility;

    private FacilityEnum(String facility) {
        this.facility = facility;
    }

    public String getFacility() {
        return facility;
    }

    public void setFacility(String facility) {
        this.facility = facility;
    }



}

This enum is used in a Facility.class

@Entity
public class Facility {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private long facilityId;

    @Enumerated(EnumType.STRING)
    private FacilityEnum service;


    @ManyToMany(mappedBy = "facilities")
    @JsonBackReference("parks-facilities-services")
    private Set<Park> parks;
    }


    public FacilityEnum getService() {
        return service;
    }

    public void setService(FacilityEnum service) {
        this.service = service;
    }

which has a ManyToMany relation with Park.class.

The problem comes when i need to use the facility String in javascript

This the javascript interested part, i'm using Spring + Thymleaf

var parcheggi = JSON.parse([[${parks}]]); //my list of Parks
parcheggi.forEach(function (arrayItem) { //it's ok

            var parcheggio = arrayItem;
            var services = parcheggio.facilities; //it's ok, i get Facility objects
            var servicesDiv = '<div>';
            services.forEach(function (service){
                var s = service; //the single Facility

                servicesDiv += '<img src="/images/park_icons/facilities/' + s.service + '.png" />'
            });
            servicesDiv += '</div>';

 //rest of the code...

In this case s.service is the rough Enum (CAR_VALET, INDOOR...) if i try s.service.facility I get undefined.. I need to have carValet, indoor, disabledAccess and so on...

One way to do what you want is to configure object mapper to serialize enums using toString method. You would add the following to the object mapper configuration:

 objectMapper.enable(SerializationFeature.WRITE_ENUMS_USING_TO_STRING);

(Note that previous Jackson versions have equivalent to this property but it's different).

Then just add toString to your enum:

@Override
public String toString ()
{
    return facility;
}

You are getting undefined because your Enum can't be deserialized in JSON, you have two options here:

  • Either change the Implementation of your Enum, so it contains only the String constants and it will be correctly mapped by Jackson.

Your code would be:

public enum FacilityEnum {
    CAR_VALET,
    INDOOR,
    DISABLED_ACCESS,
    EV_CHARGE;
}
  • Or you should override the toString() method in your Enum so it can be deserialized and returned as a String .

Your code would be:

public enum FacilityEnum {

  CAR_VALET("carValet"),
    INDOOR("indoorPark"),
    DISABLED_ACCESS("disabledAccess"),
    EV_CHARGE("evCharge"),

    private String facility;

  private FacilityEnum(String facility) {
    this.facility = facility;
  }

  public String getFacility() {
    return facility;
  }

  public void setFacility(String facility) {
    this.facility = facility;
  }

  @Override
  public String toString() {
    return facility;
  }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM