diff --git a/src/main/java/com/nexacro/spring/NexacroException.java b/src/main/java/com/nexacro/spring/NexacroException.java index 7e6f0d9..b962413 100644 --- a/src/main/java/com/nexacro/spring/NexacroException.java +++ b/src/main/java/com/nexacro/spring/NexacroException.java @@ -24,7 +24,7 @@ public class NexacroException extends Exception { public static final String DEFAULT_MESSAGE = "An Error Occured. check the ErrorCode for detail of error infomation."; private int errorCode = DEFAULT_ERROR_CODE; - private String errorMsg = DEFAULT_MESSAGE; + private String errorMsg; /** * 기본 생성자이다. diff --git a/src/main/java/com/nexacro/spring/annotation/ParamDataSet.java b/src/main/java/com/nexacro/spring/annotation/ParamDataSet.java index ca0bee3..659cbd4 100644 --- a/src/main/java/com/nexacro/spring/annotation/ParamDataSet.java +++ b/src/main/java/com/nexacro/spring/annotation/ParamDataSet.java @@ -5,8 +5,6 @@ import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; -import com.nexacro.spring.context.NexacroContextHolder; - /** *

DataSet을 List혹은 POJO형태의 데이터로 변환을 수행하기 위한 annotation이다. * @@ -17,15 +15,23 @@ import com.nexacro.spring.context.NexacroContextHolder; * @author Park SeongMin * @since 07.28.2015 * @version 1.0 - * @see NexacroContextHolder + * @see ParamVariable */ @Target({ java.lang.annotation.ElementType.PARAMETER }) @Retention(RetentionPolicy.RUNTIME) @Documented public @interface ParamDataSet { /** - * 데이터셋의 식별자 + * DataSet의 식별자 * @return dsName */ - public abstract String name(); + String name(); + + /** + * Whether the parameter is required. + *

Default is {@code true}, leading to an exception thrown in case + * of the parameter missing in the request. Switch this to {@code false} + * if you prefer a {@code null} in case of the parameter missing. + */ + boolean required() default true; } \ No newline at end of file diff --git a/src/main/java/com/nexacro/spring/annotation/ParamVariable.java b/src/main/java/com/nexacro/spring/annotation/ParamVariable.java index f610edc..a2202ec 100644 --- a/src/main/java/com/nexacro/spring/annotation/ParamVariable.java +++ b/src/main/java/com/nexacro/spring/annotation/ParamVariable.java @@ -5,8 +5,6 @@ import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; -import com.nexacro.spring.context.NexacroContextHolder; - /** *

Variable을 Primitive 형태의 데이터로 변환을 수행하기 위한 annotation이다. * @@ -17,11 +15,24 @@ import com.nexacro.spring.context.NexacroContextHolder; * @author Park SeongMin * @since 07.28.2015 * @version 1.0 - * @see NexacroContextHolder + * @see ParamDataSet */ @Target({ java.lang.annotation.ElementType.PARAMETER }) @Retention(RetentionPolicy.RUNTIME) @Documented public @interface ParamVariable { - public abstract String name(); + + /** + * Variable식별자 + * @return dsName + */ + String name(); + + /** + * Whether the parameter is required. + *

Default is {@code true}, leading to an exception thrown in case + * of the parameter missing in the request. Switch this to {@code false} + * if you prefer a {@code null} in case of the parameter missing. + */ + boolean required() default true; } \ No newline at end of file diff --git a/src/main/java/com/nexacro/spring/resolve/NexacroMethodArgumentResolver.java b/src/main/java/com/nexacro/spring/resolve/NexacroMethodArgumentResolver.java index caae50b..4109dc3 100644 --- a/src/main/java/com/nexacro/spring/resolve/NexacroMethodArgumentResolver.java +++ b/src/main/java/com/nexacro/spring/resolve/NexacroMethodArgumentResolver.java @@ -6,6 +6,7 @@ import java.util.HashMap; import java.util.List; import java.util.Map; +import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; @@ -13,6 +14,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.core.MethodParameter; import org.springframework.util.StopWatch; +import org.springframework.web.bind.MissingServletRequestParameterException; import org.springframework.web.bind.support.WebDataBinderFactory; import org.springframework.web.context.request.NativeWebRequest; import org.springframework.web.context.request.RequestAttributes; @@ -275,9 +277,12 @@ public class NexacroMethodArgumentResolver implements HandlerMethodArgumentResol String dsName = paramDataSet.name(); DataSet dataSet = nexacroCachedData.getPlatformData().getDataSet(dsName); if(dataSet == null) { - if(logger.isDebugEnabled()) { + if(logger.isDebugEnabled()) { logger.debug("@ParamDataSet '" + dsName + "' argument is null."); } + if (paramDataSet.required()) { + handleMissingValue(paramDataSet.name(), param); + } return null; } @@ -330,6 +335,9 @@ public class NexacroMethodArgumentResolver implements HandlerMethodArgumentResol if(logger.isDebugEnabled()) { logger.debug("@ParamVariable '" + varName + "' argument is null."); } + if (paramVariable.required()) { + handleMissingValue(paramVariable.name(), param); + } return null; //throw new IllegalArgumentException("invalid @ParamVariable. ex)@ParamVariable(name=\"variableName\") Object var"); } @@ -382,6 +390,10 @@ public class NexacroMethodArgumentResolver implements HandlerMethodArgumentResol return null; } + protected void handleMissingValue(String name, MethodParameter parameter) throws NexacroConvertException { + throw new MissingNexacroParameterException(name, parameter.getParameterType().getSimpleName()); + } + private Object getAttribute(NativeWebRequest nativeWebRequest, String attrName) { return nativeWebRequest.getAttribute(attrName, RequestAttributes.SCOPE_REQUEST); }