2 回答

TA贡献1906条经验 获得超10个赞
您的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());
}

TA贡献1876条经验 获得超5个赞
在您的 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;
}
}
}
添加回答
举报