html阅读404,SpringBoot访问doc.html页面404

默认情况下并不需要添加此配置即可访问

很多朋友在使用SpringBoot集成swagger-bootstrap-ui后,都无法访问doc.html界面,此时,你可能需要实现SpringBoot的WebMvcConfigurer接口,添加相关的ResourceHandler,代码如下:

@SpringBootApplication

public class SwaggerBootstrapUiDemoApplication implements WebMvcConfigurer{

@Override

public void addResourceHandlers(ResourceHandlerRegistry registry) {

registry.addResourceHandler("doc.html").addResourceLocations("classpath*:/META-INF/resources/");

registry.addResourceHandler("/webjars/**").addResourceLocations("classpath*:/META-INF/resources/webjars/");

}

}

或者

@SpringBootApplication

public class SwaggerBootstrapUiDemoApplication implements WebMvcConfigurer{

@Override

public void addResourceHandlers(ResourceHandlerRegistry registry) {

registry.addResourceHandler("doc.html").addResourceLocations("classpath:/META-INF/resources/");

registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");

}

}

如果你是使用的老的版本SpringBoot,通过继承WebMvcConfigurationSupport来扩展SpringBoot相关的配置,则把以上配置加在相应的addResourceHandlers方法中即可

推荐使用实现WebMvcConfigurer接口的方式来进行扩展

如果以上方式还是不行,建议开启Spring的Debug日志来进行跟踪,一般访问doc.html页面会出现如下日志(成功情况下):

2019-04-19 13:39:36,896 DEBUG (AbstractHandlerMethodMapping.java:312)- Looking up handler method for path /doc.html

2019-04-19 13:39:36,902 DEBUG (AbstractHandlerMethodMapping.java:322)- Did not find handler method for [/doc.html]

2019-04-19 13:39:36,921 DEBUG (AbstractUrlHandlerMapping.java:199)- Matching patterns for request [/doc.html] are [/**]

2019-04-19 13:39:36,922 DEBUG (AbstractUrlHandlerMapping.java:233)- URI Template variables for request [/doc.html] are {}

2019-04-19 13:39:36,923 DEBUG (AbstractUrlHandlerMapping.java:146)- Mapping [/doc.html] to HandlerExecutionChain with handler [ResourceHttpRequestHandler [locations=[class path resource [META-INF/resources/], class path resource [resources/], class path resource [static/], class path resource [public/], ServletContext resource [/]], resolvers=[org.springframework.web.servlet.resource.PathResourceResolver@da32f3]]] and 1 interceptor

2019-04-19 13:39:36,957 DEBUG (RequestContextFilter.java:104)- Cleared thread-bound request context: org.apache.catalina.connector.RequestFacade@28759ea7

2019-04-19 13:39:41,649 DEBUG (RequestContextFilter.java:114)- Bound request context to thread: org.apache.catalina.connector.RequestFacade@28759ea7

同理,在使用SpringMvc或者shiro等权限框架时,如果页面无法访问,配置doc.html属性即可

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你可以使用Apache POI库来将Spring Boot文档转换为docx格式并输出为文件流。下面是一个示例代码: ```java import org.apache.poi.xwpf.usermodel.*; import org.springframework.core.io.InputStreamResource; import org.springframework.http.HttpHeaders; import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; import java.io.ByteArrayOutputStream; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; @RestController public class DocxController { @GetMapping(value = "/docx", produces = MediaType.APPLICATION_OCTET_STREAM_VALUE) public ResponseEntity<InputStreamResource> generateDocx() throws IOException { // 读取Spring Boot文档 FileInputStream inputStream = new FileInputStream("path/to/spring-boot-doc.doc"); XWPFDocument document = new XWPFDocument(inputStream); // 创建输出流 ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); // 写入docx内容 document.write(outputStream); outputStream.close(); // 设置响应头 HttpHeaders headers = new HttpHeaders(); headers.add("Content-Disposition", "attachment; filename=spring-boot-doc.docx"); // 返回文件流 return ResponseEntity .ok() .headers(headers) .contentType(MediaType.APPLICATION_OCTET_STREAM) .body(new InputStreamResource(new ByteArrayInputStream(outputStream.toByteArray()))); } public void saveDocx() throws IOException { // 读取Spring Boot文档 FileInputStream inputStream = new FileInputStream("path/to/spring-boot-doc.doc"); XWPFDocument document = new XWPFDocument(inputStream); // 创建输出流 FileOutputStream outputStream = new FileOutputStream("path/to/output.docx"); // 写入docx内容 document.write(outputStream); outputStream.close(); } } ``` 你可以将以上代码放入Spring Boot项目中的控制器中,并访问`/docx`路径来生成并下载docx文件,或者调用`saveDocx()`方法将docx文件保存到指定路径。 请将代码中的`path/to/spring-boot-doc.doc`替换为实际的Spring Boot文档路径,`path/to/output.docx`替换为你希望保存输出文件的路径。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值