java shop_Javashop官方博客

1.maven依赖

(第一个后台/v2/api-do,第二个UI页面引用的jar包,版本2.6.1)

io.springfox

springfox-swagger2

2.6.1

io.springfox

springfox-swagger-ui

2.6.1

2.Swagger配置文件

@Configuration    // 配置注解,自动在本类上下文加载一些环境变量信息

@EnableSwagger2   // 使swagger2生效

@EnableWebMvc

@ComponentScan(basePackages = {“com.enation.app.javashop.xiaochengxu.action.api”})  //需要扫描的包路径

public class SwaggerConfig extends WebMvcConfigurationSupport {

@Bean

public Docket createRestApi() {

return new Docket(DocumentationType.SWAGGER_2)

.select()

.apis(RequestHandlerSelectors.any())

.paths(PathSelectors.any())

.build();

}

}

3.web.xml配置说明

(说明:Springmvc前端控制器扫描路径增加“/v2/api-docs”,用于扫描Swagger的 /v2/api-docs,否则 /v2/api-docs无法生效,备注spring的映射一定要是/,否则swagger2中需要引入相关静态资源可能会因为找不到而导致页面加载失败。)

spring-servlet

/swagger-resources

spring-servlet

/v2/api-docs

spring-servlet

/swagger-resources/configuration/ui

spring-servlet

/swagger-resources/configuration/security

4.spring-mvc.xml 增加自动扫描Swagger

(Spring的url-pattern映射关系为 /:映射到classpath:/META-INF/resources/下的一个jar里面有swagger-ui.html,用于springmvc自动扫描那个模板和相关js)

5.freemarker拦截的处理办法

// 前台处理器

if (uri.endsWith(“.html”) && !(uri.indexOf(“swagger”) >= 0)){

eopProcessor = new FacadeProcessor();

}

6.Controller中使用注解添加API文档

@RestController

@RequestMapping(“/api/xcx/adv”)

@Api(description = “获取首页广告接口”)

@Validated

public class AdvApiController {

@Autowired

private IAdvManager advManager;@ApiOperation(value = “根据某个广告id获取广告信息接口”, notes = “根据某个广告id获取广告信息”)

@ApiImplicitParams({ @ApiImplicitParam(name = “advid”, value = “广告id”, required = true, paramType = “path”, dataType = “Long”), })

@RequestMapping(value = “/{advid}”, method = RequestMethod.GET)

public Adv detail(@PathVariable Long advid) {return this.advManager.getAdvDetail(advid);

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值