我试图在我的feign.builder中添加变量,但它一直在说
未处理的异常:java.net.URISyntaxException,java.io.IOException
抛出异常并不起作用既没有抓住任何想法如何解决这个问题?假期建造者:
Interface.DeleteComment deleteComment = Feign.builder()
.client(new OkHttpClient())
.encoder(new JacksonEncoder())
.decoder(new JacksonDecoder())
.logger(new Slf4jLogger(Interface.DeleteComment.class))
.logLevel(Logger.Level.FULL)
.target(Interface.DeleteComment.class,
"http://localhost:3000/comments/" + networkConfigurationProperties.id());
NetworkConfigurationProperties
public class NetworkConfigurationProperties {
public String id() throws URISyntaxException,IOException {
final URI uri = new URI("http://localhost:3000/comments/");
String path = uri.getPath();
String idStr = path.substring(path.lastIndexOf('/') + 1);
return idStr;
}
}
你的id()
方法可以抛出那些例外,这意味着它们必须被处理(某处)。
在你的通话方法中,你不能只说:
public void callId() {
Interface.DeleteComment deleteComment = Feign.builder()
.client(new OkHttpClient())
.encoder(new JacksonEncoder())
.decoder(new JacksonDecoder())
.logger(new Slf4jLogger(Interface.DeleteComment.class))
.logLevel(Logger.Level.FULL)
.target(Interface.DeleteComment.class, "http://localhost:3000/comments/" + networkConfigurationProperties.id());
}
因为你没有处理例外。你需要抓住它们:
public void callId() {
try{
Interface.DeleteComment deleteComment = Feign.builder()
.client(new OkHttpClient())
.encoder(new JacksonEncoder())
.decoder(new JacksonDecoder())
.logger(new Slf4jLogger(Interface.DeleteComment.class))
.logLevel(Logger.Level.FULL)
.target(Interface.DeleteComment.class, "http://localhost:3000/comments/" + networkConfigurationProperties.id());
} catch(Exception e) {
e.printStackTrace(); //or similar
}
}
或者让调用该方法的方法知道通过传播它们来预期可能的异常
public void callId() throws URISyntaxException,IOException {
Interface.DeleteComment deleteComment = Feign.builder()
.client(new OkHttpClient())
.encoder(new JacksonEncoder())
.decoder(new JacksonDecoder())
.logger(new Slf4jLogger(Interface.DeleteComment.class))
.logLevel(Logger.Level.FULL)
.target(Interface.DeleteComment.class, "http://localhost:3000/comments/" + networkConfigurationProperties.id());
}
在id()方法中添加try catch块。您的id()方法抛出两个已检查的异常,必须捕获该异常。尝试这样的事情:
public class NetworkConfigurationProperties {
public String id() {
try {
final URI uri = new URI("http://localhost:3000/comments/");
String path = uri.getPath();
String idStr = path.substring(path.lastIndexOf('/') + 1);
return idStr;
} catch (URISyntaxException|IOException e) {
return null;
}
}
}