면책 조항 : 나는
당신은 this library을 시도 할 수 있습니다 ... 저자,하지만. RFC 6570 (URI 템플릿)의 구현입니다. 모든 공평성에서 나는 더 좋은 API를 가지고 있지만 훨씬 더 많은 의존성을 가지고있는 another implementation이 있음을 언급해야한다. (광산은 구아바에만 의존한다.)
의이 int1
당신이 변수가 int2
, month
을 가정 해 봅시다, 템플릿은 다음과 같습니다
// Since the lib depends on Guava, might as well use that
final List<String> months = ImmutableList.of("jan", "feb", "etc");
// Create the template
final URITemplate template
= new URITemplate("http://www.asite.com/path/to/something/{int1}/{month}-{int2}");
// Variable values
VariableValue int1, month, int2;
// Expansion data
Map<String, VariableValue> data;
// Build the strings
for (int i1 = 0; i1 <= 10; i1++)
for (final String s: months)
for (int i2 = 0; i2 <= 1000; i2++) {
int1 = new ScalarValue(Integer.toString(i1));
month = new ScalarValue(s);
int2 = new ScalarValue(Integer.toString(i2));
data = ImmutableMap.of("int1", int1, "month", month, "int2", int2);
// Print the template
System.out.println(template.expand(data));
}
중요 참고 : 라이브러리를 사용
http://www.asite.com/path/to/something/{int1}/{month}-{int2}
, 당신은 이런 일을 할 수있는 .expand()
메서드는 URI
또는 URL
이 아닌 String
을 반환합니다. 이유는 RFC가 확장 결과를 보장하지만 결과 문자열이 실제로 URI 또는 URL임을 보장 할 수 없기 때문입니다. 그 문자열을 자신이 원하는 문자열로 변환해야합니다.
출처
2013-05-31 13:38:12
fge
약 6 줄의 코드입니다. 당신이 정말로 도서관을 필요로합니까? –
@OliCharlesworth URI 템플릿 (RFC 6570)을 잊어 버린 것 같습니다. 내 대답을 참조하십시오. – fge
@fge : 내가 작성한 지점이 무효화되는 것은 확실하지 않습니다.) 답변의 예제 코드는 "복잡한" 당신이 순수한 자바에서 이것을하고있는 것처럼 ... –