为什么Swagger ui不在Spring的maven项目中显示服务?

时间:2018-03-22 21:37:30

标签: spring user-interface swagger

我有一个带有招摇的maven项目,当我尝试使用http://localhost:8080/swagger-ui.html时,没有像图片那样显示出什么样的Swagger ui:

Swagger ui

我的依赖项是在7.0版本中(我也尝试使用6.2和6.3),我的代码是:

@Configuration
@EnableSwagger2
public class SwaggerConfig {                                    
    @Bean
    public Docket api() { 
        return new Docket(DocumentationType.SWAGGER_2)  
          .select()                                  
          .apis(RequestHandlerSelectors.any())              
      .paths(PathSelectors.any())                          
      .build();                                           
    }
}

控制器是:

@RestController
@EnableAutoConfiguration
@SpringBootApplication
@EnableSwagger2
public class Example {

@Autowired
public AlfrescoService alfrescoService;

@ResponseBody
@RequestMapping(value = "/hello", method = RequestMethod.GET)
@ApiOperation(value = "Muestra mensaje", notes = "Retorna mensaje", response = ResponseMessage.class, consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
@ApiResponses({

        @ApiResponse(code = 200, message = "Documentos obtenidos existosamente.", response = ResponseMessage.class),
        @ApiResponse(code = 404, message = "No se encontraron documentos.", response = ResponseMessage.class) })
String home() {
    alfrescoService.obtieneSesion();
    return "Hello World!";
}

@ResponseBody
@RequestMapping(value = "/", method = RequestMethod.GET)
String manchester() {
    alfrescoService.obtieneSesion();
    return "United";
}

@ResponseBody
@RequestMapping(value = "/uploadDocument", method = RequestMethod.POST)
String uploadFile(@RequestParam MultipartFile file, @RequestParam String title, @RequestParam String rut,
        @RequestParam String userName, @RequestParam int year) throws Exception {
    if (file.isEmpty()) {
        return "No a ingresado el archivo";
    } else {
        alfrescoService.upload(file, title, rut, userName, year);
        return "OK";
    }
}

@ResponseBody
@RequestMapping(value = "/searchDocument", method = RequestMethod.GET)
String searchDocuments(String uuid) {
    CmisObject doc = alfrescoService.searchDocuments(uuid);
    String info = "uuid:" + 
doc.getProperty("cmis:objectId").getValueAsString() + "\nTipo del Documento:"
            + doc.getProperty("bc:documentType").getValueAsString() + "\nId 
Tipo de documento:"
            + doc.getProperty("bc:documentTypeId").getValueAsString() + 
"\nCodigo tipo de documento:"
            + doc.getProperty("bc:idDocument").getValueAsString() + "\nFolio 
Documento:"
            + doc.getProperty("bc:folioDocument").getValueAsString() + 
"\nRut del Cliente:"
            + doc.getProperty("bc:rutClient").getValueAsString() + "\nNombre 
del cliente:"
            + doc.getProperty("bc:clientName").getValueAsString() + 
"\nNombre Documento:" + doc.getName();
    return info;
}

@ResponseBody
@RequestMapping(value = "/deleteDocument", method = RequestMethod.POST)
String deleteDocument(String uuid) {
    alfrescoService.deleteDocument(uuid);
    return "Ok";
}

@ResponseBody
@RequestMapping(value = "/corruptPDF", method = RequestMethod.POST)
String corruptPDF(@RequestParam MultipartFile file) throws Exception {      
        return alfrescoService.corruptPDF(file);                    
}

@ResponseBody
@RequestMapping(value = "/consumeRest")
String consumeRest(@RequestParam(required=false,name="login") String login, 
@RequestParam(required=false,name="password") String password) throws 
SAXException, IOException, ParserConfigurationException {       
        return alfrescoService.consumeRest(login, password);                
}   

public static void main(String[] args) throws Exception {

    SpringApplication.run(Example.class, args);

}
}

我今天多次搜索和重新搜索,但只是一个像我的麻烦一样的后期,但它没有用。如果是一个基本的问题,我是一个实际的对不起。

1 个答案:

答案 0 :(得分:0)

您应该在控制器级别拥有@Api,并且控制器类中不需要@EnableSwagger2,因为您已经@Configuration

@RestController
@EnableAutoConfiguration
@SpringBootApplication
@Api(value = "Read API",
        description = "Rest APIs for read data etc",
        produces = "application/json")
public class Example {

....

}

您还需要在配置类中定义API bean,如下所示;

@Bean
public Docket api() {
        return new Docket(DocumentationType.SWAGGER_2)
                .select().apis(RequestHandlerSelectors.basePackage("controller.package"))
                .paths(PathSelectors.any())
                .build().apiInfo(apiInfo());

}

检查此链接以获取有效示例 https://www.tuturself.com/posts/view?menuId=3&postId=1091