在客户端获取Enum值

时间:2016-03-24 13:48:33

标签: angularjs spring spring-mvc enums

我正在尝试将枚举的值发送给Angularjs 服务器端:

型号:

public class Consultant implements Serializable {
    @Enumerated(EnumType.STRING)
    @Column(name = "etat_qualification")
    private EtatQualification etatQualification; 

    public EtatQualification getEtatQualification() {
        return etatQualification;
    }

    public void setEtatQualification(EtatQualification etatQualification) {
        this.etatQualification = etatQualification;
    }
}

public enum EtatQualification {
    EN_COURS("en cours"),
    OK("ok"),
    CONSULTANT_TV("consultant TV"),
    KO("ko");

    private String libelle;
    EtatQualification(String libelle) {
        this.libelle = libelle;

    }
    public String getLibelle() {
        return libelle;
    }    
}

我的资源:

@RequestMapping(value = "/consultants/{id}",
        method = RequestMethod.GET,
        produces = MediaType.APPLICATION_JSON_VALUE)
    @Timed
    public ResponseEntity<Consultant> getConsultant(@PathVariable Long id) {
        log.debug("REST request to get Consultant : {}", id);
        Consultant consultant = consultantRepository.findOne(id);
        return Optional.ofNullable(consultant)
            .map(result -> new ResponseEntity<>(
                result,
                HttpStatus.OK))
            .orElse(new ResponseEntity<>(HttpStatus.NOT_FOUND));
    }

在AngularJS方面:

var consultant = Consultant.get({id : $stateParams.id});

但是当我看顾问.etatQualification值时,我得到'EN_COURS' 我想得到:'en cours'

有可能吗?

感谢。

1 个答案:

答案 0 :(得分:1)

如果你使用jackson作为json解析器使用这个

@JsonFormat(shape = JsonFormat.Shape.OBJECT)
public enum EtatQualification { ... }