-
Notifications
You must be signed in to change notification settings - Fork 16
π 4λ¨κ³ - Controller λ©μλ μΈμ λ§€ν #61
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: woo-yu
Are you sure you want to change the base?
Conversation
- build.gradleμ μ»΄νμΌ μΈμλ‘ -parameters μΆκ°
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
μ€λλ§μ
λλ€ μ°νμ§λ!
λκΉμ§ ν¬κΈ°νμ§ μκ³ κ³μ μ§νν΄μ£Όμ
μ κ°μ¬ν©λλ€.
λ§μ§λ§ λ¨κ³λΌμ μΈμΈνκ² νκ³ λ νΌλλ°±μ λ¨κ²¨λ³΄μμ΅λλ€.
λ§μ½ νΌλλ°±μ λ°μνκΈ°μ μ§λμΉκ² μμ΄ λ§λ€ μΆμΌλ©΄ κ³Όκ°νκ² μ¬κΈ°κΉμ§ λ―Έμ
μ§ννκ² λ€κ³ λ§μμ£ΌμΈμ!
public class PathPatternUtil { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
public class PathPatternUtil { | |
public final class PathPatternUtil { | |
private PathPatternUtil() {} |
μ λ μ νΈμ± ν΄λμ€μ κ²½μ° μΈμ€ν΄μ€μ μμ±μ΄ λΆκ°λ₯νλλ‘,
μμ±μλ₯Ό private μΌλ‘ μ μΈν΄μ£Όλ κ²μ μ’μν©λλ€.
public static void add(TypedParser... parsers) { | ||
parserList.addAll(List.of(parsers)); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
μ λ©μλλ μ΄λμ μ°μ΄λμ§, λ¬΄μ¨ μλλ‘ λ§λ κ²μΈμ§ κΆκΈν΄μ μ¬μμ΄λ΄
λλ€.
λ°νμ μ€μ TypedParser κ° μΆκ°λλ κ²½μ°κ° μμμ΄ κ°μ§λ₯Ό μμμμ.
public class TypedParser { | ||
protected static final TypedParser STRING = new TypedParser(String.class, (s) -> s); | ||
protected static final TypedParser INT = new TypedParser(int.class, Integer::parseInt); | ||
protected static final TypedParser WRAPPER_INTEGER = new TypedParser(Integer.class, Integer::parseInt); | ||
protected static final TypedParser LONG = new TypedParser(long.class, Long::parseLong); | ||
protected static final TypedParser WRAPPER_LONG = new TypedParser(Long.class, Integer::parseInt); | ||
protected static final TypedParser BOOLEAN = new TypedParser(boolean.class, Boolean::parseBoolean); | ||
protected static final TypedParser WRAPPER_BOOLEAN = new TypedParser(Boolean.class, Boolean::parseBoolean); | ||
protected static final TypedParser SHORT = new TypedParser(short.class, Short::parseShort); | ||
protected static final TypedParser WRAPPER_SHORT = new TypedParser(Short.class, Short::parseShort); | ||
protected static final TypedParser FLOAT = new TypedParser(float.class, Float::parseFloat); | ||
protected static final TypedParser WRAPPER_FLOAT = new TypedParser(Float.class, Float::parseFloat); | ||
protected static final TypedParser DOUBLE = new TypedParser(double.class, Double::parseDouble); | ||
protected static final TypedParser WRAPPER_DOUBLE = new TypedParser(Double.class, Double::parseDouble); | ||
protected static final TypedParser CHAR = new TypedParser(char.class, s -> s.charAt(0)); | ||
protected static final TypedParser WRAPPER_CHAR = new TypedParser(Character.class, s -> s.charAt(0)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
TypedParser λ΄λΆμ μ μΈλ static μμλ€μ TypedParsers μμλ§ μ°μ΄λ λ―νλ°,
κ΅³μ΄ μμλͺ
κΉμ§ νλνλ μ§μ΄κ°λ©° protected νκ² κ³΅κ°λ₯Ό ν νμκ° μμμ§ κΆκΈν©λλ€.
TypedParsers ν΄λμ€ μμμ μ΅λͺ μΌλ‘ μ μΈν μλ μμ κ² κ°μμμ.
public static Object parse(final Class<?> type, final String value) { | ||
return parserList.stream().filter(parser -> parser.canParse(type)).findFirst() | ||
.orElseThrow(IllegalArgumentException::new) | ||
.parse(value); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
νμ¬ TypeParser λ₯Ό μ°Ύμ λ List μμ findFirst λ₯Ό νλλ°,
μΌκΈμ»¬λ μ
μ νμ©νκ³ μΆμΌμ
¨λ€κ³ μλλ₯Ό μ΄ν΄νμ΅λλ€.
μΌκΈμ»¬λ μ
κ°μ²΄μ λν ν
μ€νΈ μ½λ μμ±λ κ°μ΄ ν΄μ£Όμλ©΄ μ΄λ¨κΉ μ μν΄λ΄
λλ€.
public boolean matches(String path) { | ||
return pattern.matcher(path).matches(); | ||
} | ||
|
||
public Map<String, String> extractUriVariables(String path) { | ||
var matcher = pattern.matcher(path); | ||
if (!matcher.matches()) { | ||
return Collections.emptyMap(); | ||
} | ||
Map<String, String> variables = new HashMap<>(); | ||
for (int i = 0; i < variableNames.size(); i++) { | ||
variables.put(variableNames.get(i), matcher.group(i + 1)); | ||
} | ||
return variables; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
PathPatternUtil μ λν ν μ€νΈλ μμ±ν΄μ£Όμ ¨μ§λ§, μ μ PathPatternParser κ°μ²΄μ λν΄μλ ν μ€νΈ μμ±ν΄μ£Όμμ§ μμλλ° λ³΄μ λΆνλ립λλ€.
public interface ParameterParser { | ||
Object parse(final Method method, final Parameter parameter, final HttpServletRequest request); | ||
|
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ParameterParser λ₯Ό μΈν°νμ΄μ€λ‘ μ μν΄μ£Όμκ³ ,
μ΄μ λν 3κ°μ§ ꡬν체λ₯Ό λ§λ€μ΄μ£Όμ μ μ’μ΅λλ€. π
(ParameterTypedParser, PathVariableParser, QueryParamParser)
public class ParameterParsers { | ||
|
||
private final List<ParameterParser> parsers = new ArrayList<>(Arrays.asList(new PathVariableParser(), new QueryParamParser())); | ||
|
||
public ParameterParsers() { | ||
|
||
} | ||
|
||
public ParameterParsers(ParameterParser... parsers) { | ||
this.parsers.addAll(Arrays.asList(parsers)); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ParameterParsers μΌκΈμ»¬λ μ
μ λν΄νΈλ‘ PathVariableParser μ QueryParamParser λ₯Ό κ°μ§κ³ ,
νλΌλ―Έν°λ‘ λ겨λ°μ parsers λ₯Ό μΆκ°λ‘ List μ μΆκ°λ₯Ό νλ κ΅°μ
@@ -18,8 +21,9 @@ public class DispatcherServlet extends HttpServlet { | |||
private final HandlerAdapterRegistry handlerAdapters = new HandlerAdapterRegistry(); | |||
|
|||
public DispatcherServlet() { | |||
requestHandlers.addHandlerMapping(new ControllerHandlerMapping(BASE_PACKAGE)); | |||
exceptionHandlers.addHandlerMapping(new ControllerAdviceHandlerMapping(BASE_PACKAGE)); | |||
var parameterParsers = new ParameterParsers(new PathVariableParser(), new QueryParamParser()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
μ΄λ―Έ λν΄νΈλ‘ PathVariableParser μ QueryParamParser λ₯Ό
ParameterParsers κ° κ°μ§κ³ μλλ°,
λ€μ νλΌλ―Έν°λ‘ λ겨주μ΄μ addAll μ κ΅³μ΄ νλ μ΄μ κ° λ¬΄μμΈκ°μ?
|
||
private ControllerHandlerMapping handlerMapping; | ||
private HttpRequestHandlers handlers = new HttpRequestHandlers(); | ||
private ParameterParsers parsers = new ParameterParsers(new ParameterTypedParser(TestUser.class)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ParameterTypedParser μ κ²½μ° μ¬μ©λ²μ΄,
μ΄ν리μΌμ΄μ
κ°λ°μκ° μμ μ΄ λ§λ ν΄λμ€ μ 보λ₯Ό ParameterTypedParser μΈμ€ν΄μ€λ‘ λ§λ€μ΄μ ParameterParsers μ λ겨주μ΄μΌνλ€κ³ μ΄ν΄νμ΅λλ€.
μ΄ν리μΌμ΄μ κ°λ°μμκ² νλ μμν¬μ μ‘°μμ μ§λμΉκ² λ§μ΄ λ§‘κΈ°λ κ²μ μλκ° νλ μκ°μ΄ λλλ€.
@RequestMapping(value = "/users/bean", method = RequestMethod.POST) | ||
public ModelAndView create_javabean(TestUser testUser) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
컨νΈλ‘€λ¬ λ©μλμ νλΌλ―Έν° νμ
ν΄λμ€ μ 보λ₯Ό μ§μ μ½μ΄μμ,
μ΄ν리μΌμ΄μ
κ°λ°μκ° μλ νλ μμν¬ μ°¨μμμ new ParameterTypedParser(TestUser.class)
μΈμ€ν΄μ€λ₯Ό λ§λ€ μ μλλ‘ λΆνλ립λλ€.
νμ¬λ νλ μμν¬μμ ParameterTypedParser μΈμ€ν΄μ€κ° μμ±λκ³ λ±λ‘λλ λ‘μ§μ΄ 미ꡬνλμ΄ μλ€λ λλμ λ°μμ΅λλ€.
μλ νμΈμ μ μλ!
리뷰 μμ²μ΄ λ§μ΄ λ¦μ΄μ Έμ μ£μ‘ν©λλ€ πββοΈ
λ§μ§λ§ μ€ν λ μ λΆνλλ¦¬κ² μ΅λλ€!