spring bootではSwagger 2でAPI説明文書を構築します.
3998 ワード
もっと読む
maven:
設定ファイル:
controllerクラスで:
あなたのプロジェクトを梱包してWEBサーバに配置し、
アクセスアドレス
http://localhost:8080/your-contextpath/appi-docsは、注釈生成されたAPIの説明を見ることができる.
アクセスアドレス
http://localhost:8080/your-contextpath/swaggar-ui)API情報の使い方が見られます.
maven:
io.springfox
springfox-swagger2
2.2.2
io.springfox
springfox-swagger-ui
2.2.2
設定ファイル:
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 Swagger2Configuration {
@Bean
public Docket buildDocket() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(buildApiInf())
.select()
.apis(RequestHandlerSelectors.basePackage("com.site.portal.web"))// API(Controller)
.paths(PathSelectors.any())
.build();
}
private ApiInfo buildApiInf() {
return new ApiInfoBuilder()
.title("Spring Boot Swagger2 UI API ")
.contact(" ")
.version("1.0")
.build();
}
}
controllerクラスで:
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
@Api(value = " ",description=" ")
@RestController
@RequestMapping("/cities")
public class CityController {
@Autowired
private CityService cityService;
@ApiOperation(" ")
@RequestMapping
public PageInfo getAll(City city) {
List countryList = cityService.getAll();
return new PageInfo(countryList);
}
@ApiOperation(" ")
@RequestMapping(value = "/add")
public City add() {
return new City();
}
@RequestMapping(value = "/view/{id}")
public City view(@PathVariable Integer id) {
ModelAndView result = new ModelAndView();
City city = cityService.getById(id);
return city;
}
@ApiOperation(" ")
@RequestMapping(value = "/delete/{id}")
public ModelMap delete(@PathVariable Integer id) {
ModelMap result = new ModelMap();
cityService.deleteById(id);
result.put("msg", " !");
return result;
}
@ApiOperation(" ")
@RequestMapping(value = "/save")
public void save() {
ModelMap result = new ModelMap();
City city=new City();
city.setName("beijing"+System.currentTimeMillis());
city.setState("1");
String msg = city.getId() == null ? " !" : " !";
cityService.save(city);
result.put("city", city);
result.put("msg", msg);
}
}
あなたのプロジェクトを梱包してWEBサーバに配置し、
アクセスアドレス
http://localhost:8080/your-contextpath/appi-docsは、注釈生成されたAPIの説明を見ることができる.
アクセスアドレス
http://localhost:8080/your-contextpath/swaggar-ui)API情報の使い方が見られます.