我有以下电话:
http://localhost:8080/report?name=HelloReport&download=true
如何在http请求的末尾添加任意输入参数?
我的代码:
...
@RequestMapping(value = "/report", params = {"name", "download"})
public ResponseEntity<byte[]> report(
@RequestParam(value = "name") String name,
@RequestParam(value = "download") boolean download) {
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.parseMediaType("application/pdf"));
if (download) {
headers.setContentDispositionFormData("inline", name + ".pdf");
}
headers.setCacheControl("must-revalidate, post-check=0, pre-check=0");
//MultiValueMap<String, String> parameters = UriComponentsBuilder.fromUriString(request.getRequestURI()).build().getQueryParams();
...
我想用:
MultiValueMap<String, String> parameters = UriComponentsBuilder.fromUriString(request.getRequestURI()).build().getQueryParams();
新的http看起来怎么样?
新的@RequestMapping和方法报告()是什么样的呢?
发布于 2017-11-03 09:23:19
您的Spring注释不允许任意查询参数。它可能会基于缺少端点所需的参数来限制对端点的访问。但不会限制对额外端点的访问。
基于这个方法,我现在可以使用这个查询/report?name=test&download=false&myfoobar=21243hjof&blablablaba=beepboopbap
来访问它。Spring不在乎我添加了myfoobar
和blablablaba
。
要访问所有内容,只需将HttpServletRequest
参数添加到您的方法签名中,顺序就无关紧要了
@RequestMapping(value = "/report", params = {"name", "download"})
public ResponseEntity<byte[]> report(
HttpServletRequest request,
@RequestParam(value = "name") String name,
@RequestParam(value = "download") boolean download) {
然后简单地抓取你的所有参数
request.getQueryString() // everything after ?, so its name=test&download=false&myfoobar=21243hjof&blablablaba=beepboopbap
也可以使用请求方法:
request.getParameter("myfoobar") // 21243hjof
或者作为你的例子你可以
MultiValueMap<String, String> parameters = UriComponentsBuilder.fromUriString("?"+request.getQueryString()).build().getQueryParams();
此外,您还可以使用@RequestParam Map<String,String> allRequestParams
获取所有参数。
@RequestMapping(value = "/report", params = {"name", "download"})
public ResponseEntity<byte[]> report(
@RequestParam Map<String,String> allRequestParams,
@RequestParam(value = "name") String name,
@RequestParam(value = "download") boolean download) {
https://stackoverflow.com/questions/47100693
复制相似问题