Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swagger for Spring MVC project

Regarding integrating Swagger in Spring MVC:

Swagger is not displaying the GET/PUT/POST documentation for @RequestMapping

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

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 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", "", "[email protected]", "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: "[email protected]",
license: "sometext",
licenseUrl: "License URL"
}
}

Update:

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/>
like image 880
spiderman Avatar asked Nov 07 '14 18:11

spiderman


People also ask

Can we use Swagger in Spring MVC?

The complete swagger specification is available at https://github.com/wordnik/swagger-spec and it's worth being familiar with the main concepts of the specification and the documentation on the [Swagger Annotations] (https://github.com/swagger-api/swagger-core/wiki/Annotations) Typically a Spring Web MVC project will ...

What is Swagger in MVC?

NET MVC Web API. Swagger (OpenAPI) is a language-agnostic specification for describing REST APIs. It allows both computers and humans to understand the capabilities of a REST API without direct access to the source code.

How do I add Swagger to spring boot?

To enable the Swagger2 in Spring Boot application, you need to add the following dependencies in our build configurations file. For Gradle users, add the following dependencies in your build. gradle file. Now, add the @EnableSwagger2 annotation in your main Spring Boot application.

What is Swagger used for in Java?

Swagger is the most widely used tool for building APIs compliant to the OpenAPI Specification (OAS). Swagger itself is a set of open-source tools built around the OAS that can help you design, build, document, and generate the REST API documents for RESTful web services.


1 Answers

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.

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

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

like image 63
spiderman Avatar answered Sep 20 '22 22:09

spiderman