猿问

无法在 feign builder 中引发期望

我试图在我的 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());

网络配置属性


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;

    }

}


白衣染霜花
浏览 104回答 2
2回答

隔江千里

您的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());}

慕运维8079593

在您的 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;         }    }}
随时随地看视频慕课网APP

相关分类

Java
我要回答