我试图以编程方式设置Spring Boot应用程序上下文根.上下文根的原因是我们希望从中访问应用程序localhost:port/{app_name}并将所有控制器路径附加到它.
这是web-app的应用程序配置文件.
@Configuration
public class ApplicationConfiguration {
Logger logger = LoggerFactory.getLogger(ApplicationConfiguration.class);
@Value("${mainstay.web.port:12378}")
private String port;
@Value("${mainstay.web.context:/mainstay}")
private String context;
private Set<ErrorPage> pageHandlers;
@PostConstruct
private void init(){
pageHandlers = new HashSet<ErrorPage>();
pageHandlers.add(new ErrorPage(HttpStatus.NOT_FOUND,"/notfound.html"));
pageHandlers.add(new ErrorPage(HttpStatus.FORBIDDEN,"/forbidden.html"));
}
@Bean
public EmbeddedServletContainerFactory servletContainer(){
TomcatEmbeddedServletContainerFactory factory = new TomcatEmbeddedServletContainerFactory();
logger.info("Setting custom configuration for Mainstay:");
logger.info("Setting port to {}",port);
logger.info("Setting context to {}",context);
factory.setPort(Integer.valueOf(port));
factory.setContextPath(context);
factory.setErrorPages(pageHandlers);
return factory;
}
public String getPort() {
return port;
}
public void setPort(String port) {
this.port = …Run Code Online (Sandbox Code Playgroud) 我有一个Spring Boot Web应用程序,我想在我的Linode VPS(〜/ Dropbox/images)上的共享Dropbox目录中提供静态内容.我已经读过Spring Boot会自动提供静态内容
"classpath:/META-INF/resources/",
"classpath:/resources/",
"classpath:/static/",
"classpath:/public/",
Run Code Online (Sandbox Code Playgroud)
但当然我的Dropbox目录不在类路径上.
虽然我可以配置Apache来提供Dropbox文件夹中的图像,但我希望利用Spring Security来限制静态内容对经过身份验证的用户的访问.
我正在开发一个基于反应的单页面应用程序的弹簧后端,我正在使用react-router进行客户端路由.
在index.html页面旁边,后端提供路径上的数据/api/**.
为了从我的应用程序src/main/resources/public/index.html的根路径上提供我的index.html,/我添加了一个资源处理程序
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/").addResourceLocations("/index.html");
}
Run Code Online (Sandbox Code Playgroud)
我想要的是在没有其他路由匹配的情况下提供index.html页面,例如当我调用除了以外的路径时/api.
如何在春季配置这种全能路线?
spring spring-mvc catch-all single-page-application spring-boot
如何Cache-Control在Spring Boot中为静态资源添加HTTP头?
尝试在应用程序中使用过滤器组件,它正确地写入标头,但Cache-Control标头被覆盖.
@Component
public class CacheBustingFilter implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
}
@Override
public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain)
throws IOException, ServletException {
HttpServletResponse httpResp = (HttpServletResponse) resp;
httpResp.setHeader("Cache-Control", "no-cache, no-store, must-revalidate");
httpResp.setHeader("This-Header-Is-Set", "no-cache, no-store, must-revalidate");
httpResp.setHeader("Expires", "0");
chain.doFilter(req, resp);
}
Run Code Online (Sandbox Code Playgroud)
我在浏览器中得到的是:
Cache-Control:no-store
This-Header-Is-Set:no-cache, no-store, must-revalidate
Expires:0
Run Code Online (Sandbox Code Playgroud)
我想要的是:
Cache-Control:no-cache, no-store, must-revalidate
This-Header-Is-Set:no-cache, no-store, must-revalidate
Expires:0
Run Code Online (Sandbox Code Playgroud) 我想改变我的招摇的UI从路径localhost:8080/swagger-ui.html到
localhost:8080/myapi/swagger-ui.html在springboot
重定向是无奈的我
我需要为指定 root 中的所有路由提供静态html文件。我试图用注释控制器的方法,但它仅适用于路由,而不是' ,等...(/src/main/resources/static/folder/index.html)(as example '/main/\**')@RequestMapping("/main/**")'/main'/main/foo''/main/foo/bar'
那么,我如何在 Spring Boot 中做到这一点?
我有一个Spring Boot应用程序。
我没有使用@EnableWebMvc,并且我的资源位于该src/main/resources/static文件夹中。当我尝试加载时,localhost:8080/ui/它只是下载一个随机文件(类型:八位字节流)。如果我直接使用/ui/index.html它的确可以。
我也在使用WebSecurityConfigurerAdapter,但这似乎不是问题的原因。
有人遇到过吗?我希望在我请求时加载index.html文件localhost:8080/ui/
我如何开始申请:
package my.package;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
//@EnableWebMvc //Breaks js resource loading
@SpringBootApplication
@EnableJpaRepositories
@EnableZuulProxy
public class MyApplication {
public static void main(String[] args) {
SpringApplication.run(MyApplication.class, args);
}
}
Run Code Online (Sandbox Code Playgroud)
我的pom.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>nl.jaarsma</groupId>
<artifactId>my-application</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>my-application</name>
<description>Example app</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.9.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> …Run Code Online (Sandbox Code Playgroud) 我有一个使用Spring Boot 2.1.3版本开发的微服务,并且我还使用 SpringFox 2.9.2 版作为 Swagger 文档。每次当我向第三方或任何其他人分发或部署时,我都必须始终提及 swagger url,以便用户通过 REST 端点。我的问题是如何在 spring 启动的情况下创建默认重定向 url,以便它应该自动重定向到 swagger-ui.html。这意味着如果用户在浏览器中输入http://localhost:8080,浏览器应该自动重定向到 url ie。http://localhost:8080/api/swagger-ui.html。我想知道这是否需要任何配置?
在使用 stackoverflow 之前,我浏览了以下链接并尝试过,但没有任何效果。
Java Spring Boot:如何将我的应用程序根目录(“/”)映射到 index.html?
更改作为战争部署的 spring-boot 应用程序的默认欢迎页面
我也尝试了不同的方法,但我总是得到 404 或白标错误页面。我想知道有什么方法可以防止白标错误页面应该自动重定向到 swagger 页面,即。http://localhost:8080/api/swagger-ui.html。
我还在 application.properties 中添加了以下内容。
server.servlet.context-path=/api
Run Code Online (Sandbox Code Playgroud)
请在这方面帮助我。
spring-boot ×8
java ×5
spring ×5
spring-mvc ×5
apache ×1
catch-all ×1
dropbox ×1
swagger ×1
swagger-2.0 ×1
swagger-ui ×1