springboot 返回apidoc html踩的坑

项目用apidoc做接口文档,之前一直用的swagger,可是怕我坑惨了

  • 先说下apidoc的搭建,网上教程很多

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    1. Install node.js

    To check if node.js is successfully installed:
    λ node --version
    v10.15.0

    2. After node.js installed, install apiDoc.

    npm install apidoc -g

    To check if apiDoc is successfully installed:
    λ apidoc -h
    Usage: apidoc [options]

    Options:
    ......

    3. Set the apidoc configurations in apidoc.json.

    4. Write annotations above your functions in your project.

    5. Generate static api doc files.

    λ apidoc -i user\ -o user\src\main\webapp\apidoc

无非就是这五个步骤,不多说,执行apidoc -i user\ -o user\src\main\webapp\apidoc

后在webapp下会生成这样的文件:

1554969798307

其实直接点击index.html就可以打开接口文档页面,只是url是本地路径,不方便,部署之后也不方便访问,于是新建一个控制器来调度访问,如下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
@Controller
@RequestMapping(value = "/apidoc")
public class ApidocController {

private static final Logger LOGGER = LoggerFactory.getLogger(ApidocController.class);

@RequestMapping(value = "/index")
public void index(HttpServletRequest request, HttpServletResponse response) {
LOGGER.info("Go to apidoc index page");
try {
request.getRequestDispatcher("/apidoc/index.html").forward(request, response);
return;
} catch (ServletException | IOException e) {
e.printStackTrace();
}
}
}

springboot访问HTML需要依赖thymeleaf,在pom.xml加上依赖

1
2
3
4
5
<!--thymeleaf template-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>

然后问题来了,用springboot内置tomcat启动项目之后,是死活访问不到静态页面的,不行你可以试试,你会看见可爱的404

1554970128481

网上查了半天没找到原因,可能也就我一个人遇到这个情况吧,然后各种原因查找之后,用外置tomcat启动,发射 ——–>

1554970229736

你就看到它了,具体原因未查明也查不明,不倒腾了,反正线上部署肯定也是外置tomcat,影响不大,end.

0%