尝试读取WEB-INF中的属性文件时,InputStream为null

时间:2016-08-10 10:47:07

标签: java spring spring-mvc tomcat

当我尝试读取位于&#34; / WEB-INF&#34;中的属性文件时,我的<% if I18n.locale == :en %> moment.locale('en'); <% end %> <% if I18n.locale == :es %> moment.locale('es'); <% end %> 对象使控制台打印出NullPointerException。但是当我InputStream时,它打印出InputStream.toString()而不是实际的java.io.FileInputStream@1933968值。

这是我回来的堆栈跟踪:

null

SEVERE: Servlet.service() for servlet [spring] in context with path [/translator] threw exception [Request processing failed; nested exception is java.lang.NullPointerException] with root cause java.lang.NullPointerException at com.webclaims.translator.TranslatorApi.readPropertiesFile(TranslatorApi.java:149) at com.webclaims.translator.TranslatorApi.writePropertiesFile(TranslatorApi.java:128) at com.webclaims.translator.controllers.TestController.editableWebpage(TestController.java:45) ;

的布局
/WEB-INF

我在使用Java 8的Tomcat v8服务器上运行此项目。

弹簧servlet.xml中;

WEB-INF-|
        |-jsp
        |-configDummy.properties
        |-spring-servlet.xml
        |-web.xml

的web.xml

<?xml version="1.0" encoding="UTF-8"?>  
<beans xmlns="http://www.springframework.org/schema/beans"    
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    
xmlns:p="http://www.springframework.org/schema/p"    
xmlns:mvc="http://www.springframework.org/schema/mvc"   
xmlns:context="http://www.springframework.org/schema/context"    
xsi:schemaLocation="http://www.springframework.org/schema/beans    
                    http://www.springframework.org/schema/beans/spring-beans-4.3.xsd  
                    http://www.springframework.org/schema/mvc   
                    http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
                    http://www.springframework.org/schema/context    
                    http://www.springframework.org/schema/context/spring-context-4.3.xsd">

<mvc:annotation-driven/> 
<context:component-scan base-package="com.webclaims.*" />
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/WEB-INF/jsp/"></property>
    <property name="suffix" value=".jsp"></property>
</bean>
<bean id="propertiesFile" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="properties" value="/WEB-INF/configDummy.properties"></property>
</bean>

TestController.java(包含例外入口点)

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1">
  <display-name>translator</display-name>

  <context-param>
    <param-name>contextConfigLocation</param-name> 
    <param-value>/WEB-INF/spring-servlet.xml</param-value> 
  </context-param>
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener> 
  <servlet>
    <servlet-name>spring</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>spring</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>
</web-app>

TranslatorApi.java

@Controller
@PropertySource("/WEB-INF/configDummy.properties")
public class TestController {

private static final Logger LOGGER = Logger.getLogger(TestController.class.getName());

private static final String PROPS_FILE = "/WEB-INF/configDummy.properties";

@RequestMapping(value = "/test")
public ModelAndView originalWebpage() {
    return new ModelAndView("testpage");
}

@CrossOrigin()
@RequestMapping(value = "/test", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE)
public @ResponseBody String editableWebpage(@RequestBody String data, HttpServletRequest request) {
    InputStream inputStream = request.getSession().getServletContext()
            .getResourceAsStream(PROPS_FILE);
    System.out.println(inputStream);

    JsonParser parser = new JsonParser(data);
    Elements elements = parser.jsonToElements();
    TranslatorApi t = new TranslatorApi();
    t.writePropertiesFile(elements, inputStream);
    return data;
}
}

这里的大多数人都建议我将属性文件放在类路径中。如果我有它,我将来可以写它吗?

1 个答案:

答案 0 :(得分:1)

对我而言,您的NPE与properties中的TranslatorApi字段null更为相关,因为它未正确注入。

尝试使用下一课Environment代替:

@Autowired
private Environment properties;

以下是how to inject properties in your class的一个很好的例子。