Swagger for Spring MVC project(Spring MVC 项目的 Swagger)
问题描述
关于在 Spring MVC 中集成 Swagger:
Regarding integrating Swagger in Spring MVC:
Swagger 不显示 @RequestMapping
Swagger is not displaying the GET/PUT/POST
documentation for @RequestMapping
在我的 Spring MVC Rest webservice 应用程序中,我有一个登录控制器和一个学生控制器.我刚刚将 Swagger 配置为生成 Rest API 文档.参考:http://java.dzone.com/articles/how-configure-swagger- 生成
In my Spring MVC Rest webservice application, I have a Login controller and a Student Controller. I just configured Swagger to generate the Rest API documentation. Reference: http://java.dzone.com/articles/how-configure-swagger-generate
问题:但是,Swagger 只显示类级别的路径,我猜它不会显示类级别的 @RequestMapping
., 方法级映射被忽略.有什么原因?
Question: However, Swagger is displaying only the class-level path, and I guess its not wven displaying the class level @RequestMapping
. , The method level mappings are ignored. Any reason why ?
@Controller
@RequestMapping(value = "/login")
public class LoginController {
@RestController
@RequestMapping(value = "/students/")
public class StudentController {
@RequestMapping(value = "{departmentID}", method = RequestMethod.GET)
public MyResult getStudents(@PathVariable String departmentID) {
// code
}
@RequestMapping(value = "student", method = RequestMethod.GET)
public MyResult getStudentInfo(
@RequestParam(value = "studentID") String studentID,
@RequestParam(value = "studentName") String studentName) {
//code
}
@RequestMapping(value = "student", method = RequestMethod.POST)
public ResponseEntity<Student> updateStudentInfo(@RequestBody Student student) {
//code
}
Swagger 配置:
Swagger Configuration:
@Configuration
@EnableSwagger
public class SwaggerConfiguration {
private SpringSwaggerConfig swaggerConfig;
@Autowired
public void setSpringSwaggerConfig(SpringSwaggerConfig swaggerConfig) {
this.swaggerConfig = swaggerConfig;
}
@Bean
// Don't forget the @Bean annotation
public SwaggerSpringMvcPlugin customImplementation() {
return new SwaggerSpringMvcPlugin(this.swaggerConfig).apiInfo(
apiInfo()).includePatterns("/.*");
}
private ApiInfo apiInfo() {
ApiInfo apiInfo = new ApiInfo("my API", "API for my app", "", "contact@localhost.com", "License type",
"something like a License URL");
return apiInfo;
}
输出:
http://localhost:8080/studentapplication/api-docs
{
apiVersion: "1.0",
swaggerVersion: "1.2",
apis: [
{
path: "/default/login-controller",
description: "Login Controller"
},
{
path: "/default/student-controller",
description: "Student Controller"
}
],
info: {
title: "Student API",
description: "API for Student",
termsOfServiceUrl: "StudentApp API terms of service",
contact: "abc@xyz.com",
license: "sometext",
licenseUrl: "License URL"
}
}
更新:
您还需要 spring config XML 文件中的以下配置,如 https://github 中所述.com/martypitt/swagger-springmvc
you also need the below config in the spring config XML file, as mentioned in https://github.com/martypitt/swagger-springmvc
<!-- to enable the default documentation controller-->
<context:component-scan base-package="com.mangofactory.swagger.controllers"/>
<!-- to pick up the bundled spring configuration-->
<context:component-scan base-package="com.mangofactory.swagger.configuration"/>
<!-- Direct static mappings -->
<mvc:resources mapping="*.html" location="/, classpath:/swagger-ui"/>
<!-- Serve static content-->
<mvc:default-servlet-handler/>
推荐答案
现在看到的输出是好的,我们不会在这里看到 swagger UI 和 GET/POST/PUT
方法级别的映射在这个 JSON
输出中.所以没关系.它仅显示类级别路径.
Whatever output seeing now is good, we won't see the swagger UI and the GET/POST/PUT
method level mappings here in this JSON
output. So that's fine. It shows only the class level path.
要查看具有 GET/POST/PUT
方法级别映射和 URL 的实际 Swagger UI,我们需要下载 SwaggerUI
可在此处获得:https://github.com/swagger-api/swagger-ui
To see the actual Swagger UI with the GET/POST/PUT
method level mappings, and the URL's, we need
to download the SwaggerUI
which is available here: https://github.com/swagger-api/swagger-ui
然后导航到这个 index.html
文件:swagger-ui-masterswagger-ui-masterdistindex.html
在这里,将源 JSON URL 编辑为您的应用程序 api-docs URL:
And then navigate to this index.html
file: swagger-ui-masterswagger-ui-masterdistindex.html
here, edit the source JSON URL to your application api-docs URL :
即:
$(function () {
window.swaggerUi = new SwaggerUi({
url: "studentapplication/api-docs",
dom_id: "swagger-ui-container",
supportedSubmitMethods: ['get', 'post', 'put', 'delete'],
现在你看到了一切!!!
Now you see everything!!!
我只有一步之遥......
I was just one step away...
这篇关于Spring MVC 项目的 Swagger的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Spring MVC 项目的 Swagger
基础教程推荐
- 如何在不安装整个 WTP 包的情况下将 Tomcat 8 添加到 Eclipse Kepler 2022-01-01
- Spring Boot Freemarker从2.2.0升级失败 2022-01-01
- 在螺旋中写一个字符串 2022-01-01
- 首次使用 Hadoop,MapReduce Job 不运行 Reduce Phase 2022-01-01
- 如何使用 Stream 在集合中拆分奇数和偶数以及两者的总和 2022-01-01
- 如何使用 Eclipse 检查调试符号状态? 2022-01-01
- 如何对 HashSet 进行排序? 2022-01-01
- 如何强制对超级方法进行多态调用? 2022-01-01
- Java 中保存最后 N 个元素的大小受限队列 2022-01-01
- 由于对所需库 rt.jar 的限制,对类的访问限制? 2022-01-01