@Test
  public void test() {
    UriComponents uriComponentsBuilder =
        UriComponentsBuilder.fromUriString("http://localhost/{userId}/{age}")
            .build()
            .expand("lng1982", 32)
            .encode();

    String test = uriComponentsBuilder.toString();
    assertThat(test, is("http://localhost/lng1982/32"));
  }
Esempio n. 2
0
  // @Test
  public void testURI() throws Exception {

    UriComponents uriComponents =
        UriComponentsBuilder.newInstance()
            .path("/board/read")
            .queryParam("bno", 12)
            .queryParam("perPageNum", 20)
            .build();

    logger.info("/board/read?bno=12&perPageNum=20");
    logger.info(uriComponents.toString());
  }
  public GoogleSearchResponse search(String searchString, String referer) {
    try {
      if (ValidatorUtil.isNull(referer)) {
        referer = "http://longfalcon.net";
      }

      String v = "1.0";
      String userip = "192.168.0.1";
      HttpHeaders httpHeaders = new HttpHeaders();
      httpHeaders.set("Referer", referer);

      HttpEntity<?> requestEntity = new HttpEntity(httpHeaders);

      UriComponents uriComponents =
          UriComponentsBuilder.fromUriString(_SEARCH_URL)
              .queryParam("v", v)
              .queryParam("q", searchString)
              .queryParam("userip", userip)
              .build();
      ResponseEntity<GoogleSearchResponse> responseEntity =
          restTemplate.exchange(
              uriComponents.toUri(), HttpMethod.GET, requestEntity, GoogleSearchResponse.class);
      HttpStatus statusCode = responseEntity.getStatusCode();
      if (statusCode.is2xxSuccessful() || statusCode.is3xxRedirection()) {
        return responseEntity.getBody();
      } else {
        _log.error(
            String.format(
                "Search request: \n%s\n failed with HTTP code %s : %s",
                uriComponents.toString(), statusCode.toString(), statusCode.getReasonPhrase()));
        return null;
      }
    } catch (Exception e) {
      _log.error(e);
    }

    return null;
  }