在Ubuntu系统上使用Swagger生成API文档,通常涉及以下几个步骤:
npm install -g swagger-ui
@nestjs/swagger
包来配置Swagger。import { DocumentBuilder, SwaggerModule } from '@nestjs/swagger';
async function bootstrap() {
const app = await NestFactory.create(AppModule);
const options = new DocumentBuilder()
.setTitle('My API')
.setDescription('My API description')
.setVersion('1.0')
.build();
const document = SwaggerModule.createDocument(app, options);
SwaggerModule.setup('api', app, document);
await app.listen(3000);
}
bootstrap();
@ApiOperation
、@ApiParam
等注解来描述你的API操作和参数。import { ApiOperation, ApiParam } from '@nestjs/swagger';
@ApiOperation({ summary: 'Add user', tags: ['User Management'] })
@Post()
addUser(@ApiParam({ name: 'user', description: 'User object', required: true }) user: User) {
// ...
}
http://localhost:3000/api
来查看Swagger UI界面。请注意,上述信息提供了一般性的指导,具体步骤可能会根据你使用的框架和工具有所不同。如果你使用的是特定的框架(如NestJS),请参考该框架的官方文档来获取更详细的指导。