6-1 如何生成API接口文档

6-1 如何生成API接口文档

访问:

http://localhost:8000/course/cbv/list/

C:\Users\Administrator\PycharmProjects\drf_tutorial\drf_tutorial\drf_tutorial\settings.py

 

设置路由

报错:

"D:\Program Files\JetBrains\PyCharm 2017.1.3\bin\runnerw.exe" C:\Users\Administrator\Envs\chongkong_vir\Scripts\python.exe "D:\Program Files\JetBrains\PyCharm 2017.1.3\helpers\pydev\pydevd.py" --multiproc --qt-support --client 127.0.0.1 --port 51862 --file C:/Users/Administrator/PycharmProjects/drf_tutorial/drf_tutorial/manage.py runserver 8000
warning: Debugger speedups using cython not found. Run '"C:\Users\Administrator\Envs\chongkong_vir\Scripts\python.exe" "D:\Program Files\JetBrains\PyCharm 2017.1.3\helpers\pydev\setup_cython.py" build_ext --inplace' to build.
pydev debugger: process 7728 is connecting

Connected to pydev debugger (build 171.4424.42)
warning: Debugger speedups using cython not found. Run '"C:\Users\Administrator\Envs\chongkong_vir\Scripts\python.exe" "D:\Program Files\JetBrains\PyCharm 2017.1.3\helpers\pydev\setup_cython.py" build_ext --inplace' to build.
pydev debugger: process 9780 is connecting

Performing system checks...

Unhandled exception in thread started by <_pydev_bundle.pydev_monkey._NewThreadStartupWithTrace object at 0x000001CA4415FA20>
Traceback (most recent call last):
  File "D:\Program Files\JetBrains\PyCharm 2017.1.3\helpers\pydev\_pydev_bundle\pydev_monkey.py", line 589, in __call__
    return self.original_func(*self.args, **self.kwargs)
  File "C:\Users\Administrator\Envs\chongkong_vir\lib\site-packages\django\utils\autoreload.py", line 225, in wrapper
    fn(*args, **kwargs)
  File "C:\Users\Administrator\Envs\chongkong_vir\lib\site-packages\django\core\management\commands\runserver.py", line 120, in inner_run
    self.check(display_num_errors=True)
  File "C:\Users\Administrator\Envs\chongkong_vir\lib\site-packages\django\core\management\base.py", line 364, in check
    include_deployment_checks=include_deployment_checks,
  File "C:\Users\Administrator\Envs\chongkong_vir\lib\site-packages\django\core\management\base.py", line 351, in _run_checks
    return checks.run_checks(**kwargs)
  File "C:\Users\Administrator\Envs\chongkong_vir\lib\site-packages\django\core\checks\registry.py", line 73, in run_checks
    new_errors = check(app_configs=app_configs)
  File "C:\Users\Administrator\Envs\chongkong_vir\lib\site-packages\django\core\checks\urls.py", line 13, in check_url_config
    return check_resolver(resolver)
  File "C:\Users\Administrator\Envs\chongkong_vir\lib\site-packages\django\core\checks\urls.py", line 23, in check_resolver
    return check_method()
  File "C:\Users\Administrator\Envs\chongkong_vir\lib\site-packages\django\urls\resolvers.py", line 397, in check
    for pattern in self.url_patterns:
  File "C:\Users\Administrator\Envs\chongkong_vir\lib\site-packages\django\utils\functional.py", line 36, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "C:\Users\Administrator\Envs\chongkong_vir\lib\site-packages\django\urls\resolvers.py", line 536, in url_patterns
    patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
  File "C:\Users\Administrator\Envs\chongkong_vir\lib\site-packages\django\utils\functional.py", line 36, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "C:\Users\Administrator\Envs\chongkong_vir\lib\site-packages\django\urls\resolvers.py", line 529, in urlconf_module
    return import_module(self.urlconf_name)
  File "c:\users\administrator\appdata\local\programs\python\python35\lib\importlib\__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 986, in _gcd_import
  File "<frozen importlib._bootstrap>", line 969, in _find_and_load
  File "<frozen importlib._bootstrap>", line 958, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 673, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 673, in exec_module
  File "<frozen importlib._bootstrap>", line 222, in _call_with_frames_removed
  File "C:/Users/Administrator/PycharmProjects/drf_tutorial/drf_tutorial\drf_tutorial\urls.py", line 21, in <module>
    schema_view = get_schema_view(title="DRF API文档", description="XXX---")
  File "C:\Users\Administrator\Envs\chongkong_vir\lib\site-packages\rest_framework\schemas\__init__.py", line 47, in get_schema_view
    urlconf=urlconf, patterns=patterns, version=version
  File "C:\Users\Administrator\Envs\chongkong_vir\lib\site-packages\rest_framework\schemas\coreapi.py", line 120, in __init__
    assert coreapi, '`coreapi` must be installed for schema support.'
AssertionError: `coreapi` must be installed for schema support.

解决办法:

pip install coreapi==2.0.8
pip install coreschema

 

启动又报错:

"D:\Program Files\JetBrains\PyCharm 2017.1.3\bin\runnerw.exe" C:\Users\Administrator\Envs\chongkong_vir\Scripts\python.exe "D:\Program Files\JetBrains\PyCharm 2017.1.3\helpers\pydev\pydevd.py" --multiproc --qt-support --client 127.0.0.1 --port 52436 --file C:/Users/Administrator/PycharmProjects/drf_tutorial/drf_tutorial/manage.py runserver 8000
warning: Debugger speedups using cython not found. Run '"C:\Users\Administrator\Envs\chongkong_vir\Scripts\python.exe" "D:\Program Files\JetBrains\PyCharm 2017.1.3\helpers\pydev\setup_cython.py" build_ext --inplace' to build.
pydev debugger: process 9460 is connecting

Connected to pydev debugger (build 171.4424.42)
warning: Debugger speedups using cython not found. Run '"C:\Users\Administrator\Envs\chongkong_vir\Scripts\python.exe" "D:\Program Files\JetBrains\PyCharm 2017.1.3\helpers\pydev\setup_cython.py" build_ext --inplace' to build.
pydev debugger: process 2024 is connecting

Performing system checks...

Unhandled exception in thread started by <_pydev_bundle.pydev_monkey._NewThreadStartupWithTrace object at 0x00000131C3893E48>
Traceback (most recent call last):
  File "D:\Program Files\JetBrains\PyCharm 2017.1.3\helpers\pydev\_pydev_bundle\pydev_monkey.py", line 589, in __call__
    return self.original_func(*self.args, **self.kwargs)
  File "C:\Users\Administrator\Envs\chongkong_vir\lib\site-packages\django\utils\autoreload.py", line 225, in wrapper
    fn(*args, **kwargs)
  File "C:\Users\Administrator\Envs\chongkong_vir\lib\site-packages\django\core\management\commands\runserver.py", line 120, in inner_run
    self.check(display_num_errors=True)
  File "C:\Users\Administrator\Envs\chongkong_vir\lib\site-packages\django\core\management\base.py", line 364, in check
    include_deployment_checks=include_deployment_checks,
  File "C:\Users\Administrator\Envs\chongkong_vir\lib\site-packages\django\core\management\base.py", line 351, in _run_checks
    return checks.run_checks(**kwargs)
  File "C:\Users\Administrator\Envs\chongkong_vir\lib\site-packages\django\core\checks\registry.py", line 73, in run_checks
    new_errors = check(app_configs=app_configs)
  File "C:\Users\Administrator\Envs\chongkong_vir\lib\site-packages\django\core\checks\urls.py", line 13, in check_url_config
    return check_resolver(resolver)
  File "C:\Users\Administrator\Envs\chongkong_vir\lib\site-packages\django\core\checks\urls.py", line 23, in check_resolver
    return check_method()
  File "C:\Users\Administrator\Envs\chongkong_vir\lib\site-packages\django\urls\resolvers.py", line 397, in check
    for pattern in self.url_patterns:
  File "C:\Users\Administrator\Envs\chongkong_vir\lib\site-packages\django\utils\functional.py", line 36, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "C:\Users\Administrator\Envs\chongkong_vir\lib\site-packages\django\urls\resolvers.py", line 536, in url_patterns
    patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
  File "C:\Users\Administrator\Envs\chongkong_vir\lib\site-packages\django\utils\functional.py", line 36, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "C:\Users\Administrator\Envs\chongkong_vir\lib\site-packages\django\urls\resolvers.py", line 529, in urlconf_module
    return import_module(self.urlconf_name)
  File "c:\users\administrator\appdata\local\programs\python\python35\lib\importlib\__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 986, in _gcd_import
  File "<frozen importlib._bootstrap>", line 969, in _find_and_load
  File "<frozen importlib._bootstrap>", line 958, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 673, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 673, in exec_module
  File "<frozen importlib._bootstrap>", line 222, in _call_with_frames_removed
  File "C:/Users/Administrator/PycharmProjects/drf_tutorial/drf_tutorial\drf_tutorial\urls.py", line 18, in <module>
    from rest_framework.authtoken import views
  File "C:\Users\Administrator\Envs\chongkong_vir\lib\site-packages\rest_framework\authtoken\views.py", line 10, in <module>
    class ObtainAuthToken(APIView):
  File "C:\Users\Administrator\Envs\chongkong_vir\lib\site-packages\rest_framework\authtoken\views.py", line 25, in ObtainAuthToken
    description="Valid username for authentication",
TypeError: __new__() got an unexpected keyword argument 'schema'

 

pip install -i https://pypi.doubanio.com/simple/ django-rest-swagger
pip install -i https://pypi.doubanio.com/simple/ django-filter

 

访问:

http://localhost:8000/schema/

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Swagger 是一个用于构建、文档化和使用 RESTful Web 服务的开源工具。Swagger 有很多版本,其中 Swagger2 是其中最常用的一个版本。Swagger2 可以通过注解的方式生成 API 接口文档,这些注解包括 @Api、@ApiOperation、@ApiParam 等。 下面是使用 Swagger2 生成 API 接口文档的步骤: 1. 添加 Swagger2 依赖 在项目的 pom.xml 文件中添加 Swagger2 的依赖: ``` <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.9.2</version> </dependency> ``` 2. 配置 Swagger2 在 Spring Boot 应用的启动类上添加 @EnableSwagger2 注解开启 Swagger2 支持,并配置 Docket 对象: ``` @Configuration @EnableSwagger2 public class SwaggerConfig { @Bean public Docket api() { return new Docket(DocumentationType.SWAGGER_2) .select() .apis(RequestHandlerSelectors.any()) .paths(PathSelectors.any()) .build(); } } ``` 这个配置会扫描所有的 Controller 类,并生成 API 接口文档。 3. 添加 Swagger2 注解 在 Controller 类的方法上添加 Swagger2 注解,包括: - @Api:用于标识这个 Controller 类的作用和说明。 - @ApiOperation:用于标识这个方法的作用和说明。 - @ApiParam:用于标识方法参数的作用和说明。 示例代码: ``` @RestController @RequestMapping("/api") @Api(value = "HelloWorldController", description = "示例控制器") public class HelloWorldController { @GetMapping("/hello") @ApiOperation(value = "打招呼", notes = "向用户打招呼") public String hello(@ApiParam(name = "name", value = "用户名", required = true) @RequestParam String name) { return "Hello, " + name + "!"; } } ``` 4. 访问 Swagger UI 启动应用后,访问 http://localhost:8080/swagger-ui.html 可以看到 Swagger UI 界面,其中包含了生成API 接口文档。在这个界面中,可以查看 API 接口的详细信息、测试 API 接口等。 以上就是使用 Swagger2 生成 API 接口文档的步骤。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值