springboot整合swagger2

1.导入jar包:

<!--swagger2-->
		<dependency>
			<groupId>io.springfox</groupId>
			<artifactId>springfox-swagger2</artifactId>
			<version>2.6.1</version>
		</dependency>
		<dependency>
			<groupId>io.springfox</groupId>
			<artifactId>springfox-swagger-ui</artifactId>
			<version>2.6.1</version>
		</dependency>

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 Swagger2 {
	
	@Bean
	 public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage(""))
                .paths(PathSelectors.any())
                .build();
    }
	
    private ApiInfo apiInfo() {
    	return new ApiInfoBuilder()
                .title("结合swagger使用,实现restful api文档")
                .description("")
                .termsOfServiceUrl("https://blog.csdn.net/xxx")
                .version("1.0")
                .build();
    }

}

3.在controller层写注解:

@ApiOperation("创建用户")
	@ApiImplicitParam(name="user", value="用户实体", required=true, dataType="User")
	@PostMapping("/add")
	@ResponseBody
	public ServerResponse<String> add(@ModelAttribute User user) {
		ServerResponseDTO<String> response = userService.saveRegister(user);
		if(response.isSuccess()) {
			return ServerResponse.createBySuccess();
		}
		return ServerResponse.createByErrorMessage(response.getMsg());
	}
	
	@ApiOperation("根据id获取用户信息")
	@ApiImplicitParam(name="id", value="用户Id", required=true, dataType="Integer", paramType="path")
	@GetMapping("/{id}")
	@ResponseBody
	public ServerResponse<User> getUserById(@PathVariable Integer id) {
		User user = userService.getUserById(id);
		if(null != user) {
			return ServerResponse.createBySuccess(user);
		}
		return ServerResponse.createByErrorMessage("用户不存在");
	}
	
	@ApiOperation("根据id删除用户信息")
	@ApiImplicitParam(name="id", value="用户Id", required=true, dataType="Integer", paramType="path")
	@DeleteMapping("/{id}")
	@ResponseBody
	public ServerResponse<User> deleteUserById(@PathVariable Integer id) {
		ServerResponseDTO<String> deleteResponse = userService.deleteUserById(id);
		if(null != deleteResponse) {
			return ServerResponse.createBySuccess();
		}
		return ServerResponse.createByErrorMessage(deleteResponse.getMsg());
	}
	
	@ApiOperation(value="更新用户详细信息", notes="根据url的id指定更新用户,并根据传过来的user值来更新用户信息")
	@ApiImplicitParams({
		@ApiImplicitParam(name="id", value="用户Id", required=true, dataType="Integer", paramType="path"),
		@ApiImplicitParam(name="user", value="用户详细信息实体user", required=true, dataType="User")
	})
	@PutMapping("/{id}")
	@ResponseBody
	public ServerResponse<User> updateUserById(@PathVariable Integer id, @ModelAttribute User user) {
		user.setId(id);
		ServerResponseDTO<User> updateResponse = userService.updateUser(user);
		if(updateResponse.isSuccess()) {
			return ServerResponse.createBySuccess();
		}
		return ServerResponse.createByErrorMessage(updateResponse.getMsg());
	}

4.访问地址:

http://localhost:8080/shop/swagger-ui.html

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值