【问题标题】:How to print out URL when @RequestParam throws MissingServletRequestParameterException当@RequestParam 抛出 MissingServletRequestParameterException 时如何打印出 URL
【发布时间】:2015-07-26 22:14:11
【问题描述】:

这是一个春季项目。我使用下面的代码来处理传入的 http GET 请求,格式为 http://xxx.comm/vastTracking?rtbProvider=AAA&adGroupKey=BBB&transactionId=CCC&event=DDD 。 当服务器运行时,大多数请求都得到了正确处理,但也有一些抛出异常——大部分是

unhandled exception
org.springframework.web.bind.MissingServletRequestParameterException: Required VastEventType parameter 'event' is not present.

在极少数情况下,它们是“无法将字符串转换为 VastEventType 类型”或“未提供参数 adGroupKey”。

看起来传入的请求有时会修剪 URL。请求 URL 由我的系统提供,因此不应丢失,尽管它是由用户的视频播放器发送的。 我想知道这是怎么发生的,所以我想要一种在抛出异常时打印出 URL 的方法。到目前为止,我只是找到了打印所有传入 URL 的方法。

我的代码:

@Controller
public class VastEventTrackingController {
  @RequestMapping(method = RequestMethod.GET, value = "/vastTracking")
  public ResponseEntity<String> vastEventTrack( @RequestParam("rtbProvider") String strRtbProvider,
                                                @RequestParam("adGroupKey") String strAdGroupKey,
                                                @RequestParam("transactionId") String strTransactionId,
                                                @RequestParam("event") VastEventType event,
                                                HttpServletRequest request) {
    ...
  }
..
}

【问题讨论】:

    标签: java spring url


    【解决方案1】:

    默认情况下,所有请求参数都是required。见春天doc。如果你想让它们成为可选的,你应该设置required=false

    @RequestParam(value = "event",required = false) VastEventType event
    

    【讨论】:

    • 感谢回复,此参数为必填项。请求 URL 由我的系统提供,因此不应丢失。
    • 是的,但你正在传递 String 并期待 VastEventType 我不明白
    • VastEventType 是一个枚举并且能够从字符串转换。我的麻烦是缺少这个 String 参数,所以我想查看整个 URL 字符串以了解发生了什么。
    猜你喜欢
    • 1970-01-01
    • 2023-04-05
    • 2018-10-31
    • 2016-02-17
    • 2019-10-03
    • 2014-08-01
    • 2016-04-07
    • 2013-01-25
    • 2017-09-07
    相关资源
    最近更新 更多