03, Swagger2 and SPRINGMVC integration detailed record (crawl pit record)

Source: Internet
Author: User

Time content Notes
June 18, 2018 Basic use SPIRNGMVC Integrated Swagger2

Before you begin this series of blog post is basically, in the use of the project some modules of the content record, but later gradually optimized, not just the integration content.

Swagger Introduction

1, swagger mainly provides three functions:

  • Swagger Editor:swagger provides an editor to write API documentation through the specific YAML syntax provided by Swagger
  • Swagger Codegen: Code generator
  • Swagger Ui:yaml syntax defines our restful API and then it automatically generates a beautifully formatted API document and provides a real-time preview.
SPIRNGMV Integrated Swagger

In fact, this integration example is very many, can choose to use.

Recommendation: official documentation, although in English but the meaning of each configuration is clearly recommended to use, the later optimization content according to this implementation.

Import Basic steps:

1, modify the Pom.xml; Add the following swagger dependencies

<!-- 构建Restful API --><dependency>    <groupId>io.springfox</groupId>    <artifactId>springfox-swagger2</artifactId>    <version>2.4.0</version></dependency><dependency>    <groupId>io.springfox</groupId>    <artifactId>springfox-swagger-ui</artifactId>    <version>2.4.0</version></dependency>

2, add the following configuration class
Note the following are not necessary steps (swagger will use the default configuration)

Package com.weir.utils;import Org.springframework.context.annotation.Bean;import Org.springframework.context.annotation.ComponentScan;import org.springframework.context.annotation.Configuration;import ORG.SPRINGFRAMEWORK.WEB.SERVLET.CONFIG.ANNOTATION.ENABLEWEBMVC;import Org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;import Springfox.documentation.builders.ApiInfoBuilder;import springfox.documentation.builders.PathSelectors;import springfox.documentation.builders.RequestHandlerSelectors;import Springfox.documentation.service.ApiInfo;import springfox.documentation.service.Contact;import Springfox.documentation.spi.DocumentationType;import Springfox.documentation.spring.web.plugins.Docket;import Springfox.documentation.swagger2.annotations.EnableSwagger2;@Configuration@EnableSwagger2@EnableWebMvc@ComponentScan(Basepackages ="Com.weir")classApiconfigextendsWebmvcconfigurationsupport {@Bean     PublicDocketAPI() {return New Docket(Documentationtype.swagger_2)                .Select()                .APIs(Requesthandlerselectors.Basepackage("Com.weir"))                .Paths(Pathselectors. any())                .Build()                .Apiinfo(Apiinfo()); }PrivateApiinfoApiinfo() {return New Apiinfobuilder()                .title("interface list v1.1.0")//arbitrary, please slightly standardize the point.Description("Interface Test")//arbitrary, please slightly standardize the point.Termsofserviceurl("Http://localhost:8080/swagger-ui.html")//Replace "url" with your own ip:port.version("1.1.0")                .Build(); }}

3. Modify the Springmvc.xml file; Add the configuration about swagger as follows:

<mvc:default-servlet-handler /><!-- 配置Swagger相关静态资源 --><mvc:resources location="classpath:/META-INF/resources/" mapping="swagger-ui.html"/><mvc:resources location="classpath:/META-INF/resources/webjars/" mapping="/webjars/**"/><!-- 添加扫描配置类 --><bean class="com.weir.utils.ApiConfig" />

4, the use of the code:

@RequestMapping(value = "/login")@ApiOperation(value = "用户登录", notes = "用户登录操作")@ApiImplicitParam(name = "id", value = "用户ID", required = true, dataType = "Long")@ResponseBodypublic ServerResponse<User> login(String userName, String password, HttpSession session){    ServerResponse<User> response = userService.login (userName, password);    if (response.isSuccess ()){        session.setAttribute ("user",response);    }    return response;}

5. Running Display

The import encountered a problem record:

The most important thing: (the content of oneself crawl pit) " must pay attention to "

1, console error content:Getting HTTP 404 error on /swagger-ui.html but other swagger endpoint works

The interception path is instead / configured as a suffix, such as: /*.do etc., which causes the wagger-ui.html page to be blocked and cannot be loaded.

<!--dispatcherservlet --<servlet><servlet-name>Dispatcherservlet</servlet-name><servlet-class>Org.springframework.web.servlet.DispatcherServlet</servlet-class><init-param>  <param-name>Contextconfiglocation</param-name>  <param-value>Classpath:springmvc.xml</param-value></init-param></servlet><servlet-mapping><servlet-name>Dispatcherservlet</servlet-name><url-pattern>/</url-pattern></servlet-mapping>

Of course, if you want to configure with a suffix is also possible, please refer to the following content:

    • Key reference page to resolve [GitHub issue]
    • about how to configure a suffix to resolve official documents
Summarize:
  • Others blog is only a reference, as far as possible to refer to the official documentation, understand the principle is the key
  • Recommended official documents, Chinese documents and blogs are uneven
  • Don't be afraid of English documents and materials

03, Swagger2 and SPRINGMVC consolidation detail records (crawl pit Records)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.