我有以下电话:
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和方法报告()如何显示?
答案 0 :(得分:1)
你的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) {