如何在 Spring Boot Rest API 中读取包含与号 (&) 的 @Request Param 属性值
How to read the @Request Param attribute values that contains ampersand (&) in Spring Boot Rest API
团队,
当我尝试在 spring 启动休息 api 中读取包含与号 (&) 的请求参数属性值时,我收到数字格式异常。下面是我试过的示例代码。请给我建议。
请求URL:http://loacalhost:8080/search/ad?skey="uc"&fn="M&M"
休息控制器方法:
@GetMapping(value = "/search/ad")
public ResponseEntity<List<SearchResultDTO>> findSearchResult(
@RequestParam(value="skey",required=true) String skey,
@RequestParam(value="fn",required=false,defaultValue = "null") String fn
) {
.....
}
异常是:“java.lang.NumberFormatException:对于输入字符串:“M&M”
我也尝试了以下方法:
fn="M%26M" , fn=""M%26amp;M" , fn=""M&M" 以下每种情况都是我遇到的异常。
"java.lang.NumberFormatException:对于输入字符串:"M%26M"", "M%26amp;M"" "M&M""
按照建议我在下面尝试了。
@SpringBootTest(classes = Application.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class SearchIntegrationTest {
@LocalServerPort
private int port;
@Autowired
TestRestTemplate testRestTemplate;
@Test
public void findearchResult_IntegrationTest() throws JSONException {
String url = UriComponentsBuilder.fromUriString("/search/ad").queryParam("skey", "uc")
.queryParam("pf", "C&S").encode().toUriString();
ResponseEntity<String> response = testRestTemplate.getForEntity(url, String.class);
assertEquals(HttpStatus.OK, response.getStatusCode());
}
}
错误是:java.lang.NumberFormatException:对于输入字符串:“C%26S”
发送请求时必须进行URL编码。如果您手动测试 API,则必须自己对其进行编码。
例如
http://loacalhost:8080/search/ad?skey="uc%26fn%3D%22M%26M"
否则,如果您使用 RestTemplate 来测试这个 API,那么您可以使用这样的东西:
例如
String url = UriComponentsBuilder
.fromUriString("http://loacalhost:8080/search/ad")
.queryParam("skey", "uc&fn=\"M&M").encode().toUriString();
new RestTemplate().getForEntity(url, String.class).getBody();
试试这个:
@GetMapping("/example")
public Map<String, String[]> getExample(HttpServletRequest request) {
return request.getParameterMap();
}
URI 将是:
?skey="uc"&fn="M%26M"
并且响应采用 JSON 格式
{
"skey": [
"\"uc\""
],
"fn": [
"\"M&M\""
]
}
如果您知道它的名称,您也可以使用
提取单个参数
request.getParameter("skey");
团队,
当我尝试在 spring 启动休息 api 中读取包含与号 (&) 的请求参数属性值时,我收到数字格式异常。下面是我试过的示例代码。请给我建议。
请求URL:http://loacalhost:8080/search/ad?skey="uc"&fn="M&M"
休息控制器方法:
@GetMapping(value = "/search/ad")
public ResponseEntity<List<SearchResultDTO>> findSearchResult(
@RequestParam(value="skey",required=true) String skey,
@RequestParam(value="fn",required=false,defaultValue = "null") String fn
) {
.....
}
异常是:“java.lang.NumberFormatException:对于输入字符串:“M&M”
我也尝试了以下方法:
fn="M%26M" , fn=""M%26amp;M" , fn=""M&M" 以下每种情况都是我遇到的异常。
"java.lang.NumberFormatException:对于输入字符串:"M%26M"", "M%26amp;M"" "M&M""
按照建议我在下面尝试了。
@SpringBootTest(classes = Application.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) public class SearchIntegrationTest {
@LocalServerPort
private int port;
@Autowired
TestRestTemplate testRestTemplate;
@Test
public void findearchResult_IntegrationTest() throws JSONException {
String url = UriComponentsBuilder.fromUriString("/search/ad").queryParam("skey", "uc")
.queryParam("pf", "C&S").encode().toUriString();
ResponseEntity<String> response = testRestTemplate.getForEntity(url, String.class);
assertEquals(HttpStatus.OK, response.getStatusCode());
}
}
错误是:java.lang.NumberFormatException:对于输入字符串:“C%26S”
发送请求时必须进行URL编码。如果您手动测试 API,则必须自己对其进行编码。
例如
http://loacalhost:8080/search/ad?skey="uc%26fn%3D%22M%26M"
否则,如果您使用 RestTemplate 来测试这个 API,那么您可以使用这样的东西:
例如
String url = UriComponentsBuilder
.fromUriString("http://loacalhost:8080/search/ad")
.queryParam("skey", "uc&fn=\"M&M").encode().toUriString();
new RestTemplate().getForEntity(url, String.class).getBody();
试试这个:
@GetMapping("/example")
public Map<String, String[]> getExample(HttpServletRequest request) {
return request.getParameterMap();
}
URI 将是:
?skey="uc"&fn="M%26M"
并且响应采用 JSON 格式
{
"skey": [
"\"uc\""
],
"fn": [
"\"M&M\""
]
}
如果您知道它的名称,您也可以使用
提取单个参数request.getParameter("skey");