forked from f-lab-edu/NoobLoL
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'develop' into feature/f-lab-edu#61-swagger
- Loading branch information
Showing
16 changed files
with
283 additions
and
146 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package com.nooblol.global.config; | ||
|
||
import com.nooblol.global.utils.enumhandle.EnumConvertFactoryUtils; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.format.FormatterRegistry; | ||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport; | ||
|
||
@Configuration | ||
public class WebConfig extends WebMvcConfigurationSupport { | ||
|
||
@Override | ||
protected void addFormatters(FormatterRegistry registry) { | ||
registry.addConverterFactory(new EnumConvertFactoryUtils()); | ||
} | ||
} |
51 changes: 51 additions & 0 deletions
51
src/main/java/com/nooblol/global/utils/enumhandle/EnumConvertFactoryUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package com.nooblol.global.utils.enumhandle; | ||
|
||
|
||
import java.util.Arrays; | ||
import org.springframework.core.convert.converter.Converter; | ||
import org.springframework.core.convert.converter.ConverterFactory; | ||
|
||
/** | ||
* 소문자로 EnumType이 들어오는 경우 대문자로 치환해서 타입을 전달해 주기 위한 셋팅 | ||
* <p> | ||
* Reference : https://kapentaz.github.io/java/spring/Enum-and-@RequestParam-in-Spring/# | ||
*/ | ||
public class EnumConvertFactoryUtils implements | ||
ConverterFactory<String, Enum<? extends EnumConvertUtils>> { | ||
|
||
@Override | ||
public <T extends Enum<? extends EnumConvertUtils>> Converter<String, T> getConverter( | ||
Class<T> targetType) { | ||
return new StringToEnumConvertUtils<>(targetType); | ||
} | ||
|
||
private static final class StringToEnumConvertUtils<T extends Enum<? extends EnumConvertUtils>> implements | ||
Converter<String, T> { | ||
|
||
private final Class<T> enumType; | ||
private final boolean constantEnum; | ||
|
||
public StringToEnumConvertUtils(Class<T> enumType) { | ||
this.enumType = enumType; | ||
this.constantEnum = Arrays.stream(enumType.getInterfaces()) | ||
.anyMatch(i -> i == EnumConvertUtils.class); | ||
} | ||
|
||
@Override | ||
public T convert(String source) { | ||
if (source.isEmpty()) { | ||
return null; | ||
} | ||
|
||
source = source.toUpperCase(); | ||
|
||
T[] constants = enumType.getEnumConstants(); | ||
for (T c : constants) { | ||
if (constantEnum && c.name().equals(source.trim())) { | ||
return c; | ||
} | ||
} | ||
return null; | ||
} | ||
} | ||
} |
5 changes: 5 additions & 0 deletions
5
src/main/java/com/nooblol/global/utils/enumhandle/EnumConvertUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package com.nooblol.global.utils.enumhandle; | ||
|
||
public interface EnumConvertUtils { | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.