依赖
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>3.0.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>3.0.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-boot-starter</artifactId>
<version>3.0.0</version>
</dependency>
Swagger配置
package com.xxxx.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
@Configuration
@EnableSwagger2
public class SwaggerConfig {
private ApiInfo apiInfo() {
return new ApiInfoBuilder().title("API接口文档")
.description("使用swagger来测试接口文档")
.version("1.0.0")
.build();
}
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("com.xxxx"))
.paths(PathSelectors.any())
.build();
}
}
接口配置
package com.xxxx.controller;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import java.util.HashMap;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import com.xxxx.utils.ResultResponse;
@RestController
@Api(value = "Hello接口")
public class HelloController {
@Autowired
private ResultResponse rresp;
@RequestMapping(method = RequestMethod.POST, value = "/hello2/{id}")
@ApiOperation(value = "测试post请求", notes = "测试post请求")
public ResultResponse hello2(
@RequestParam(defaultValue = "jack", name = "name") String name,
@RequestPart("file") MultipartFile file,
@PathVariable(value = "id") Integer id,
@RequestParam(name = "age") Integer age) {
Map<String, Object> m = new HashMap<String, Object>();
m.put("age", age);
m.put("id", id);
rresp.setCode(200);
rresp.setMsg("成功");
rresp.setData(m);
System.out.println(rresp);
return rresp;
}
}