使用@RequestBody将JSON转换为枚举类型 [英] Converting JSON to Enum type with @RequestBody

查看:1547
本文介绍了使用@RequestBody将JSON转换为枚举类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个主枚举类,它本质上是一类对象的类定义。例如,它看起来像下面的例子:

  public enum ColorDefinition 
{
private String abbrev;
私人字符串颜色;
private Class colorClass;
RED(RD,Red,Red.class),
GREEN(GN,Green,Green.class),
BLUE(BL,Blue ,Blue.class)....
}

我正在设置来自Javascript模型的帖子请求,它在主体中发送映射,例如
{Red:255,Green:0,Blue:0}


对于使用

  @RequestMapping(value =v1 / color / EnableColors,方法= RequestMethod.POST)
@ResponseBody
public ResponseObject enableColors(@RequestBody Map< ColorDefinition,Integer> colorMapping)

我收到以下错误消息:_
无法从StringRed构造ColorDefinition类型的Map键:不是有效的表示形式:无法构造类型的Map键来自字符串的ColorDefinitionRed:枚举类的值不是一个类别_
我在这里做错什么?在枚举类中是否需要其他方法来正确转换传入的枚举值?应该使用枚举中的另一个值(我已经尝试了没有成功)?任何帮助都不胜感激,似乎这样可以自动转换传入的值,我只是无法想象出来!

解决方案

错误消息说明出了什么问题:在 ColorDefinition 内的 Red 没有定义。案件需要匹配;枚举值区分大小写。您的JSON中的键值需要 RED GREEN BLUE



在内部,Spring使用 valueOf 获取String的枚举表示形式。在反序列化期间, ColorDefinition.valueOf(Red)将抛出 IllegalArgumentException ,因为 Red in ColorDefinition 。这个异常被Spring拦截,这就是为什么你看到一个错误消息。然而, ColorDefinition.valueOf(RED)将返回 ColorDefinition.RED ,因为 RED ColorDefinition


I have a master enum class which is essentially a class definition for a type of object. For example it looks something like the example below:

public enum ColorDefinition
{
     private String abbrev;
     private String color;
     private Class colorClass;
     RED("RD", "Red", Red.class),
     GREEN("GN", "Green", Green.class),
     BLUE("BL", "Blue", Blue.class)....
}

I am trying to set up a post request from a Javascript model, that sends a mapping in the body such as
{Red : 255, Green : 0, Blue: 0}

To a spring controlled endpoint that uses

@RequestMapping(value = "v1/color/EnableColors", method = RequestMethod.POST)
@ResponseBody
public ResponseObject enableColors(@RequestBody Map<ColorDefinition, Integer> colorMapping)

To which I get the following error message:
Can not construct Map key of type ColorDefinition from String "Red": not a valid representation: Can not construct Map key of type ColorDefinition from String "Red": not one of values for Enum class

What am I doing wrong here? Do I need some other method in the enum class to properly convert the incoming enum value? Should it be using another value from the enum (I have tried them with no success)? Any help is appreciated, it seems like this should be possible to automatically convert the incoming values, I just can't figure it out!

解决方案

The error message explains what is going wrong: there is no definition for Red inside ColorDefinition. The case needs to match; enum values are case-sensitive. The keys in your JSON need to be RED, GREEN, and BLUE.

Internally, Spring uses valueOf to get the enum representation of the String. During deserialization, ColorDefinition.valueOf("Red") will throw an IllegalArgumentException because there is no definition for Red in ColorDefinition. This exception is intercepted by Spring, and this is why you see an error message. However, ColorDefinition.valueOf("RED") will return ColorDefinition.RED because there is a definition for RED in ColorDefinition.

这篇关于使用@RequestBody将JSON转换为枚举类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆