我有一个属性文件url.properties
位于maven webapp项目的src/main/resources
文件夹中,并希望从类中读取它。我该怎么做?我尝试使用以下代码库但给了我错误。
tax.service.url=http://secdevapp11.gspt.net:8080/istore-tax-service/rest/tax
public static String getTaxServiceUrl() {
String taxServiceUrl = "";
try {
Properties props = GenericUtils.loadProperties("/url.properties");
log.debug("props = " + props);
taxServiceUrl = props.getProperty("tax.service.url");
log.debug("taxServiceUrl = " + taxServiceUrl);
} catch (IOException ioe) {
log.debug("Failed to read properties file: url.properties");
}
return taxServiceUrl;
}
public static Properties loadProperties(String fileName) throws IOException {
Properties prop = new Properties();
InputStream in = ClassLoader.getSystemResourceAsStream(fileName);
prop.load(in);
return prop;
}
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.3</version>
<configuration>
<failOnMissingWebXml>true</failOnMissingWebXml>
<nonFilteredFileExtensions>
<nonFilteredFileExtension>gif</nonFilteredFileExtension>
<nonFilteredFileExtension>ico</nonFilteredFileExtension>
<nonFilteredFileExtension>jpg</nonFilteredFileExtension>
<nonFilteredFileExtension>png</nonFilteredFileExtension>
<nonFilteredFileExtension>pdf</nonFilteredFileExtension>
</nonFilteredFileExtensions>
<webResources>
<resource>
<directory>src/main/webapp</directory>
<filtering>true</filtering>
</resource>
</webResources>
</configuration>
</plugin>
SEVERE: Servlet.service() for servlet [CheckoutPaymentController] in context with path [/istore-mvc2-webapp] threw exception
java.lang.NullPointerException
at java.util.Properties$LineReader.readLine(Properties.java:434)
at java.util.Properties.load0(Properties.java:353)
at java.util.Properties.load(Properties.java:341)
at com.istore.utils.GenericUtils.loadProperties(GenericUtils.java:319)
at com.istore.web.service.client.TaxClient.getTaxServiceUrl(TaxClient.java:44)
at com.istore.web.service.client.TaxClient.getTaxAmount(TaxClient.java:19)
at com.istore.utils.TaxCalculator.getSalesTax(TaxCalculator.java:16)
at com.istore.web.controllers.CheckoutPaymentController.getTaxableAmt(CheckoutPaymentController.java:52)
at com.istore.web.controllers.CheckoutPaymentController.processRequest(CheckoutPaymentController.java:72)
at com.istore.web.controllers.CheckoutPaymentController.doPost(CheckoutPaymentController.java:175)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:647)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:581)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1023)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:724)
答案 0 :(得分:1)
从文件名前面取“/”。您不是在路径上查找文件,而是要求类加载器按名称查找资源,并且您的文件名为“url.properties”,而不是“/url.properties”。