spring-tutorial icon indicating copy to clipboard operation
spring-tutorial copied to clipboard

博主你好,发现一个错误,不知道是我的操作有问题,还是代码不正确

Open RockChuLee opened this issue 5 years ago • 0 comments

本篇文章种有一部分讲述有关自定义响应消息的代码如下

.useDefaultResponseMessages(false)
.globalResponseMessage(RequestMethod.GET, newArrayList(
new ResponseMessageBuilder()
              .code(500)
              .message("服务器发生异常")
              .responseModel(new ModelRef("Error"))
              .build(),
       new ResponseMessageBuilder()
              .code(403)
              .message("资源不可用")
              .build()
));

如果直接复制,会报以下错误。 Cannot resolve constructor 'ArrayList(springfox.documentation.service.ResponseMessage, springfox.documentation.service.ResponseMessage)'

应该是ArrayList的初始化问题

我做了如下修改

.useDefaultResponseMessages(false)
                .globalResponseMessage(RequestMethod.GET, new ArrayList() {{
                    add(new ResponseMessageBuilder()
                            .code(500)
                            .message("服务器发生异常")
                            .responseModel(new ModelRef("Error"))
                            .build());
                    add(new ResponseMessageBuilder()
                            .code(403)
                            .message("资源不可用")
                            .build());
                }})

问题解决。不知道是不是代码有问题,如果有问题希望可以起到一些帮助作用,如果是我理解有误,希望可以得到指正。

RockChuLee avatar Sep 21 '20 05:09 RockChuLee