有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

加载swaggerui时java获取错误。spring中的html(5.0.0.RELEASE)mvc

无法解析引用,因为:无法解析指针:/definitions/文档中不存在错误

我遵循了这个链接http://www.baeldung.com/swagger-2-documentation-for-spring-rest-api,但在为自定义响应消息添加globalResponseMessage()方法时出现了上述错误。我不明白原因是什么。 请帮忙。。。。短暂性脑缺血发作

 @Bean
public Docket api() {
    return new Docket(DocumentationType.SWAGGER_2)
            .select()
            .apis(RequestHandlerSelectors.any())
            .paths(PathSelectors.any())
            .build()
            .apiInfo(apiInfo())
            .consumes(getContentType())
            .produces(getContentType())
            .useDefaultResponseMessages(false)
            .globalResponseMessage(RequestMethod.GET, newArrayList(
                    new ResponseMessageBuilder()
                            .code(500).message("500 message")
                            .responseModel(new ModelRef("Error")).build(),
                    new ResponseMessageBuilder()
                            .code(403)
                            .message("Forbidden!!!!!")
                            .build()));
}

enter image description here


共 (2) 个答案

  1. # 1 楼答案

    应该使用基本类,比如字符串。如果要使用自定义类(比如Error),应该在摘要定义中添加additionalModels(typeResolver.resolve (CustomResponseClass.class))来解析此模型。下面是一个运行良好的代码:

    @Autowired
    TypeResolver typeResolver;
    
    @Bean
    public Docket api() {
        return new Docket(DocumentationType.SWAGGER_2)
              .useDefaultResponseMessages(false)
              .directModelSubstitute(Object.class, java.lang.Void.class)
              .select()
              .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
              .build()
              .pathMapping("/")
              .apiInfo(apiInfo())
              .additionalModels(typeResolver.resolve (MensagemVo.class) )
              .globalResponseMessage(RequestMethod.GET,
                    newArrayList(new ResponseMessageBuilder()
                                .code(500)
                                .message("Execution error message")
                                .responseModel(new ModelRef("String"))
                                .build(),
                          new ResponseMessageBuilder()
                                .code(422)
                                .message("Validation error message")
                                .responseModel(new ModelRef("MensagemVo"))
                                .build())
              );
    }
    

    其中TypeResolve来自com.fasterxml.classmate

    结果图像: documentation result

  2. # 2 楼答案

    你有两个选择:

    1)将“错误”替换为“字符串”(小写)

    new ResponseMessageBuilder()
                            .code(500).message("500 message")
                            .responseModel(new ModelRef("string")).build(),
    

    2)将“Error”替换为响应正文中用于错误信息的类的名称(或为此定义一个Error类)。例如:

    new ResponseMessageBuilder()
                            .code(500).message("500 message")
                            .responseModel(new ModelRef("ErrorInfo")).build(),
    

    在本例中,类ErrorInfo应该位于web应用程序的类路径中(可以位于多个web应用程序共享的库中)。例如:

    @XmlRootElement
    public class ErrorInfo {
    
        private String url;
    
        @ApiModelProperty(notes = "HTTP Status Code")
        private int statusCode;
    
        @ApiModelProperty(notes = "HTTP Reason Phrase")
        private String reasonPhrase;
    
        @ApiModelProperty(notes = "Mensage to the user")
        private String message;
    
        @ApiModelProperty(notes = "Ticket created on IT help desk if applicable", required = false)
        private String helpDeskTicket;
    
        @ApiModelProperty(notes = "Debug information (e.g., stack trace), not visible if runtime environment is 'production'", required = false)
        private String debugInfo;
    
        public ErrorInfo() {
            // required by Jackson deserialization.
        }
    
        // ... other constructors, get/set methods...
    
    }