Team,
When i try to read the Request Param attribute values that contains ampersand (&) in spring Boot rest api i am getting Number Format Exception. Below is sample code which i tried. Please Suggest me on this.
Request URL : http://loacalhost:8080/search/ad?skey="uc"&fn="M&M"
Rest Controller method:
@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
) {
.....
}
Exception is : "java.lang.NumberFormatException: For input string: "M&M""
I tried below ways also :
fn="M%26M" , fn=""M%26amp;M" , fn=""M&M" in each case below was the exception i am getting.
"java.lang.NumberFormatException: For input string: "M%26M"", "M%26amp;M"" "M&M""
As suggested i tried below .
@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());
}
}
Error is: java.lang.NumberFormatException: For input string: "C%26S"