我正试图用swagger制作我的API文档。
我的REST API是在spring的帮助下编写的,典型的Controller看起来像:
@Controller
@RequestMapping("/mymapping")
public class ParameterController {
@Autowired
Mydao myDao;
/**
* GET list
*
* @return The resulting string
*/
//produces replaced accept-header
@RequestMapping(value="/Parameter", method=RequestMethod.GET, produces={MediaType.APPLICATION_JSON_UTF8_VALUE, "application/ld+json"})
public @ResponseBody List<String> getAllParameters() {
List<String> params= myDao.getAllParameters();
return params;
}
}
为了配置Swagger,我遵循了here指令:
SwaggerConfig
(我只使用Spring的Java配置)WebMvcConfigurerAdapter
@ComponentScan
WebMvcConfigurerAdapter
注释中
醇>
但每当我拨打http://localhost:8080/spring-security-rest/api/v2/api-docs时,我都会收到
16:43:43,370 WARN qtp1072754634-13 servlet.PageNotFound:1136 - No mapping found for HTTP request with URI [/spring-security-rest/api/v2/api-docs] in DispatcherServlet with name 'org.springframework.web.servlet.DispatcherServlet-437a7f13'
系统在控制器的映射中搜索路径/spring-security-rest/api/v2/api-docs
。并没有找到它。
我忘了什么?
提前谢谢。
答案 0 :(得分:0)
我的SwaggerConfig
课程看起来像这样:
@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Bean
public Docket beerMapApi() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.any())
.paths(regex("/api.*"))
.build()
.pathMapping("/")
.apiInfo(apiInfo());
}
@Bean
public UiConfiguration uiConfig() {
return UiConfiguration.DEFAULT;
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("title")
.description("description")
.version("0.1")
.build();
}
}
这就是我的WebMvcConfigurerAdapter.addResourceHandlers
看起来的样子:
@Configuration
@EnableWebMvc
@ComponentScan({ "com.something.server.controller" })
public class WebConfig extends WebMvcConfigurerAdapter {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry
.addResourceHandler("/resources/**")
.addResourceLocations("/resources/");
registry
.addResourceHandler("swagger-ui.html")
.addResourceLocations("classpath:/META-INF/resources/");
registry
.addResourceHandler("/webjars/**")
.addResourceLocations("classpath:/META-INF/resources/webjars/");
}
@Bean
public InternalResourceViewResolver viewResolver() {
InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
viewResolver.setViewClass(JstlView.class);
viewResolver.setPrefix("/WEB-INF/views/jsp/");
viewResolver.setSuffix(".jsp");
return viewResolver;
}
}
答案 1 :(得分:0)
我通过将SwaggerConfig Java类放在与@SpringBootApplication创建类的同一包中解决了我的问题。
这里有个建议:
通常,这是由于组件扫描使用不当造成的。您可以在这里阅读更多有关组件扫描的信息(完整阅读两个问题:))
点击此处查看2 questions