我正在使用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