随笔记录
springmvc-@RequestParam设置是否必填参数优缺点分析
2016-11-23 diaba


通过如下代码设置参数是否必输:



@RequestParam(value = "parameterName", required = true) String parameterName







优点:简单,不用代码进行检查输入参数是否有值

缺点:如果没有填写该参数值,被springmvc直接拦截,该方法中的任何日志都不会生效,查询到日志如下








2016-11-23 11:44:47 |-DEBUG org.springframework.web.servlet.DispatcherServlet - Null ModelAndView returned to DispatcherServlet with name 'applicationContext': assuming HandlerAdapter completed request handling
2016-11-23 11:44:47 |-DEBUG org.springframework.web.servlet.DispatcherServlet - Successfully completed request
2016-11-23 11:44:47 |-DEBUG org.springframework.web.servlet.DispatcherServlet - DispatcherServlet with name 'applicationContext' processing POST request for [/bx/api/v2/ins/pay/payOrder/DDE0AC4C-906D-45AB-BB06-5D872C80D139]
2016-11-23 11:44:47 |-DEBUG o.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping - Looking up handler method for path /bx/api/v2/ins/pay/payOrder/DDE0AC4C-906D-45AB-BB06-5D872C80D139
2016-11-23 11:44:47 |-DEBUG o.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping - Returning handler method [public com.ins.model.result.FengResponse<java.util.Map<java.lang.String, java.lang.Object>> com.ins.pc.controller.pay.PayController.payOrder(javax.servlet.http.HttpServletRequest,java.lang.String,java.lang.String,java.lang.String,java.lang.String,java.lang.String,java.lang.String,java.lang.String,java.lang.String,java.lang.String,java.lang.String,java.lang.String,java.lang.String,java.lang.String,java.lang.String,java.lang.String,java.lang.String,java.lang.String,java.lang.String,java.lang.String,java.lang.String,java.lang.String,java.lang.String,java.lang.String,java.lang.String,java.lang.String)]
2016-11-23 11:44:47 |-DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Returning cached instance of singleton bean 'payController'
2016-11-23 11:44:47 |-DEBUG o.s.web.servlet.mvc.method.annotation.ExceptionHandlerExceptionResolver - Resolving exception from handler [public com.ins.model.result.FengResponse<java.util.Map<java.lang.String, java.lang.Object>> com.ins.pc.controller.pay.PayController.payOrder(javax.servlet.http.HttpServletRequest,java.lang.String,java.lang.String,java.lang.String,java.lang.String,java.lang.String,java.lang.String,java.lang.String,java.lang.String,java.lang.String,java.lang.String,java.lang.String,java.lang.String,java.lang.String,java.lang.String,java.lang.String,java.lang.String,java.lang.String,java.lang.String,java.lang.String,java.lang.String,java.lang.String,java.lang.String,java.lang.String,java.lang.String,java.lang.String)]: org.springframework.web.bind.MissingServletRequestParameterException: Required String parameter 'parameterName' is not present
2016-11-23 11:44:47 |-DEBUG org.springframework.web.servlet.mvc.annotation.ResponseStatusExceptionResolver - Resolving exception from handler [public com.ins.model.result.FengResponse<java.util.Map<java.lang.String, java.lang.Object>> com.ins.pc.controller.pay.PayController.payOrder(javax.servlet.http.HttpServletRequest,java.lang.String,java.lang.String,java.lang.String,java.lang.String,java.lang.String,java.lang.String,java.lang.String,java.lang.String,java.lang.String,java.lang.String,java.lang.String,java.lang.String,java.lang.String,java.lang.String,java.lang.String,java.lang.String,java.lang.String,java.lang.String,java.lang.String,java.lang.String,java.lang.String,java.lang.String,java.lang.String,java.lang.String,java.lang.String)]: org.springframework.web.bind.MissingServletRequestParameterException: Required String parameter 'parameterName' is not present
2016-11-23 11:44:47 |-DEBUG org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver - Resolving exception from handler [public com.ins.model.result.FengResponse<java.util.Map<java.lang.String, java.lang.Object>> com.ins.pc.controller.pay.PayController.payOrder(javax.servlet.http.HttpServletRequest,java.lang.String,java.lang.String,java.lang.String,java.lang.String,java.lang.String,java.lang.String,java.lang.String,java.lang.String,java.lang.String,java.lang.String,java.lang.String,java.lang.String,java.lang.String,java.lang.String,java.lang.String,java.lang.String,java.lang.String,java.lang.String,java.lang.String,java.lang.String,java.lang.String,java.lang.String,java.lang.String,java.lang.String,java.lang.String)]: org.springframework.web.bind.MissingServletRequestParameterException: Required String parameter 'parameterName' is not present







个人觉得,通过RequestParam标签方式设置参数是否必填,查看日志不方便,不如在方法签名层面设置为可选填写,接下来在方法内部进行参数值的检查,如果为空,则返回404错误,这样调用者得到的消息比较直观,日志追踪者查询比较方便。











发表评论:
昵称

邮件地址 (选填)

个人主页 (选填)

内容