Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to implement enumerated type passing by springboot

2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly explains "springboot how to achieve enumerated type transfer", the content of the article is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in depth, together to study and learn "springboot how to achieve enumerated type transfer" bar!

test

First, create an enumeration class:

Public enum ScoreType {TOTAL_SCORE (overall grade), MIDDLE_SCORE (midterm grade), FINAL_SCORE (final grade); String des; / / description ScoreType (String des) {this.des = des;} public String getDes () {return des;}}

Then create an enumerated api interface:

@ RestController@RequestMapping ("/ Klass") public class KlassController {@ GetMapping ("testEnum") public String testEnum (@ RequestParam ScoreType scoreType) {return "enumeration serial number:" + scoreType.ordinal () + ", enumeration name:" + scoreType.name ();}}

Test and send data using enumerated names:

Use the enumeration sequence number to send data:

Thus, in the springboot default request parameter mapping, enumerated types can only be mapped by enumerated names, but sometimes we need to use ordinal numbers for mapping.

Converter

Since Converter means conversion, we can use the defined Converter to determine the conversion between parameters to enumerated types:

Public class BaseEnumConverter implements Converter {private Map enumMap = new HashMap (); public BaseEnumConverter (Class enumType) {T [] enums = enumType.getEnumConstants (); for (T e: enums) {enumMap.put (String.valueOf (e.ordinal ()), e); enumMap.put (e.name (), e) } @ Override public T convert (String source) {T T1 = enumMap.get (source.toLowerCase ()); T T2 = enumMap.get (source.toUpperCase ()); if (T1 = = null & & T2 = = null) {throw new IllegalArgumentException ("cannot match the corresponding enumeration type");} return T1 = = null? T2: T1;}}

Analyze the code, get all the enumerated values according to the parameters of the specific enumerated class at run time, and map each enumerated value sequence and the enumerated value name to the enumerated value (saved in Map). As in the above enumerated type, the following Map will be generated:

0 = > ScoreType.TOTAL_SCORETOTAL_SCORE = > ScoreType.TOTAL_SCORE1 = > ScoreType.MIDDLE_SCOREMIDDLE_SCORE = > ScoreType.MIDDLE_SCORE2 = > ScoreType.FINAL_SCOREFINAL_SCORE = > ScoreType.FINAL_SCORE

With this Converter, you can implement foreground sequence numbers and enumerated names, both of which can be successfully mapped to enumerated types, and this Converter can be provided to the springboot through the factory pattern:

Public class BaseEnumConverterFactory implements ConverterFactory {private static final Map CONVERTERS = new HashMap (); @ Override public Converter getConverter (Class targetType) {/ / each type creates a converter Converter converter = CONVERTERS.get (targetType); if (converter = = null) {converter = new BaseEnumConverter (targetType); CONVERTERS.put (targetType, converter);} return converter } @ Configuration@EnableWebMvcpublic class WebConfig implements WebMvcConfigurer {@ Override public void addFormatters (FormatterRegistry registry) {registry.addConverterFactory (new BaseEnumConverterFactory ());}}

Test and send data using enumerated names:

Dexterity

To ensure flexibility, each enumerated type can customize the conversion method, establish an interface, convert the interface, and establish a BaseEnum interface:

Public interface BaseEnum {String [] getKeys (); / / the returned keys can be converted to BaseEnum}

The enumeration class implements this interface and defines the mapping method

Public enum ScoreType implements BaseEnum {TOTAL_SCORE (overall grade), MIDDLE_SCORE (midterm grade), FINAL_SCORE (final grade); String des; / / description ScoreType (String des) {this.des = des;} public String getDes () {return des } @ Override public String [] getKeys () {String [] s = {String.valueOf (this.ordinal ()), this.name ()}; / / order and names can be converted to enumerations, such as 0 and total_score = > ScoreType.TOTAL_SCORE return s;}}

The converter uniformly converts BaseEnum:

Public class BaseEnumConverter implements Converter {private Map enumMap = new HashMap (); public BaseEnumConverter (Class enumType) {T [] enums = enumType.getEnumConstants (); / / establish and transform for (T e: enums) {for (String key: e.getKeys ()) {enumMap.put (key, e) according to keys }} @ Override public T convert (String source) {T T1 = enumMap.get (source.toLowerCase ()); T T2 = enumMap.get (source.toUpperCase ()); if (T1 = = null & & T2 = = null) {throw new IllegalArgumentException ("cannot match the corresponding enumeration type");} return T1 = = null? T2: T1;}}

For each enumerated type, you can customize how the conversion is done through the returned keys.

Thank you for your reading, the above is the content of "how to achieve enumerated type transfer in springboot". After the study of this article, I believe you have a deeper understanding of how to achieve enumerated type transfer in springboot, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report