spring boot 集成 swagger

swagger是用来测试接口用的,虽然有少许代码入侵但是个人认为是值得的

在spring boot中引入swagger首先在maven中引入swagger相应的包

        
            io.springfox
            springfox-swagger2
            2.7.0
        
        
            io.springfox
            springfox-swagger-ui
            2.7.0
        
        
            io.springfox
            springfox-bean-validators
            2.7.0
        

然后在项目中配置


@Configuration
@EnableSwagger2
public class Swagger2Config  {
    

    @Bean
    public Docket createRestApi() {

        List parameterList = new ArrayList<>();

        //初始化公共header参数
//        parameterList.add(new ParameterBuilder().name(JWTConfigurer.AUTHORIZATION)
//                .description("登录token").modelRef(new ModelRef("string")).parameterType("header")
//                .hidden(false).required(false).defaultValue("").build());


        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.zero.project.web.controller"))
                .paths(PathSelectors.ant("/**"))
                .build()
                .globalOperationParameters(parameterList);
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("base project api")
                .description("")
                .termsOfServiceUrl("localhost:8001")
                .version("v1")
                .build();
    }

}

以上就是swagger在spring boot中的集成,而swagger的使用是在controller层的

@Slf4j
@Api(value = "用户接口")
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserManager userManager;

    @ApiOperation(value = "登录")
    @PostMapping("/signIn")
    public CommonResult siginIn(@Valid @RequestBody SignInForm form, BindingResult result, HttpServletResponse response) {
        log.info("登录");
        return CommonResultTemplate.execute(() -> {
            if (result.hasErrors()) {
                throw new CommonException(result.getAllErrors().get(0).getDefaultMessage());
            }

            return userManager.signIn(form, response);
        });
    }
}

@Api和 @ApiOperation是对接口的说明,不添加注解swagger也可以正常使用的,要进入swagger界面调用接口只要在浏览器输入 http://localhost:项目端口/swagger-ui.html#!/就可以了

此外,如果你的项目中集成了security的话swagger可能会被拦截掉,这个时候就要再添加配置

@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true, securedEnabled = true)
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {

        http
                .csrf()
                .disable()
                .headers()
                .frameOptions()
                .disable()
                .and()
                .sessionManagement()
                .sessionCreationPolicy(SessionCreationPolicy.STATELESS)
                .and()
                .authorizeRequests()
                // swagger start
                .antMatchers("/swagger-resources").permitAll()
                .antMatchers("/swagger-resources/configuration/ui").permitAll()
                .antMatchers("/swagger-ui.html").permitAll()
                .antMatchers("/images/**").permitAll()
                .antMatchers("/webjars/**").permitAll()
                .antMatchers("/v2/api-docs").permitAll()
                .antMatchers("/configuration/ui").permitAll()
                .antMatchers("/configuration/security").permitAll()
                // swagger end
                .antMatchers("/services/**").permitAll()
                .antMatchers("/sign/**").permitAll()
                .antMatchers("/public/**").permitAll()
                .antMatchers("/management/**").permitAll()
                .antMatchers("/**").authenticated();

    }


}

你可能感兴趣的:(spring boot 集成 swagger)