【问题标题】:Springboot : Autodecoding does not work with TestRestTemplateSpring Boot:自动编码不适用于 TestRestTemplate
【发布时间】: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


    【解决方案1】:

    解决了这个问题

    @RunWith(SpringRunner.class)
    @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
    public class WeChatOauthControllerTest {
    
     @LocalServerPort
        private int port;
    
        TestRestTemplate restTemplate = new TestRestTemplate();
    
      @Test
        public void testRetrieveStudentCourse(){
            ResponseEntity<String> response = 
            restTemplate.exchange(createURI("%26"),HttpMethod.GET, null, String.class);
            assertEquals(response.getStatusCode(), HttpStatus.OK);
        }
    
      private URI createURI(String param){
            URI uri = null;
            String url = "http://localhost:"+ port +"/v1/test?query=" + param;
            try {
                uri = new URI(url);
            } catch (URISyntaxException e) {
               log.error(e.getMessage());
            }
            return uri;
        }
    

    【讨论】:

      猜你喜欢
      • 2017-07-11
      • 1970-01-01
      • 1970-01-01
      • 2020-02-23
      • 2020-06-22
      • 2017-01-21
      • 1970-01-01
      • 2016-11-25
      • 2017-05-24
      相关资源
      最近更新 更多