Springboot Add Swagger2 Notes

Source: Internet
Author: User

Reference
Http://www.conglin-site.com/document/html/1494763157.html

    1. Pom

      <dependency>  <groupId>io.springfox</groupId>  <artifactId>springfox-swagger2</artifactId>  <version>2.6.1</version></dependency><dependency>  <groupId>io.swagger</groupId>  <artifactId>swagger-annotations</artifactId>  <version>1.5.13</version></dependency>
    2. Integration with Springboot
      ```
      @Configuration
      @EnableSwagger2
      public class Swagger2 {

@Bean
Public Docket Config () {
return new Docket (documentationtype.swagger_2)
. Host ("127.0.0.1:8080")
. Apiinfo (Apiinfo ())
. Usedefaultresponsemessages (False)
. Select ()
. APIs (Requesthandlerselectors.basepackage ("Cc.homer3000.web"))
. build ();
}

Private Apiinfo Apiinfo () {
return new Apiinfobuilder ()
. Title ("Blog System API Documentation")
. Contact ("Author", "Access Address", "Contacts")
. build ();
}
}

3. 下载swagger ui`https://github.com/swagger-api/swagger-ui`dist 目录 index.htmlurl: "http://petstore.swagger.io/v2/swagger.json"修改为: url: "http://127.0.0.1:8080/v2/api-docs",4. 跨域问题http://blog.sina.com.cn/s/blog_13b4eadda0102xaam.html加拦截器

@Component
public class Corsinterceptor extends Handlerinterceptoradapter {
@Override
public boolean prehandle (HttpServletRequest request, httpservletresponse response, Object handler)
Throws Exception {
Add a cross-domain cors
Response.setheader ("Access-control-allow-origin", "*");
Response.setheader ("Access-control-allow-headers", "X-requested-with,content-type,token");
Response.setheader ("Access-control-allow-methods", "GET, HEAD, POST, PUT, DELETE, TRACE, OPTIONS, PATCH");
return true;
}
}

@Configuration
public class Crossdomain extends Webmvcconfigureradapter {
@Autowired
Private Corsinterceptor Corsinterceptor;

@Override
public void Addinterceptors (Interceptorregistry registry) {
Registry.addinterceptor (Corsinterceptor);
}
}
```

Springboot Add Swagger2 Notes

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.