我有一个带有方法getApplication()的应用程序控制器。此方法为我的客户端返回applicationJson objekt。 在这个方法中,我调用了一个应用程序构建器。
应用程序构建器是工厂并生成特殊的应用程序构建器对象。 在特殊应用程序构建器中使用映射器和服务,使用的服务是daos。
所有服务和daos都在applicationContext.xml和serviceBeans.xml中查找bean并确认。
调用方法时,getApplication()在我的eclipse java控制台中是一个例外。
问题出现在MobileApplicationBuilder类和方法buildPagenaviData()
中Class ApplicationController:
package core.application.controller;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import core.application.builder.ApplicationBuilder;
import core.application.builder.MobileApplicationBuilder;
import core.application.factories.ApplicationControllerProcessHelperFactory;
import core.application.mapper.json.ApplicationJson;
import core.application.process.helper.MobileApplicationBuildHelper;
import core.base.abstracts.ControllerAbstract;
import core.security.model.interfaces.SystemSettingsModelInterface;
import core.security.service.interfaces.SystemLanguagesServiceInterface;
import core.security.service.interfaces.SystemSettingsServiceInterface;
import core.security.service.interfaces.SystemUserServiceInterface;
/**
*
* Class: {@link ApplicationController}
*
* Über diesen Controller wird die Anwendung zusammengebaut und als großes JSON Objekt an den Client geschickt.
*
*/
@Path("/app")
@Component
@Scope("session")
public class ApplicationController extends ControllerAbstract {
private static final String JSON_PATH = System.getProperty("wtp.deploy") + "/decon_fm_version5/WEB-INF/classes/json/";
@Autowired
private ApplicationBuilder applicationBuilder;
@Autowired
@Qualifier("systemUserService")
private SystemUserServiceInterface systemUserService;
@Autowired
@Qualifier("systemLanguagesService")
private SystemLanguagesServiceInterface languagesService;
@Autowired
@Qualifier("systemSettingsService")
private SystemSettingsServiceInterface systemSettings;
private ApplicationJson applicationJson = null;
private String test;
public String getTest() {
return test;
}
public void setTest(String test) {
this.test = test;
}
/**
* Process für den Build der gesamten Applikation.
* Es wird ein gemapptes JSON Objekt zurück gegeben welches dann auf dem Client verarbeitet werden kann.
* Größere Build Logiken werden in eine Helper Klasse ausgelagert, welche über eine entsprechende Factory geladen wird.
* @return
*/
@GET
@Path("/build")
@Produces(MediaType.APPLICATION_JSON + ";charset=utf-8")
public ApplicationJson getApplication() {
// auslesen und Zuwiesung der User Settings
SystemSettingsModelInterface settingsModel = systemSettings.getSettingsByUser(Long.parseLong(session.getAttribute("idUser").toString()));
systemUserService.model().setSettings(settingsModel);
applicationBuilder.setClientApp(settingsModel.getClientView())
.setSession(session)
.setUserModel(this.deconSecurity.getUser());
applicationJson = applicationBuilder.build();
return applicationJson;
}
}
Class ApplicationBuilder:
package core.application.builder;
import javax.servlet.http.HttpSession;
import org.apache.commons.lang.WordUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import core.application.mapper.json.ApplicationJson;
import core.security.model.interfaces.SystemUserModelInterface;
import core.security.service.interfaces.SystemLanguagesServiceInterface;
public class ApplicationBuilder {
/**
* Klassennamen Suffix für jeden Builder
*/
private static final String CLASS_SUFFIX = "ApplicationBuilder";
/**
* http session objekt aus dem controller
*/
private HttpSession session;
/**
* Client Application die vom Nutzer eingestellt wurde
*/
private String clientApp;
private SystemUserModelInterface userModel;
@Autowired
@Qualifier("systemLanguagesService")
private SystemLanguagesServiceInterface languageService;
/**
* Auszuführender Builder gegen Interface Sicherung
*/
private ApplicationBuilderInterface builder;
public ApplicationJson build() {
ApplicationJson application = null;
try {
builder = (ApplicationBuilderInterface) Class.forName("core.application.builder." + WordUtils.capitalize(clientApp) + CLASS_SUFFIX).newInstance();
builder.setSession(session);
builder.setUserModel(userModel);
builder.setLanguages(languageService.getLanguages());
builder.setFavorite(userModel.getSettings().getFavorite());
application = builder.build();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
return application;
}
public ApplicationBuilder setClientApp(String clientApp) {
this.clientApp = clientApp;
return this;
}
public ApplicationBuilder setSession(HttpSession session) {
this.session = session;
return this;
}
public ApplicationBuilder setUserModel(SystemUserModelInterface userModel) {
this.userModel = userModel;
return this;
}
}
类MobileApplicationBuilder:
package core.application.builder;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.List;
import org.codehaus.jackson.JsonParseException;
import org.codehaus.jackson.map.JsonMappingException;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import core.application.mapper.json.ApplicationAnimateDataJson;
import core.application.mapper.json.ApplicationIconbarJson;
import core.application.mapper.json.ApplicationJson;
import core.application.mapper.json.ApplicationPagenaviJson;
import core.application.mapper.json.Formular;
import core.application.mapper.json.MobileApplicationJson;
import core.cache.model.interfaces.ApplicationCacheModelInterface;
import core.cache.service.ApplicationCacheService;
@Component
@Scope("session")
public class MobileApplicationBuilder extends ApplicationBuilderAbstract implements ApplicationBuilderInterface {
private static final String JSON_PATH = System.getProperty("wtp.deploy") + "/decon_fm_version5/WEB-INF/classes/json/";
private MobileApplicationJson mapper = new MobileApplicationJson();
@Autowired
@Qualifier("applicationCacheService")
private ApplicationCacheService applicationCacheService;
public ApplicationJson build() {
mapper.setStartPage(getFavorite())
.setSystemLangguages(getLanguages())
.setSystemUser(getUserModel());
buildIconbar();
buildAnimateData();
buildPagenaviData();
return mapper;
}
/**
* Zusammenbau sämtlicher Iconbars im System
*/
protected void buildIconbar() {
ApplicationIconbarJson iconbarMapper = new ApplicationIconbarJson();
File directory = new File(JSON_PATH + "/iconbars/");
File[] files = directory.listFiles();
Formular formsMapper = new Formular();
for (int i = 0; i < files.length; i++) {
try {
byte[] iconbar = Files.readAllBytes(Paths.get(files[i].getAbsolutePath()));
iconbarMapper = getObjectMapper().readValue(iconbar, ApplicationIconbarJson.class);
mapper.setIconbars(iconbarMapper.getName().toString(), iconbarMapper);
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* Zusammenbau der Animate Pages für die Application
*/
protected void buildAnimateData() {
ApplicationAnimateDataJson aadnMapper = new ApplicationAnimateDataJson();
try {
byte[] animateData = Files.readAllBytes(Paths.get(JSON_PATH + "anmiate_data.json"));
aadnMapper = getObjectMapper().readValue(animateData, ApplicationAnimateDataJson.class);
mapper.setAnimateData(aadnMapper);
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* Zusammenbau des Objektes für Daten der Module welche in der Applikation verfügbar sein sollen
*/
protected void buildPagenaviData() {
String component = "pagenaviData";
// ApplicationCacheServiceInterface cacheService = new ApplicationCacheService();
// here arises a problem
ApplicationCacheModelInterface cache = applicationCacheService.getCacheResource(
Long.parseLong(session.getAttribute("idUser").toString()),
component,
deconSecurity().getUser().getSettings().getClientView());
if (cache == null) {
List<ApplicationPagenaviJson> list = null;
File directory = new File(JSON_PATH + "/pagesnavi/");
File[] files = directory.listFiles();
for (int i = 0; i < files.length; i++) {
try {
String key = files[i].getName().substring(0, files[i].getName().indexOf("."));
byte[] navi = Files.readAllBytes(Paths.get(files[i].getAbsolutePath()));
list = getObjectMapper().readValue(navi, getObjectMapper().getTypeFactory().constructCollectionType(
List.class, ApplicationPagenaviJson.class));
mapper.setPagenaviData(key, list);
} catch (IOException e) {
e.printStackTrace();
}
}
// cache Module Event um die Daten für die Pagenavi nach Rechten zu kontrollieren und entsprechend in den Cache zu speichern
eventDispatcher().dispatch("cleanAndCachePagenaviData", this);
} else {
try {
HashMap<String, List<ApplicationPagenaviJson>> map = getObjectMapper().readValue(
cache.getJsonObject(),
HashMap.class);
mapper.setPagenaviData(map);
} catch (JsonParseException e) {
e.printStackTrace();
} catch (JsonMappingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
类ApplicationCacheService:
package core.cache.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import core.cache.dao.interfaces.ApplicationCacheDaoInterface;
import core.cache.model.interfaces.ApplicationCacheModelInterface;
import core.cache.service.interfaces.ApplicationCacheServiceInterface;
/**
* Class: ApplicationCacheService
*
* Service Klasse für das verwalten einer Application Cache Speicherung
*
*/
@Service
public class ApplicationCacheService implements ApplicationCacheServiceInterface {
@Autowired
@Qualifier("applicationCacheDao")
private ApplicationCacheDaoInterface cacheDao;
public void saveCacheObject(Object obj) {
cacheDao.saveCacheObject(obj);
}
public boolean isCacheUpdated(Long id, String applicationComponent) {
return cacheDao.isCacheUpdated(id, applicationComponent);
}
public ApplicationCacheModelInterface getCacheResource(Long idUser, String applicationComponent, String applicationType) {
return cacheDao.getCacheResource(idUser, applicationComponent, applicationType);
}
}
Class ApplicationCacheDao:
package core.cache.dao;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import core.base.abstracts.DeconDaoAbstract;
import core.cache.dao.interfaces.ApplicationCacheDaoInterface;
import core.cache.model.ApplicationCacheModel;
import core.cache.model.interfaces.ApplicationCacheModelInterface;
/**
* Class: ApplicationCacheDao
*
* DAO Klasse für das speichern, auslesen und verwalten der ApplicationCache Tabelle
*
*/
@Repository
@Transactional
public class ApplicationCacheDao extends DeconDaoAbstract implements ApplicationCacheDaoInterface {
public void saveCacheObject(Object obj) {
try {
super.getSession().save(obj);
} catch (Exception e) {
e.printStackTrace();
}
}
public boolean isCacheUpdated(Long id, String applicationComponent) {
boolean isUpdated = false;
String hql = "from ApplicationCacheModel where idUser = " + id + " AND applicationComponent = '" + applicationComponent + "'";
List<ApplicationCacheModel> list = super.getSession().createQuery(hql).list();
if (list.size() == 0) {
isUpdated = true;
} else {
ApplicationCacheModel obj = list.get(0);
if (obj.isUpdate() != false) {
isUpdated = true;
}
}
return isUpdated;
}
public ApplicationCacheModelInterface getCacheResource(Long idUser, String applicationComponent, String applicationType) {
String hql = "from ApplicationCacheModel where idUser = " + idUser + " AND applicationComponent = '" + applicationComponent + "' AND applicationType = '" + applicationType +"'";
List<ApplicationCacheModel> list = super.getSession().createQuery(hql).list();
return (list.size() > 0) ? list.get(0) : null;
}
}
serviceBeans.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:context="http://www.springframework.org/schema/context"
xmlns:oxm="http://www.springframework.org/schema/oxm"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/oxm
http://www.springframework.org/schema/oxm/spring-oxm-3.1.xsd">
<!-- User Service -->
<bean id="systemUserService" class="core.security.service.SystemUserService"></bean>
<bean id="systemUserModel" class="core.security.model.SystemUserModel" />
<bean id="systemUserDao" class="core.security.dao.SystemUserDao" />
<!-- SystemRolesService -->
<bean id="systemRoleService" class="core.security.service.SystemRoleService"></bean>
<bean id="systemRoleModel" class="core.security.model.SystemRoleModel"></bean>
<bean id="systemRoleDao" class="core.security.dao.SystemRoleDao"></bean>
<!-- SystemGroupService -->
<bean id="systemGroupService" class="core.security.service.SystemGroupService"></bean>
<bean id="systemGroupModel" class="core.security.model.SystemGroupModel"></bean>
<bean id="systemGroupDao" class="core.security.dao.SystemGroupDao"></bean>
<!-- SystemLanguagesService -->
<bean id="systemLanguagesService" class="core.security.service.SystemLanguagesService"></bean>
<bean id="systemLanguagesModel" class="core.security.model.SystemLanguagesModel"></bean>
<bean id="systemLanguagesDao" class="core.security.dao.SystemLanguagesDao"></bean>
<!-- System Setting Service -->
<bean id="systemSettingsService" class="core.security.service.SystemSettingsService"></bean>
<bean id="systemSettingsModel" class="core.security.model.SystemSettingsModel"></bean>
<bean id="systemSettingsDao" class="core.security.dao.SystemSettingsDao"></bean>
<!-- ApplicationCacheService -->
<bean id="applicationCacheModel" class="core.cache.model.ApplicationCacheModel"></bean>
<bean id="applicationCacheDao" class="core.cache.dao.ApplicationCacheDao"></bean>
<bean id="applicationCacheService" class="core.cache.service.ApplicationCacheService"></bean>
</beans>
的applicationContext.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:context="http://www.springframework.org/schema/context"
xmlns:oxm="http://www.springframework.org/schema/oxm"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/oxm
http://www.springframework.org/schema/oxm/spring-oxm-3.1.xsd">
<context:annotation-config />
<context:component-scan base-package="core.application.controller" />
<context:component-scan base-package="core.security.controller" />
<context:component-scan base-package="core.cache.controller" />
<import resource="database/hibernate.cfg.xml"/>
<import resource="serviceBeans.xml"/>
<bean id="objectMapper" class="org.codehaus.jackson.map.ObjectMapper"></bean>
<bean id="jsonFactory" class="org.codehaus.jackson.JsonFactory"></bean>
<bean id="mobileJsonMapper" class="core.application.mapper.json.MobileApplicationJson"></bean>
<bean id="desktopJsonMapper" class="core.application.mapper.json.DesktopApplicationJson"></bean>
<bean id="mobileApplicationBuilder" class="core.application.builder.MobileApplicationBuilder">
</bean>
<bean id="desktopApplicationBuilder" class="core.application.builder.DesktopApplicationBuilder"></bean>
<bean id="applicationBuilder" class="core.application.builder.ApplicationBuilder"></bean>
<bean id="deconModuleConfigParser" class="core.base.beans.DeconModuleConfigParser"></bean>
<bean id="deconSessionManager" class="core.base.beans.DeconSessionManager" scope="session"></bean>
<bean id="eventConfigLoader" class="core.event.dispatcher.EventConfigLoader">
<constructor-arg ref="deconModuleConfigParser" />
</bean>
<bean id="eventFactory" class="core.event.dispatcher.EventFactory"></bean>
<bean id="eventDispatcher" class="core.event.dispatcher.EventDispatcher">
<constructor-arg ref="eventFactory" />
<constructor-arg ref="eventConfigLoader" />
</bean>
<bean id="deconSecurity" class="core.base.beans.DeconSecurity"></bean>
<!-- <bean id="sce" class="javax.servlet.ServletContextEvent"></bean> -->
<!-- <import resource="jsonMapperBeans.xml"/> -->
</beans>
的web.xml
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<display-name>Decon FM Version 5</display-name>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:META-INF/spring/applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<listener>
<listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
</listener>
<listener>
<listener-class>core.base.listeners.DeconSession</listener-class>
</listener>
<servlet>
<servlet-name>Rest Service</servlet-name>
<servlet-class>com.sun.jersey.spi.spring.container.servlet.SpringServlet</servlet-class>
<init-param>
<param-name>com.sun.jersey.config.property.packages</param-name>
<param-value>
core.application.controller;
core.security.controller;
core.cache.controller;
org.codehaus.jackson.jaxrs
</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Rest Service</servlet-name>
<url-pattern>/api/*</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>30</session-timeout>
</session-config>
</web-app>
异常消息:
SEVERE: The RuntimeException could not be mapped to a response, re-throwing to the HTTP container
java.lang.NullPointerException
at core.application.builder.MobileApplicationBuilder.buildPagenaviData(MobileApplicationBuilder.java:94)
at core.application.builder.MobileApplicationBuilder.build(MobileApplicationBuilder.java:47)
at core.application.builder.ApplicationBuilder.build(ApplicationBuilder.java:49)
at core.application.controller.ApplicationController.getApplication(ApplicationController.java:81)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at com.sun.jersey.spi.container.JavaMethodInvokerFactory$1.invoke(JavaMethodInvokerFactory.java:60)
at com.sun.jersey.server.impl.model.method.dispatch.AbstractResourceMethodDispatchProvider$TypeOutInvoker._dispatch(AbstractResourceMethodDispatchProvider.java:185)
at com.sun.jersey.server.impl.model.method.dispatch.ResourceJavaMethodDispatcher.dispatch(ResourceJavaMethodDispatcher.java:75)
at com.sun.jersey.server.impl.uri.rules.HttpMethodRule.accept(HttpMethodRule.java:302)
at com.sun.jersey.server.impl.uri.rules.RightHandPathRule.accept(RightHandPathRule.java:147)
at com.sun.jersey.server.impl.uri.rules.ResourceClassRule.accept(ResourceClassRule.java:108)
at com.sun.jersey.server.impl.uri.rules.RightHandPathRule.accept(RightHandPathRule.java:147)
at com.sun.jersey.server.impl.uri.rules.RootResourceClassesRule.accept(RootResourceClassesRule.java:84)
at com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1480)
at com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1411)
at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1360)
at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1350)
at com.sun.jersey.spi.container.servlet.WebComponent.service(WebComponent.java:416)
at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:538)
at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:716)
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:502)
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(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
感谢您的帮助。
答案 0 :(得分:1)
我对您的代码进行了概述,我发现了问题。让我先解释为什么空指针。 在您的代码中,创建了两个容器;春天和泽西岛。 当执行Jersey时,你试图从spring容器中获取bean,这是不可能的,所以你需要一个像(Application Context)这样的超级容器来检索spring容器中的bean。
解决方案在春天很简单。 解决方案:使用此WebApplicationContextUtils.getRequiredWebApplicationContext.getBean(“beanName”);
,而不是自动装配为依赖注入。这只是一种方式,通过应用程序上下文有很多其他的依赖注入方式。尝试理解它将帮助您进入spring bean创建核心的问题。休息此解决方案应该工作。干杯