Ich bin sehr neu in SpringMVC und ich muss ein Projekt für die Arbeit in Spring MVC erstellen. Also bitte nimm mich mit.InputStream ist Null beim Versuch, eine Eigenschaftendatei in WEB-INF zu lesen
Mein InputStream
Objekt macht die Konsole Drucken eine NullPointerException immer wenn ich versuche, eine Eigenschaftendatei zu lesen, die in "/ WEB-INF" befindet. Aber wenn ich InputStream.toString()
mache, druckt es [email protected]
anstelle eines tatsächlichen null
Wertes aus. Hier
ist der Stack-Trace, die ich immer wieder:
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)
Das Layout /WEB-INF
;
WEB-INF-|
|-jsp
|-configDummy.properties
|-spring-servlet.xml
|-web.xml
Ich bin mit diesem Projekt auf einem Tomcat v8-Server mit Java 8.
feder servlet.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>
web.xml
<?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>
TestController.java (cotains die Ausnahme Einspeisepunkt)
@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;
}
}
TranslatorApi.java
@PropertySource("/WEB-INF/configDummy.properties")
public class TranslatorApi {
@Autowired
private Properties properties;
private static final Logger LOGGER = Logger.getLogger(TranslatorApi.class.getName());
public TranslatorApi() {}
public Properties getProperties() {
return properties;
}
public void setProperties(Properties properties) {
this.properties = properties;
}
public void writePropertiesFile(Elements elements, InputStream inputStream) {
System.out.println("write opertion executed");
System.out.println(elements);
try {
readPropertiesFile(inputStream);
for(Element e : elements) {
properties.setProperty(e.className(), e.text());
}
String path=Thread.currentThread().getContextClassLoader()
.getResource("src/main/webapp/WEB-INF/props/configDummy.properties").toString();
File file = new File(path);
FileOutputStream fileOut = new FileOutputStream(file);
properties.store(fileOut, "DUMMY");
fileOut.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
private void readPropertiesFile(InputStream inputStream) throws IOException {
try {
properties.load(inputStream);
inputStream.close();
} catch(FileNotFoundException e) {
LOGGER.log(Level.SEVERE, "FileNotFoundException has occured", e);
} catch(IOException e) {
LOGGER.log(Level.SEVERE, "IOException has occured", e);
}
}
Die meisten Leute hier empfehlen, dass ich die Eigenschaftendatei in den Klassenpfad einfügen soll. Wenn ich es da drin habe, kann ich in Zukunft darauf schreiben?
Ich konnte die Zeilennummer nicht sehen. Gerade sah 2 Zeile in der Methode readPropertiesFile, die tatsächlich die NullPointerException werfen. properties.load (inputStream); inputStream.close(); Es könnten die Eigenschaften NULL sein, nicht der InputStream. Die Eigenschaften wurden möglicherweise nicht autowired und war NULL – NangSaigon