Swagger for Spring MVC项目 [英] Swagger for Spring MVC project
问题描述
关于在Spring MVC中集成Swagger:
Regarding integrating Swagger in Spring MVC:
Swagger没有显示 GET / PUT / POST
文档 @RequestMapping
Swagger is not displaying the GET/PUT/POST
documentation for @RequestMapping
在我的Spring MVC Rest webservice应用程序中,我有一个Login控制器和一个Student Controller。
我刚刚配置了Swagger来生成Rest API文档。
参考: http://java.dzone.com/articles/how -configure-swagger-generate
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文件中的以下配置,如
$ b
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/>
推荐答案
无论现在看到的输出是好的,我们都不会在此 JSON
输出中查看swagger UI和 GET / POST / PUT
方法级别映射。所以没关系。它只显示类级别路径。
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
方法查看实际的Swagger UI级别映射和URL,我们需要
来下载 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-master\swagger-ui-master\dist\index.html
在这里,编辑应用程序api-docs URL的源JSON URL:
And then navigate to this index.html
file: swagger-ui-master\swagger-ui-master\dist\index.html
here, edit the source JSON URL to your application api-docs URL :
ie:
$(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...
这篇关于Swagger for Spring MVC项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!