【发布时间】:2019-04-25 14:38:28
【问题描述】:
我有一个在 Tomcat 上运行的 Spring-Boot 应用程序。在其中,我有一个带有请求参数的 RestController。
@RequestMapping(value = "/v1/test", method = RequestMethod.GET,produces = MediaType.APPLICATION_JSON_VALUE)
public String getV2LocationByName(
@RequestParam
String cityName cityName,
@RequestParam(value = LANGUAGE, defaultValue = US_ENGLISH_LOCALE) String language,
HttpServletRequest request) throws InterruptedException, ExecutionException {
--------------
---------------
System.out.println(cityName);
}
当我调试 Spring Boot 应用程序时,请求参数 cityName 被解码 即如果URL是http://localhost:8080/v1/test?cityName=Andaman%26Nicobar,它会被解码成http://localhost:8080/v1/test?cityName=Andaman&Nicobar。
但是当我写了一个spring mvc测试时:
@RunWith(SpringRunner.class)
@SpringBootTest( webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class ApplicationTest {
@LocalServerPort
private int port;
TestRestTemplate restTemplate = new TestRestTemplate();
HttpHeaders headers = new HttpHeaders();
@Test
public void testRetrieveStudentCourse() {
HttpEntity<String> entity = new HttpEntity<String>(null, headers);
ResponseEntity<String> response = restTemplate.exchange(
createURLWithPort("v1/test?cityName=Andaman%26Nicobar"),
HttpMethod.GET, entity, String.class);
}
private String createURLWithPort(String uri) {
return "http://localhost:" + port + uri;
}
当我调试这个测试和控制器时,这次没有解码cityName。为什么会这样?如何为此添加单元测试?
【问题讨论】:
标签: java spring-boot spring-mvc resttemplate urldecode