SPRINGMVC Project Access Springfox

Source: Internet
Author: User

First, Introduction

Springfox, formerly known as SWAGGER-SPRINGMVC, is an open source API Doc framework that can be used to document the way our controller is presented.

Second, Access 1, pom-dependent 1) necessary
<!--springfox dependent--><dependency>    <groupId>com.mangofactory</groupId>    < Artifactid>swagger-springmvc</artifactid>    <version>1.0.2</version></dependency> <!--Jackson relies on--><dependency>    <groupId>com.fasterxml.jackson.core</groupId>    <artifactId>jackson-databind</artifactId>    <version>2.4.2</version></dependency ><!--static page-dependent webjar--><dependency>    <groupId>org.webjars.bower</groupId>    < Artifactid>swagger-ui</artifactid>    <version>2.1.8-M1</version></dependency>
2) If the project relies on a spring version less than 3.2.0.RELEASE, you need to upgrade spring-web dependencies, and guava version dependencies
<dependency>    <groupId>org.springframework</groupId>    <artifactId>spring-web< /artifactid>    <version>3.2.0.RELEASE</version></dependency><dependency>    < groupid>com.google.guava</groupid>    <artifactId>guava</artifactId>    <version> 15.0</version></dependency>
2. Springfox configuration file

springfoxconfig class

 /* * Copyright (c) 2015. Lorem ipsum dolor sit amet, consectetur adipiscing elit. * Morbi non lorem porttitor neque feugiat blandit. Ut Vitae ipsum eget quam lacinia accumsan. * etiam sed turpis ac ipsum condimentum Fringilla. Maecenas Magna. * Proin dapibus sapien vel ante. Aliquam erat volutpat. Pellentesque sagittis ligula eget metus. * Vestibulum Commodo. Ut rhoncus gravida Arcu. */import Com.mangofactory.swagger.configuration.springswaggerconfig;import Com.mangofactory.swagger.models.dto.apiinfo;import Com.mangofactory.swagger.plugin.enableswagger;import Com.mangofactory.swagger.plugin.swaggerspringmvcplugin;import Org.springframework.beans.factory.annotation.autowired;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.WebMvcConfiguReradapter;import java.io.file;/** * Use annotations to scan API * without configuring in the spring XML configuration file, replaced by @see @EnableWebMvc * <p/> * <p> @author Liu Xinyu * <p/> * <p> @date April 26, 2015 PM 1:18:48 * <p> @version 0.0.1 * * @Configuration @enablewebmvc@ Enableswaggerpublic class Springfoxconfig extends Webmvcconfigureradapter {/** * Project Name */Public STA    Tic String project_name;        static {String ProjectName = System.getproperty ("User.dir"); if (Projectname.contains ("Hotel")) {int end = Projectname.indexof (File.separator, Projectname.indexof ("Hotel")            ); Project_Name = projectname.substring (Projectname.lastindexof (File.separator, Projectname.indexof ("Hotel") + 1, end = =-1?        Projectname.length (): End);        } else {project_name = "hotel-server";    }} private Springswaggerconfig springswaggerconfig; @Autowired public void Setspringswaggerconfig (Springswaggerconfig springswaggerconfig) {This.springswagGerconfig = Springswaggerconfig; }/** * Chain programming to customize the API style * followed by grouping information * * @return */@Bean public swaggerspringmvcplugin Customim                Plementation () {return new Swaggerspringmvcplugin (This.springswaggerconfig). Apiinfo (Apiinfo ())        . IncludePatterns (". *"). Apiversion ("0.0.1");    . Swaggergroup (PROJECT_NAME);                } private Apiinfo Apiinfo () {apiinfo apiinfo = new Apiinfo (project_name + "API",                PROJECT_NAME + "Background API documentation", "Http://127.0.0.1:9081/api", "[email protected]",        "MTA License", "MTA API License URL");    return apiinfo; }}

api Jump Controller

/* Copyright (c) 2015. Lorem ipsum dolor sit amet, consectetur adipiscing elit. * Morbi non lorem porttitor neque feugiat blandit. Ut Vitae ipsum eget quam lacinia accumsan. * etiam sed turpis ac ipsum condimentum Fringilla. Maecenas Magna. * Proin dapibus sapien vel ante. Aliquam erat volutpat. Pellentesque sagittis ligula eget metus. * Vestibulum Commodo. Ut rhoncus gravida Arcu. */import Com.mangofactory.swagger.annotations.apiignore;import com.meituan.hotel.common.constants.LogConstants; Import Org.apache.commons.httpclient.httpclient;import Org.apache.commons.httpclient.httpmethod;import Org.apache.commons.httpclient.methods.getmethod;import Org.apache.commons.logging.log;import Org.apache.commons.logging.logfactory;import Org.springframework.stereotype.controller;import org.springframework.web.bind.annotation.RequestMapping; Import Javax.servlet.http.httpservletrequest;import Javax.servlet.http.httpservletresponse;import Java.io.IOException; /** * API Access Controller * using HttpClientTransit Request * Created by Liuxinyu on 15/4/24. */@Controller @apiignore@requestmapping ("/api") public class Springfoxcontroller {private final static Log logger = Log    Factory.getlog (Logconstants.log_level_info);    Private final static String COLON = ":";     Private final static String Api_doc = "/api-docs";        @RequestMapping ("/") public void API (HttpServletRequest request, httpservletresponse response) throws IOException {        Gets the URL address of String Requrl = Request.getrequesturl (). toString ();        Logger.info ("API page Request:" + Requrl);        Requrl = Requrl.replace ("API", "API") + "/index.html";        HttpClient client = new HttpClient ();        HttpMethod method = new GetMethod (Requrl);        Client.executemethod (method);         API URL String apiurl = request.getservername () + COLON + request.getserverport () + Api_doc;        String html = method.getresponsebodyasstring (); The HTML page content is processed so that it can access the static resource String BODY = Html.replace ("Petstore.swagger.io/v2/swagger.JSON ", Apiurl). Replace (" css/","/api/css/"). Replace (" lib/","/api/lib/"). R        Eplace ("Swagger-ui.js", "/api/swagger-ui.js");        Response.getwriter (). write (body);    Response.getwriter (). Flush (); }}
Third, the effect

Access $server: $port/api/


SPRINGMVC Project Access Springfox

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.