ã¯ããã«
ã¨ããµã¤ãæ ªå¼ä¼ç¤¾ 21å ããã¯ã¨ã³ãã¨ã³ã¸ãã¢ã®å±±ç¸£ã§ãã
UriComponentsã®replaceQueryParam()
ã使ç¨ãã¦ã¯ã¨ãªãã©ã¡ã¼ã¿ã®å¤ãæ¸ãæããã¨ãã«ãæ¸ãæããåã¨æ¸ãæããå¾ã¨ã§ã¯ã¨ãªãã©ã¡ã¼ã¿ã®é åºãç°ãªã£ã¦ãã¾ãã
åä½ãã¹ãã§è½ã¡ã¦ãã¾ãã¾ããã
æ¬è¨äºã§ã¯ãã¯ã¨ãªãã©ã¡ã¼ã¿ã®é åºãç°ãªãURIãæ¯è¼ããæ¹æ³ã«ã¤ãã¦å
±æãã¾ãã
æ¦è¦
ã¯ã¨ãªãã©ã¡ã¼ã¿ã®é åºãç°ãªã2ã¤ã®URIãç¨æãã¾ãã
ãã®ã¨ããuri1ã¨uri2ãæ¯è¼ããã¨false
ã«ãªãã¾ãã
URI uri1 = URI.create("https://example.com?page=2&per_page=10"); URI uri2 = URI.create("https://example.com?per_page=10&page=2"); System.out.println(Objects.equals(uri1, uri2)); // â false
ããã¯ãURIãã¯ã¨ãªãã©ã¡ã¼ã¿ãæååã§ä¿æãã¦ãããequals
ã§æ¯è¼ããã¨ãã«ã¯ã¨ãªãã©ã¡ã¼ã¿ã¯"page=2&per_page=10"
ã¨"per_page=10&page=2"
ã®æååæ¯è¼ãè¡ã£ã¦ãããããfalse
ã«ãªã£ã¦ãã¾ãããã§ãã
ã¯ã¨ãªãã©ã¡ã¼ã¿ã®é åºãç°ãªã£ãã¨ãã¦ãåä¸ã®ãã®ã¨ãã¦æ¯è¼ãããã¨ãã¯ãå°ã工夫ããå¿
è¦ãããã¾ãã
解決ç
URIãUriComponentsã«å¤æãããã¨ã§å®¹æã«æ¯è¼ãããã¨ãã§ããããã«ãªãã¾ãã
UriComponentsã§ã¯ã¯ã¨ãªãã©ã¡ã¼ã¿ãMap<K, List<V>>
ã®ã©ããã¼ã§ããMultiValueMapã§ä¿æãã¦ãããããã¯ã¨ãªãã©ã¡ã¼ã¿ã®é åºãç°ãªããã®ãæ¯è¼ããã¨ãã«true
ãè¿ãã¾ãã
UriComponents u1 = UriComponentsBuilder.fromUri(uri1).build();
UriComponents u2 = UriComponentsBuilder.fromUri(uri2).build();
System.out.println(Objects.equals(u1, u2));
// â true
å®éã«UriComponentsã®ã¯ã¨ãªãã©ã¡ã¼ã¿ã®ä¸èº«ãè¦ã¦ã¿ãã¨ã確ãã«Mapå½¢å¼ã§ä¿æããã¦ãããã¨ã確èªã§ãã¾ãã
System.out.println(u1.getQueryParams());
// â {page=[2], per_page=[10]}
注æç¹
MultiValueMapã¯1ã¤ã®ãã¼ã«å¯¾ãã¦è¤æ°ã®å¤ãæ±ããã¨ãã§ãã¾ãã
ãã®ãããã¯ã¨ãªãã©ã¡ã¼ã¿ã«åä¸ã®ãã¼ãè¤æ°æå®ããã¦ããå ´åãListã«å¤æããããããfalse
ãè¿ãå¯è½æ§ãããã¾ãã
URI uri3 = URI.create("https://example.com?page=2&per_page=10&page=3"); URI uri4 = URI.create("https://example.com?page=3&per_page=10&page=2"); UriComponents u3 = UriComponentsBuilder.fromUri(uri3).build(); UriComponents u4 = UriComponentsBuilder.fromUri(uri4).build(); System.out.println(u3.getQueryParams()); // â {page=[2, 3], per_page=[10]} System.out.println(u4.getQueryParams()); // â {page=[3, 2], per_page=[10]} System.out.println(Objects.equals(u3, u4)); // â false
ãããã£ã¦ãåä¸ã®ãã¼ãè¤æ°æå®ãããå ´åã¯æ³¨æããªãã¦ã¯ãªãã¾ãã
ãããã«
UriComponentsãå©ç¨ãã¦ã¯ã¨ãªãã©ã¡ã¼ã¿ã®é åºãç°ãªãURIãæ¯è¼ããæ¹æ³ã«ã¤ãã¦ã¾ã¨ãã¾ããã
ã¯ã¨ãªãã©ã¡ã¼ã¿ã®é åºãç°ãªãURIãæ¯è¼ãããã¨ãã£ã¦ä¸åº¦ã¯ããã®ããªã¨æãã¾ãã
URIãUriComponentsã«ãã¦æ¯è¼ãè¡ããã¨ã§ãã¯ã¨ãªãã©ã¡ã¼ã¿é åºãç°ãªã£ã¦ããã¨ãã¦ãtrue
ãè¿ãããã«ãªãã¾ãã
ãã¯ã¨ãªãã©ã¡ã¼ã¿ã®é åºãç°ãªãURIã§ãåããã®ã¨ãã¦æ±ãããï¼ãã¨ãã£ãæ¹ã«æ¬è¨äºãåèã«ãªãã°å¹¸ãã§ãã