Lower case enum resource parameters

2,926 views
Skip to first unread message

James Harvey

unread,
Oct 9, 2014, 6:05:41 AM10/9/14
to dropwiz...@googlegroups.com
I think I am going crazy.

I have a PathParam that is an enum, with an uppercase value. I'd like to support the client sending a lower case value, too, eg

public enum Type {
NOW
}

Currently, unless Type is uppercase the client receives a 404.

One solution is to wrap it in an AbstractParam that does a .toLowerCase on the string, but this seems like overkill (although it does work).

I tried using an @JsonCreator on the enum itself, but DW seems to ignore this in the default config. I also started tracing through the innards of the DW Jackson project, and it seems to register a FuzzyEnumModule which should do what we need, but doesn't :)

How have others tackled this problem?

Dimas Guardado

unread,
Oct 9, 2014, 12:48:21 PM10/9/14
to dropwiz...@googlegroups.com
Hi James,

Enum Parameter types are not handled by Jackson at all. They are handled natively by Jersey according to the JAX-RS spec. The rules for how types, including enums, can be found here[1] among other places.

A lighter weight way to get case-insensitive handling of enums is to provide a fromString method on the enum that forwards an upperCase string to valueOf.

public enum Type {
    NOW;

    public static Type fromString(String name) {
        return valueOf(name.toUpperCase());
    }
}

Hope that helps,
Dimas

[1] https://jsr311.java.net/nonav/javadoc/javax/ws/rs/PathParam.html

James Harvey

unread,
Oct 13, 2014, 1:31:04 PM10/13/14
to dropwiz...@googlegroups.com
Lifesaver, thanks Dimas.

Who knew reading the spec would help! 😳

Sent from my iPhone
--
You received this message because you are subscribed to a topic in the Google Groups "dropwizard-user" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/dropwizard-user/eyPlmz36CJ0/unsubscribe.
To unsubscribe from this group and all its topics, send an email to dropwizard-us...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages