當(dāng)你寫一段代碼很糾結(jié)時(shí)淤年,不防停下來Google一下苟蹈,一定有人已經(jīng)解決了你的煩惱!
一胳蛮、背景
今天PM提出想給一個(gè)URL埋點(diǎn)销凑,統(tǒng)計(jì)一下訪問來源,需要我在路徑上加一個(gè)參數(shù)fromPage=shop_detail;本來是一個(gè)很簡單的問題仅炊,因?yàn)樵溄痈緵]參數(shù)闻鉴,只只需要在url后面加上?fromPage=shop_detail;可想了一下有安全隱患茂洒,萬一url變了孟岛,帶了參數(shù)怎么辦?于是想那我就自己判斷是否有參數(shù)督勺,再決定加?或/;當(dāng)我下手寫的時(shí)候又糾結(jié)了渠羞,為了這么一點(diǎn)東西要寫好幾行代碼呢,不行智哀,還是得先google 一下次询,就有了如下結(jié)論。
二瓷叫、 添加parameter的方式
2.1 java.net.URI
通過string 生成URI屯吊,再通過URI獲取query,再通過query判斷是添加&或?; 似乎也挺麻煩……
package urItest;
import java.net.URI;
import java.net.URISyntaxException;
/**
* Created by fuxiangxu on 2017/1/10.
*/
public class URITest {
public static URI appendUri(String uri, String appendQuery) throws URISyntaxException {
URI oldUri = new URI(uri);
String newQuery = oldUri.getQuery();
if (newQuery == null) {
newQuery = appendQuery;
} else {
newQuery += "&" + appendQuery;
}
URI newUri = new URI(oldUri.getScheme(), oldUri.getAuthority(),
oldUri.getPath(), newQuery, oldUri.getFragment());
return newUri;
}
public static void main(String[] args) throws Exception {
System.out.println(appendUri("http://example.com", "name=John"));
System.out.println(appendUri("http://example.com#fragment", "name=John"));
System.out.println(appendUri("http://example.com?email=john.doe@email.com", "name=John"));
System.out.println(appendUri("http://example.com?email=john.doe@email.com#fragment", "name=John"));
}
}
2.2 javax.ws.rs.core.UriBuilder
package urItest;
import javax.ws.rs.core.UriBuilder;
import java.net.URISyntaxException;
/**
* Created by fuxiangxu on 2017/1/10.
*/
public class URIBuilderTest {
public static void main(String[] args) throws URISyntaxException {
System.out.println(UriBuilder.fromUri("http://example.com").queryParam("name", "john").build().toString());
System.out.println(UriBuilder.fromUri("http://example.com?email=john.doe@email.com").queryParam("name", "john").build().toString());
}
}
2.3 org.apache.http.client.utils.URIBuilder
package urItest;
import org.apache.http.client.utils.URIBuilder;
import java.net.URISyntaxException;
/**
* Created by fuxiangxu on 2017/1/10.
*/
public class URIBuilderTest {
public static void main(String[] args) throws URISyntaxException {
System.out.println(new URIBuilder("http://example.com").addParameter("name", "john").build().toString());
System.out.println(new URIBuilder("http://example.com?email=john.doe@email.com").addParameter("name", "john").build().toString());
}
}
2.4 org.springframework.web.util.UriComponentsBuilder (建議)
package urItest;
import org.springframework.web.util.UriComponentsBuilder;
import java.net.URISyntaxException;
/**
* Created by fuxiangxu on 2017/1/10.
*/
public class URIBuilderTest {
public static void main(String[] args) throws URISyntaxException {
System.out.println(UriComponentsBuilder.fromUriString("http://example.com").queryParam("name", "john").build().toString());
System.out.println(UriComponentsBuilder.fromUriString("http://example.com?email=john.doe@email.com").queryParam("name", "john").build().toString());
}
}