Unusual JSON after passing an Enum using Thymeleaf with spring model

Viewed 561

This is my object Policy, which has an Enum "effect"

@Entity
public class Policy {

    @Id
    @GeneratedValue
    @Column(name = "id")
    private Integer id;

    @Enumerated(EnumType.STRING)
    @Column(name="effect")
    private Effect effect;

    public Policy() {}

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public Effect getEffect() {
        return effect;
    }

    public void setEffect(Effect effect) {
        this.effect = effect;
    }

This is what that Enum Effect looks like:

public enum Effect {
    allow,
    deny
}

This is my Controller that passes that Enum "effect" with spring model

@RequestMapping("/modifyPolicy/{id}")
public String modifyPolicy(@PathVariable Integer id, Model model) {
    model.addAttribute("enumEffect", policyService.getPolicy(id).getEffect());
    return "modifyPolicy";
}

I access the Enum "effect" by seting up a JavaScript variable by using Thymeleaf

<!DOCTYPE html>
<html...>
...
...
    <script th:inline="javascript">
    /*<![CDATA[*/
    ...

    var someEnum = /*[[${enumEffect}]]*/;
    alert(JSON.stringify(enumEffect));

    ...
     /*]]>*/
    </script>
</html>

Unfortunately, this is delivering me following JSON which is unusual:

{"$type":"Effect","$name":"deny"}

I need to get a JSON like this, which should be the normal case:

{"Effect":"deny"}

How can I achieve this without making it complicated with custom JsonSerializer or JsonDeserializer? There needs to be a simple way...

0 Answers
Related