我有一个以下字符串,我必须在某些条件下拆分并替换该值
http://localhost:8080/api/upload/form/{uploadType}/{uploadName}
我必须只替换 {uploadType}/{uploadName} 的值。我真的不知道如何用价值替换它们。
其中的字符串{uploadType}/{uploadName}
可以是任何要替换的类型。
我尝试过类似以下的方法:
包 com.test.poc;
public class TestString {
public static void main(String[] args) throws ClassNotFoundException {
String toBeFixed = "http://localhost:8080/api/upload/form/{uploadType}/{uploadName}";
String[] toReplaceWith =toBeFixed.split("{");
for (String string : toReplaceWith) {
System.out.println("string : "+string);
}
}
}
但我得到以下异常:
Exception in thread "main" java.util.regex.PatternSyntaxException: Illegal repetition
{
at java.util.regex.Pattern.error(Pattern.java:1713)
at java.util.regex.Pattern.closure(Pattern.java:2775)
at java.util.regex.Pattern.sequence(Pattern.java:1889)
at java.util.regex.Pattern.expr(Pattern.java:1752)
at java.util.regex.Pattern.compile(Pattern.java:1460)
at java.util.regex.Pattern.<init>(Pattern.java:1133)
at java.util.regex.Pattern.compile(Pattern.java:823)
at java.lang.String.split(String.java:2292)
at java.lang.String.split(String.java:2334)
at com.test.poc.TestString.main(TestString.java:9)
编辑 :
这是我根据Sean Patrick Floyd
答案尝试的方法
public String doPOSTPathVariable(String uri,List paramsList) throws IllegalArgumentException, IllegalAccessException, InstantiationException, Exception{
String uriString="";
UriComponents uriComponents = UriComponentsBuilder.fromUriString(uri).build();
for (int j = 0; j<= paramsList.size(); j++) {
System.out.println("path variable");
MethodParams methodParams;
methodParams =(MethodParams) paramsList.get(j);
if(methodParams.isPrimitive() && methodParams.getDataType()=="boolean"){
uriString = uriComponents.expand(true).encode().toUriString();
}
if(methodParams.isPrimitive() && methodParams.getDataType()=="java.math.BigDecimal"){
uriString = uriComponents.expand(123).encode().toUriString();
}
if(methodParams.isPrimitive() && methodParams.getDataType()=="java.lang.String"){
uriString = uriComponents.expand("hexgen").encode().toUriString();
}
}
return uriString;
}
但我得到以下异常:
java.lang.IllegalArgumentException: Not enough variable values available to expand 'uploadName'
at org.springframework.web.util.UriComponents$VarArgsTemplateVariables.getValue(UriComponents.java:1025)
at org.springframework.web.util.UriComponents.expandUriComponent(UriComponents.java:443)
at org.springframework.web.util.UriComponents.access$1(UriComponents.java:431)
at org.springframework.web.util.UriComponents$FullPathComponent.expand(UriComponents.java:800)
at org.springframework.web.util.UriComponents.expandInternal(UriComponents.java:413)
at org.springframework.web.util.UriComponents.expand(UriComponents.java:404)
at com.hexgen.tools.HexgenClassUtils.doPOSTPathVariable(HexgenClassUtils.java:208)
at com.hexgen.reflection.HttpClientRequests.handleHTTPRequest(HttpClientRequests.java:77)
at com.hexgen.reflection.HexgenWebAPITest.main(HexgenWebAPITest.java:115)
有人可以帮我解决这个问题吗?