漂亮直观的rest API文档, 不容错过,swagger
gitHub :https://github.com/springfox/springfox
1.maven的配置
<dependency>
<groupId>com.mangofactory</groupId>
<artifactId>swagger-springmvc</artifactId>
<version>1.0.2</version>
</dependency>
2.设置自己的配置
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 com.mangofactory.swagger.configuration.SpringSwaggerConfig;
import com.mangofactory.swagger.models.dto.ApiInfo;
import com.mangofactory.swagger.plugin.EnableSwagger;
import com.mangofactory.swagger.plugin.SwaggerSpringMvcPlugin;
@Configuration
//@EnableWebMvc
@EnableSwagger
//@ComponentScan(basePackages ={"com.test.controller"})
// Loads the spring beans required by the framework
public class MySwaggerConfig
{
private SpringSwaggerConfig springSwaggerConfig;
/**
* Required to autowire SpringSwaggerConfig
*/
@Autowired
public void setSpringSwaggerConfig(SpringSwaggerConfig springSwaggerConfig)
{
this.springSwaggerConfig = springSwaggerConfig;
}
/**
* Every SwaggerSpringMvcPlugin bean is picked up by the swagger-mvc
* framework - allowing for multiple swagger groups i.e. same code base
* multiple swagger resource listings.
*/
@Bean
public SwaggerSpringMvcPlugin customImplementation()
{
return new SwaggerSpringMvcPlugin(this.springSwaggerConfig).apiInfo(apiInfo()).includePatterns(
".*?");
}
private ApiInfo apiInfo()
{
ApiInfo apiInfo = new ApiInfo(
"xxx系统API接口管理",
"各个controller下对应相应业务接口",
"My Apps API terms of service",
"xxx",
null,
null);
return apiInfo;
}
}
spring-mvc.xml
<bean class="com.chengkun.util.MySwaggerConfig" />
<mvc:annotation-driven />3.添加HTML和JS 文件 见附件
4.修改index.html的就JS 中的加载地址
http://localhost:8080/{projectName}/api-docs
5.添加注解
@RequestMapping(value = "updateStudent", method = RequestMethod.POST, produces = {"application/json; charset=utf-8","application/xml; charset=utf-8"})
@ApiOperation(value = "修改学生信息", httpMethod = "POST", notes = "学生信息以json格式传递", response = BaseResultVo.class)
public @ResponseBody String updateStudent(@ApiParam(required = true) @RequestParam String postData)
{
LOGGER.debug(String.format("enter function, %s", postData));
return buildSuccessResultInfo(1);
}
注解API 见:
https://github.com/swagger-api/swagger-core/wiki/Annotations
6.访问http://localhost:8080/{projectName}/index.html
7 ,在@Api注解中用中文名, 会出现404的情况;需要设置tomcat 的编码格式 URIEncoding="UTF-8"