为了账号安全,请及时绑定邮箱和手机立即绑定

无法在 feign builder 中引发期望

无法在 feign builder 中引发期望

白衣染霜花 2022-07-20 10:30:15
我试图在我的 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;    }}
查看完整描述

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());

}


查看完整回答
反对 回复 2022-07-20
?
慕运维8079593

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;

         }

    }

}


查看完整回答
反对 回复 2022-07-20
  • 2 回答
  • 0 关注
  • 103 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号