Swagger for Spring MVC project

Regarding Swagger integration in Spring MVC:

Swagger does not display GET/PUT/POST documentation for @RequestMapping

In my Spring MVC Rest webservice application, I have an input controller and Student Controller. I just configured Swagger to create the Rest API documentation. Link: http://java.dzone.com/articles/how-configure-swagger-generate

Question However, Swagger only displays the path at the class level, and I think it is not wven displaying the class level @RequestMapping . 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 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; } 

Output:

 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" } } 

Update:

you will also need the following configuration in the Spring config configuration file, as indicated at 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/> 
+6
java spring-mvc swagger
Nov 07 '14 at 18:32
source share
1 answer

No matter which view we see now is good, we will not see the Swagger UI and GET/POST/PUT interfaces at this JSON level. So good. It shows only the path to the level.

To see the actual Swagger user interface showing the GET/POST/PUT method level and URL, we need to download the SwaggerUI , which is available here: https://github.com/swagger-api/swagger-ui

And then browse to this index.html file: swagger-ui-master\swagger-ui-master\dist\index.html here edit the URL of the original JSON of your application. api-docs URL:

t

  $(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 ...

+2
Nov 11 '14 at 15:34
source share



All Articles