我正在使用Spring Boot和嵌入式Tomcat,并且UrlRewriteFilter类找不到配置文件urlrewrite.xml,因此此类使用servletcontext.getResourceAsStream(this.confPath)而我在其他文章中读到,当包是罐子时,这种方法不起作用。有人有这个问题吗?Spring引导程序无法在jar文件中找到urlrewrite.xml
2
A
回答
0
我在Spring Boot中使用了Tuckey UrlRewriteFilter。我遵循这个stack overflow question(不是答案)中的初始问题实现。 我还添加了
registrationBean.addInitParameter("confPath", "urlrewrite.xml");
来设置urlrewrite.xml文件中的特定路径。 在项目中,我把文件放在src/main/webapp中。
这种方式在使用'mvn spring-boot:run'时会起作用,我认为它满足您在jar中运行的需要。在我的项目中,我建立了一个战争档案,这也是可行的。
1
发现在blog post
import org.springframework.core.io.Resource;
import org.tuckey.web.filters.urlrewrite.Conf;
import org.tuckey.web.filters.urlrewrite.UrlRewriteFilter;
//Adding @Component Annotation to a Filter is enough to register the Filter, when you have no web.xml
@Component
public class MyUrlRewriteFilter extends UrlRewriteFilter {
private static final String CONFIG_LOCATION = "classpath:/urlrewrite.xml";
//Inject the Resource from the given location
@Value(CONFIG_LOCATION)
private Resource resource;
//Override the loadUrlRewriter method, and write your own implementation
@Override
protected void loadUrlRewriter(FilterConfig filterConfig) throws ServletException {
try {
//Create a UrlRewrite Conf object with the injected resource
Conf conf = new Conf(filterConfig.getServletContext(), resource.getInputStream(), resource.getFilename(), "@@[email protected]@");
checkConf(conf);
} catch (IOException ex) {
throw new ServletException("Unable to load URL rewrite configuration file from " + CONFIG_LOCATION, ex);
}
}
}
-1
下面的代码为我工作。
请使用下面的依赖关系:
<dependency>
<groupId>org.tuckey</groupId>
<artifactId>urlrewritefilter</artifactId>
<version>4.0.4</version>
</dependency>
创建资源文件夹urlrewrite.xml:
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE urlrewrite
PUBLIC "-//tuckey.org//DTD UrlRewrite 3.0//EN"
"http://www.tuckey.org/res/dtds/urlrewrite3.0.dtd">
<urlrewrite>
<rule>
<name>Domain Name Check</name>
<condition name="host" operator="notequal">www.userdomain.com</condition>
<from>^(.*)$</from>
<to type="redirect">http://www.userdomain.com$1</to>
</rule>
</urlrewrite>
添加以下主ApplicationRunner.java:
@Bean
public FilterRegistrationBean tuckeyRegistrationBean() {
final FilterRegistrationBean registrationBean = new FilterRegistrationBean();
registrationBean.setFilter(new CustomURLRewriter());
return registrationBean;
}
,创造CustomURLRewriter:
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.core.io.ClassPathResource;
import org.tuckey.web.filters.urlrewrite.Conf;
import org.tuckey.web.filters.urlrewrite.UrlRewriteFilter;
import org.tuckey.web.filters.urlrewrite.UrlRewriter;
import javax.servlet.*;
import java.io.InputStream;
public class CustomURLRewriter extends UrlRewriteFilter {
private UrlRewriter urlRewriter;
@Autowired
Environment env;
@Override
public void loadUrlRewriter(FilterConfig filterConfig) throws ServletException {
try {
ClassPathResource classPathResource = new ClassPathResource("urlrewrite.xml");
InputStream inputStream = classPathResource.getInputStream();
Conf conf1 = new Conf(filterConfig.getServletContext(), inputStream, "urlrewrite.xml", "");
urlRewriter = new UrlRewriter(conf1);
} catch (Exception e) {
throw new ServletException(e);
}
}
@Override
public UrlRewriter getUrlRewriter(ServletRequest request, ServletResponse response, FilterChain chain) {
return urlRewriter;
}
@Override
public void destroyUrlRewriter() {
if(urlRewriter != null)
urlRewriter.destroy();
}
}
+0
任何理由将其标记为一个否定的回答信息? –
0
我不以此为荣,但这对我有效。库本身一次使用ClassLoader.getSystemResourceAsStream()
来尝试解析文件名,所以我认为这将工作,除非库更新/更改的那一天。
String fileName = "urlrewrite.xml";
// The prefixed version seems to be needed when running within a jar.
// Rumor has it that it may be a tomcat 8 specific.
String confPath = java.lang.ClassLoader.getSystemResourceAsStream(fileName) != null ? fileName : "/WEB-INF/classes/" + fileName;
registrationBean.addInitParameter("confPath", confPath);
相关问题
- 1. 无法在/WEB-INF/urlrewrite.xml中找到urlrewrite conf文件
- 2. org.tuckey.web.filters.urlrewrite.UrlRewriteFilter错误:无法找到urlrewrite.xml
- 3. 引导程序无法运行,找不到文件
- 4. Spring引导可执行文件jar无法解析freemarker模板
- 5. 在Angular应用程序中找不到引导程序文件
- 6. 无法在GAE应用程序中导入jar文件
- 7. Spring引导自动配置无法将属性文件从jar绑定到bean
- 8. 在Eclipse中导出Spring Boot应用程序作为JAR文件
- 9. SonarQube Visual Studio引导程序无法找到程序集
- 10. jar文件无法找到org.eclipse.swt.widgets.Display
- 11. 无法找到cordova-3.5.0.jar文件
- 12. Spring应用程序无法找到“com.tibco.tibjms.naming.TibjmsInitialContextFactory”
- 13. Spring引导应用程序无法在Eclipse中启动Oxygen
- 14. RestAssured自动化 - 无法找到确切的jar文件导入
- 15. sqoop导入无法找到sqoop-1.4.6.jar
- 16. 无法在web应用程序中找到witten文件(tomcat,Spring MVC)
- 17. Java无法在Linux中的.jar文件中找到类
- 18. Spring 4.0.0M2:无法找到Spring.jar文件。
- 19. Spring Boot jsp文件无法找到
- 20. Eclipse无法找到Spring配置文件
- 21. 应用程序无法使用One-Jar查找XSD文件
- 22. Spring Boot不会在jsp文件中加载引导程序
- 23. SRVE9967W:无法在jar文件中找到清单类路径xercesImpl.jar
- 24. 猪寄存器 - 无法在hdfs中找到jar文件
- 25. Java classloader无法在jar文件中找到资源
- 26. 我无法在mysql-connector.jar.zip中找到jar文件
- 27. 无法禁用Spring引导应用程序中的ContextCredentialsAutoConfiguration
- 28. Spring引导应用程序。起动器类找不到.xml文件
- 29. FXCop - 无法找到引用程序集
- 30. 我在哪里可以找到.NET引导程序文件?
这个问题https://github.com/spring-projects/spring-boot/issues/3627 – goat