Exception in thread "management-client-thread 1-2" java.lang.OutOfMemoryError: Java heap space running an Arquillian test on a managed JBoss7.1.1Final - junit

I'm getting the exception above while running a JUnit/Arquillian test in Eclipse Juno having a #Deployment method like this:
#Deployment
public static Archive<?> createDeployment() throws Exception {
File[] libs = Maven.resolver()
.loadPomFromFile("pom.xml")
.importRuntimeDependencies()
.as(File.class);
WebArchive war = ShrinkWrap.create(WebArchive.class, "test.war")
.addClass(Permission.class)
.addClass(PermissionInterface.class)
.addClass(PermissionModel.class)
.addClass(PermissionModelInterface.class)
...
.addAsLibraries(libs)
.addAsResource(
"resources-jbossas-managed/test-persistence.xml",
"META-INF/persistence.xml")
.addAsWebResource(
EmptyAsset.INSTANCE,
ArchivePaths.create("beans.xml"));
System.out.println(war.toString(true));
return war;
}
and a persistence unit described like this:
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0"
xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://java.sun.com/xml/ns/persistence
http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
<persistence-unit name="talmudDatabaseTest">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<jta-data-source>java:/Alpha2Test</jta-data-source>
<properties>
<property name="eclipselink.target-server" value="JBoss" />
<property name="eclipselink.target-database" value="MySQL" />
<property name="eclipselink.ddl-generation" value="create-tables" />
<property name="eclipselink.ddl-generation.output-mode" value="database" />
<property name="eclipselink.weaving" value="static" />
<property name="eclipselink.logging.level" value="FINEST" />
<property name="eclipselink.deploy-on-startup" value="True" />
<property name="eclipselink.session-event-listener" value="it.cnr.ilc.omega.model.PersistenceEventListener" />
<property name="omega.import-sql" value="false" />
<property name="omega.import-sql.postlogin-filename" value="preconstruct.sql" />
<property name="omega.import-sql.postcreate-filename" value="postconstruct.sql" />
</properties>
</persistence-unit>
</persistence>
The running JBoss 7.1.1 Final instance has been launched with this configuration (on a Macbook Pro):
-server -Xms512m -Xmx1024m -XX:MaxPermSize=512m -Djava.net.preferIPv4Stack=true -Dorg.jboss.resolver.warning=true -Dsun.rmi.dgc.client.gcInterval=3600000 -Dsun.rmi.dgc.server.gcInterval=3600000 -Djboss.modules.system.pkgs=org.jboss.byteman -Djava.awt.headless=true "-Dorg.jboss.boot.log.file=/usr/local/jboss-as-7.1.1.Final/standalone/log/boot.log" "-Dlogging.configuration=file:/usr/local/jboss-as-7.1.1.Final/standalone/configuration/logging.properties" "-Djboss.home.dir=/usr/local/jboss-as-7.1.1.Final" "-Dfile.encoding=UTF-8" "-Djboss.modules.system.pkgs=com.apple.laf,com.apple.laf.resources"
The #Test method isn't even called... the exception is thrown before it, but after the completion of the #Deployment method. Shrinkwrap creates this web archive:
test.war:
/beans.xml
/WEB-INF/
/WEB-INF/lib/
/WEB-INF/lib/atmosphere-compat-tomcat-1.1.0.beta3.jar
/WEB-INF/lib/bcprov-jdk14-1.38.jar
/WEB-INF/lib/jackson-databind-2.0.5.jar
/WEB-INF/lib/bcmail-jdk14-1.38.jar
/WEB-INF/lib/maven-aether-provider-3.0.5.jar
/WEB-INF/lib/shrinkwrap-impl-base-1.1.2.jar
/WEB-INF/lib/jcl-over-slf4j-1.6.4.jar
/WEB-INF/lib/shrinkwrap-resolver-spi-maven-2.0.0-beta-3.jar
/WEB-INF/lib/shrinkwrap-resolver-api-2.0.0-beta-3.jar
/WEB-INF/lib/maven-plugin-registry-2.0.9.jar
/WEB-INF/lib/shrinkwrap-spi-1.1.1.jar
/WEB-INF/lib/maven-model-builder-3.0.5.jar
/WEB-INF/lib/plexus-container-default-1.0-alpha-9-stable-1.jar
/WEB-INF/lib/geronimo-stax-api_1.0_spec-1.0.jar
/WEB-INF/lib/maven-plugin-api-2.0.9.jar
/WEB-INF/lib/poi-3.7.jar
/WEB-INF/lib/bcmail-jdk14-138.jar
/WEB-INF/lib/wagon-http-shared4-2.4.jar
/WEB-INF/lib/bcprov-jdk14-138.jar
/WEB-INF/lib/xmlbeans-2.3.0.jar
/WEB-INF/lib/maven-profile-2.0.9.jar
/WEB-INF/lib/poi-ooxml-schemas-3.7.jar
/WEB-INF/lib/aether-util-1.13.1.jar
/WEB-INF/lib/slf4j-log4j12-1.6.4.jar
/WEB-INF/lib/atmosphere-compat-jbossweb-1.1.0.beta3.jar
/WEB-INF/lib/jackson-core-2.0.5.jar
/WEB-INF/lib/commons-io-1.3.2.jar
/WEB-INF/lib/shrinkwrap-resolver-spi-2.0.0-beta-3.jar
/WEB-INF/lib/log4j-1.2.16.jar
/WEB-INF/lib/commons-digester-2.1.jar
/WEB-INF/lib/itext-2.1.7.js1.jar
/WEB-INF/lib/plexus-interpolation-1.14.jar
/WEB-INF/lib/classworlds-1.1-alpha-2.jar
/WEB-INF/lib/slf4j-simple-1.7.5.jar
/WEB-INF/lib/plexus-utils-2.0.6.jar
/WEB-INF/lib/annotation-detector-3.0.1.jar
/WEB-INF/lib/commons-codec-20041127.091804.jar
/WEB-INF/lib/atmosphere-compat-tomcat7-1.1.0.beta3.jar
/WEB-INF/lib/commons-collections-2.1.jar
/WEB-INF/lib/el-api-2.2.jar
/WEB-INF/lib/jdtcore-3.1.0.jar
/WEB-INF/lib/primefaces-3.5.jar
/WEB-INF/lib/commonj.sdo-2.1.1.v201112051852.jar
/WEB-INF/lib/shrinkwrap-resolver-impl-maven-2.0.0-beta-3.jar
/WEB-INF/lib/eclipselink-staticweave-maven-plugin-1.0.4.jar
/WEB-INF/lib/aether-spi-1.13.1.jar
/WEB-INF/lib/aether-impl-1.13.1.jar
/WEB-INF/lib/stax-api-1.0.1.jar
/WEB-INF/lib/jcommon-1.0.15.jar
/WEB-INF/lib/commons-logging-1.1.1.jar
/WEB-INF/lib/wagon-provider-api-2.4.jar
/WEB-INF/lib/xml-apis-1.4.01.jar
/WEB-INF/lib/plexus-compiler-javac-2.1.jar
/WEB-INF/lib/shrinkwrap-api-1.1.1.jar
/WEB-INF/lib/shrinkwrap-resolver-api-maven-2.0.0-beta-3.jar
/WEB-INF/lib/jsoup-1.6.3.jar
/WEB-INF/lib/plexus-component-api-1.0-alpha-33.jar
/WEB-INF/lib/maven-project-2.0.9.jar
/WEB-INF/lib/shrinkwrap-resolver-impl-maven-archive-2.0.0-beta-3.jar
/WEB-INF/lib/httpcore-4.2.3.jar
/WEB-INF/lib/maven-settings-builder-3.0.5.jar
/WEB-INF/lib/shrinkwrap-resolver-api-maven-archive-2.0.0-beta-3.jar
/WEB-INF/lib/maven-artifact-2.0.9.jar
/WEB-INF/lib/poi-ooxml-3.7.jar
/WEB-INF/lib/wagon-http-lightweight-2.4.jar
/WEB-INF/lib/dom4j-1.6.1.jar
/WEB-INF/lib/jena-core-2.10.0.jar
/WEB-INF/lib/slf4j-api-1.6.4.jar
/WEB-INF/lib/eclipselink-2.4.0.jar
/WEB-INF/lib/aether-connector-wagon-1.13.1.jar
/WEB-INF/lib/xercesImpl-2.10.0.jar
/WEB-INF/lib/castor-1.2.jar
/WEB-INF/lib/jfreechart-1.0.12.jar
/WEB-INF/lib/maven-repository-metadata-3.0.5.jar
/WEB-INF/lib/bctsp-jdk14-1.38.jar
/WEB-INF/lib/jasperreports-fonts-4.0.0.jar
/WEB-INF/lib/shrinkwrap-resolver-spi-maven-archive-2.0.0-beta-3.jar
/WEB-INF/lib/jena-tdb-0.10.0.jar
/WEB-INF/lib/commons-lang3-3.0.1.jar
/WEB-INF/lib/maven-artifact-manager-2.0.9.jar
/WEB-INF/lib/plexus-classworlds-2.4.jar
/WEB-INF/lib/maven-settings-3.0.5.jar
/WEB-INF/lib/commons-beanutils-1.8.0.jar
/WEB-INF/lib/jena-arq-2.10.0.jar
/WEB-INF/lib/httpclient-4.2.3.jar
/WEB-INF/lib/javax.persistence-2.0.4.v201112161009.jar
/WEB-INF/lib/aether-api-1.13.1.jar
/WEB-INF/lib/maven-model-3.0.5.jar
/WEB-INF/lib/atmosphere-runtime-1.1.0.beta3.jar
/WEB-INF/lib/plexus-compiler-api-2.1.jar
/WEB-INF/lib/jena-iri-0.9.5.jar
/WEB-INF/lib/jackson-annotations-2.0.5.jar
/WEB-INF/lib/jasperreports-5.0.0.jar
/WEB-INF/lib/wagon-file-2.4.jar
/WEB-INF/classes/
/WEB-INF/classes/META-INF/
/WEB-INF/classes/META-INF/persistence.xml
/WEB-INF/classes/it/
/WEB-INF/classes/it/cnr/
/WEB-INF/classes/it/cnr/ilc/
/WEB-INF/classes/it/cnr/ilc/omega/
/WEB-INF/classes/it/cnr/ilc/omega/resources/
/WEB-INF/classes/it/cnr/ilc/omega/resources/model/
/WEB-INF/classes/it/cnr/ilc/omega/resources/model/SourceInterface.class
/WEB-INF/classes/it/cnr/ilc/omega/resources/model/Source.class
/WEB-INF/classes/it/cnr/ilc/omega/resources/model/AttachmentInterface.class
/WEB-INF/classes/it/cnr/ilc/omega/resources/model/ResourceType.class
/WEB-INF/classes/it/cnr/ilc/omega/resources/model/ResourceInterface.class
/WEB-INF/classes/it/cnr/ilc/omega/resources/model/Resource.class
/WEB-INF/classes/it/cnr/ilc/omega/resources/model/Attachment.class
/WEB-INF/classes/it/cnr/ilc/omega/resources/model/ResourceTypeInterface.class
/WEB-INF/classes/it/cnr/ilc/omega/access/
/WEB-INF/classes/it/cnr/ilc/omega/access/LoginViewController.class
/WEB-INF/classes/it/cnr/ilc/omega/access/LoginViewControllerInterface.class
/WEB-INF/classes/it/cnr/ilc/omega/access/model/
/WEB-INF/classes/it/cnr/ilc/omega/access/model/Task.class
/WEB-INF/classes/it/cnr/ilc/omega/access/model/Role.class
/WEB-INF/classes/it/cnr/ilc/omega/access/model/Credentials.class
/WEB-INF/classes/it/cnr/ilc/omega/access/model/PermissionModel.class
/WEB-INF/classes/it/cnr/ilc/omega/access/model/TaskInterface.class
/WEB-INF/classes/it/cnr/ilc/omega/access/model/UserInterface.class
/WEB-INF/classes/it/cnr/ilc/omega/access/model/UserModelInterface.class
/WEB-INF/classes/it/cnr/ilc/omega/access/model/TaskModelInterface.class
/WEB-INF/classes/it/cnr/ilc/omega/access/model/PermissionInterface.class
/WEB-INF/classes/it/cnr/ilc/omega/access/model/PermissionModelInterface.class
/WEB-INF/classes/it/cnr/ilc/omega/access/model/User.class
/WEB-INF/classes/it/cnr/ilc/omega/access/model/LogoutEvent.class
/WEB-INF/classes/it/cnr/ilc/omega/access/model/TaskModel.class
/WEB-INF/classes/it/cnr/ilc/omega/access/model/Permission.class
/WEB-INF/classes/it/cnr/ilc/omega/access/model/UserModel.class
/WEB-INF/classes/it/cnr/ilc/omega/model/
/WEB-INF/classes/it/cnr/ilc/omega/model/AbstractEntityModel.class
/WEB-INF/classes/it/cnr/ilc/omega/model/AbstractEntityInterface.class
/WEB-INF/classes/it/cnr/ilc/omega/model/EntityDeleteEvent.class
/WEB-INF/classes/it/cnr/ilc/omega/model/EntityCreateEvent.class
/WEB-INF/classes/it/cnr/ilc/omega/model/EntityUpdateEvent.class
/WEB-INF/classes/it/cnr/ilc/omega/model/AbstractEntityModelInterface.class
/WEB-INF/classes/it/cnr/ilc/omega/model/AbstractEntity$Status.class
/WEB-INF/classes/it/cnr/ilc/omega/model/PersistenceEventListener.class
/WEB-INF/classes/it/cnr/ilc/omega/model/AbstractEntity.class
/WEB-INF/classes/it/cnr/ilc/omega/BaseController.class
/WEB-INF/classes/it/cnr/ilc/omega/Resources.class
/WEB-INF/classes/it/cnr/ilc/omega/reference/
/WEB-INF/classes/it/cnr/ilc/omega/reference/model/
/WEB-INF/classes/it/cnr/ilc/omega/reference/model/Reference.class
/WEB-INF/classes/it/cnr/ilc/omega/reference/model/ReferenceInterface.class
After that, Arquillian adds the archive on the active instance of JBoss, but the deployment can't terminate its operations. I've also managed to raise the memory for the JVM, but it doesn't seem to work. If I remove the libs form the archive, the #Test method is passed correctly, but the Session bean i'd like to inject:
#Inject
LoginViewController loginViewController;
isn't resolved like it should.
Can anyone help to find why the exception occurs? Thanks in advance... any help is appreciated!

.addAsWebResource(EmptyAsset.INSTANCE, ArchivePaths.create("beans.xml"));
should be
.addAsWebInfResource(EmptyAsset.INSTANCE, ArchivePaths.create("beans.xml"));

Related

Could not initialize Start up controller. TaskManager not found

i have written a scheduled task which call an Api in sequence in wso2 ei 6.4.0. my task is :
<task xmlns="http://ws.apache.org/ns/synapse"
name="GetMunicipalityAPIToken"
class="org.apache.synapse.startup.tasks.MessageInjector"
group="synapse.simple.quartz">
<trigger interval="60"/>
<property xmlns:task="http://www.wso2.org/products/wso2commons/tasks" name="message">
<test xmlns=""/>
</property>
<property xmlns:task="http://www.wso2.org/products/wso2commons/tasks"
name="sequenceName"
value="GetTokenFromMunicipalityService"/>
<property xmlns:task="http://www.wso2.org/products/wso2commons/tasks"
name="injectTo"
value="sequence"/>
</task>
and my sequence is :
<?xml version="1.0" encoding="UTF-8"?>
<sequence name="GetTokenFromMunicipalityService" xmlns="http://ws.apache.org/ns/synapse">
<send>
<endpoint>
<http method="POST" uri-template="http://x.x.x.x/api/MunicipalityBill/connect/token"/>
</endpoint>
</send>
</sequence>
but when i run integrator.bat in wso2eiHome/bin I got this error : "Could not initialize Start up controller. TaskManager not found" and the Api dont be called. Do i forget something ?
The error can be caused when you do not have org.wso2.carbon.mediation.ntask.NTaskTaskManager defined in the server. Could you please check whether the following is added in the synapse.xml file located in the [EI_HOME]/repository/deployment/server/synapse-configs/default/ directory
<definitions xmlns="http://ws.apache.org/ns/synapse">
<taskManager provider="org.wso2.carbon.mediation.ntask.NTaskTaskManager"/>
</definitions>

JPA/Hibernate: Missing table error

I am trying to build a simple application using Hibernate JPA with MySQL but I keep getting a "Missing table" error whenever I try to deploy it to Wildfly.
My persistent class looks like this:
package com.example;
#Entity #Table
public class FooBar {
//...
}
Here is my persistence.xml:
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
version="2.0">
<persistence-unit name="ExampleJPA">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<class>com.example.FooBar</class>
<properties>
<!-- Configuring JDBC properties -->
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/mydatabase" />
<property name="javax.persistence.jdbc.user" value="(my user)" />
<property name="javax.persistence.jdbc.password" value="(my password)" />
<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" />
<!-- Hibernate properties -->
<property name="hibernate.show_sql" value="true" />
<property name="hibernate.format_sql" value="true" />
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5InnoDBDialect" />
<property name="hibernate.hbm2ddl.auto" value="validate" />
<!-- Configuring Connection Pool -->
<property name="hibernate.c3p0.min_size" value="5" />
<property name="hibernate.c3p0.max_size" value="20" />
<property name="hibernate.c3p0.timeout" value="500" />
<property name="hibernate.c3p0.max_statements" value="50" />
<property name="hibernate.c3p0.idle_test_period" value="2000" />
</properties>
</persistence-unit>
</persistence>
And this is the message I get when deploying to a Wildfly 8 server:
org.jboss.msc.service.StartException in service jboss.persistenceunit.\"ExampleJPA.war#ExampleJPA\": org.hibernate.HibernateException: Missing table: FooBar
Caused by: org.hibernate.HibernateException: Missing table: FooBar
I do have a table called FooBar in the database. I can access it directly with JDBC using the same url, user and password defined in the persistence.xml. What is wrong with my JPA setup?
Your table doesn't exist in the schema, so the hibernate.hbm2ddl.auto value of "validate" fails. Either create the table or change the hibernate.hbm2ddl.auto value to "create" or "update".

Why is my EclipseLink Query Results Cache NOT working

I am developing a website on TomEE, and I want to use the EclipseLink Query Results Cache (L2) working, but every time I reload my web page, SELECT query is running (checked via general_log of mysql).
My Database entity looks like below:
#Entity(name="item")
#NamedQueries({
#NamedQuery(name="ItemEntity.getAllList",
query="Select distinct itemEntity from mypackage.entity.ItemEntity itemEntity",
hints={
#QueryHint(name="eclipselink.query-results-cache", value="true"),
#QueryHint(name="eclipselink.query-results-cache.size", value="1000"),
#QueryHint(name="eclipselink.query-results-cache.expiry", value="10000"), //10 secs for test but not working
#QueryHint(name="eclipselink.query-results-cache.type", value="FULL")
}
)
})
#org.eclipse.persistence.annotations.Cache(
type= CacheType.FULL,
size=10000,
expiry=60000, // 1 minute for test
coordinationType= CacheCoordinationType.INVALIDATE_CHANGED_OBJECTS
)
public class ItemEntity implements Serializable, Comparable {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
#Column(name="name", unique=true, nullable=false)
private String name;
/* getters and setters for fields */
public CompanyEntity(){}
#Override
public int compareTo(Object o) {.......}
}
My persistence.xml looks like below:
<?xml version="1.0" encoding="UTF-8" ?>
<persistence version="1.0"
xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">
<persistence-unit name="myprojectname-persistence-unit" transaction-type="JTA">
<jta-data-source>myprojectname-mysql-jdbc-jta-resource</jta-data-source>
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<properties>
<property name="eclipselink.target-database" value="org.eclipse.persistence.platform.database.MySQLPlatform"/>
<property name="eclipselink.cache.shared.default" value="true"/>
</properties>
</persistence-unit>
</persistence>
I read database table like below:
#Stateful
public class CompanyDao {
#PersistenceContext(unitName = "myprojectname-persistence-unit", type = PersistenceContextType.EXTENDED)
protected EntityManager em;
public List<CompanyEntity> getAllList(){
return this.em.createNamedQuery("ItemEntity.getAllList", ItemEntity.class).getResultList();
}
}
Dependency version details:
TomEE 1.7.2, Java EE6, openJPA 2.4.0, openEJB Java EE API 6.0-6, openEJB core 4.7.2, EclipseLink 2.6.2, MySQL 5.6.23, MySQL connector/J 5.1.38 (Tomcat connection pool)
I've looked at a similar question:
Can't get Eclipselink level 2 cache to work
but it doesn't describe how the OP managed to cache query results.
BTW, the default cache (L2), with em.find(ItemEntity.class, id); is working just fine.
What am I missing? Please help me out.
Solved.
I was using EclipseLink 2.6.2, I downgraded to version 2.4.2, and now Query Results Cache WORKS as expected!
I guess EclipseLink 2.6.x or 2.5.x is not quite compatible with JPA 2.0.x.
It's confusing, because when I use 2.5.x or higher, those still seem to be working, BESIDES the feature of Query Results Cache.
My persistence.xml looks like bellow now, so I can get log output:
<?xml version="1.0" encoding="UTF-8" ?>
<persistence version="1.0"
xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">
<persistence-unit name="myprojectname-persistence-unit" transaction-type="JTA">
<jta-data-source>myprojectname-mysql-jdbc-jta-resource</jta-data-source>
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<properties>
<property name="eclipselink.target-database" value="org.eclipse.persistence.platform.database.MySQLPlatform"/>
<property name="eclipselink.logging.logger" value="JavaLogger"/>
<!-- The warning log of "Problem while registering MBean: java.lang.NullPointerException" did not go away even if I set bellow 2 propertes -->
<!--
<property name="eclipselink.register.dev.mbean" value="false" />
<property name="eclipselink.register.run.mbean" value="false" />
-->
<property name="eclipselink.cache.shared.default" value="true"/>
<property name="eclipselink.logging.parameters" value="true"/>
<property name="eclipselink.logging.timestamp" value="true"/>
<property name="eclipselink.logging.session" value="true"/>
<property name="eclipselink.logging.thread" value="true"/>
<property name="eclipselink.logging.exceptions" value="true"/>
<property name="eclipselink.logging.level" value="FINEST"/>
</properties>
</persistence-unit>
</persistence>

Best way for logging exceptions in Spring.NET with Log4net or nlog

I would like to know which way of log exception in Spring.NET is prefered and why.
I found two common scenarios.
1.Use IThrowAdvice.
I created throws advice and in method AfterThrowing handle / log exception.
namespace Aspects
{
public class ExLogThrowsAdvice : IThrowsAdvice
{
private ILog _logger;
public ExLogThrowsAdvice()
{
_logger = LogManager.GetLogger("Error_file");
}
public void AfterThrowing(MethodInfo methodInfo,
Object []args, Object target, Exception exception)
{
_logger.Error(exception);
}
}
}
and use Common.Loggin API (Common Loggin API) for configuring for example Log4net for logging.
<sectionGroup name="common">
<section name="logging" type="Common.Logging.ConfigurationSectionHandler, Common.Logging"/>
</sectionGroup>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/>
<log4net>
<appender name="ErrorFileAppender"
type="log4net.Appender.FileAppender">
<file value="errors.txt"/>
<filter type="log4net.Filter.LevelRangeFilter">
<levelMin value="ERROR" />
<levelMax value="FATAL" />
</filter>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date%newline%username%newline[%thread] %message %newline"/>
</layout>
</appender>
<root>
<level value="ERROR"/>
<appender-ref ref="ErrorFileAppender"/>
</root>
</log4net>
And last, create a proxy for the object in the businees layer.
<!--ex log advice-->
<object id="theExLogAdvice" type="Aspects.ExLogThrowsAdvice, ExceptionLogging"/>
<!--auto proxy creator-->
<object type="Spring.Aop.Framework.AutoProxy.TypeNameAutoProxyCreator, Spring.Aop">
<property name="TypeNames" value="Aspects*"/>
<property name="InterceptorNames">
<list>
<value>theExLogAdvice</value>
</list>
</property>
</object>
This is first concept. The second which I found is to use aspect fo exception handling from the Spring Aspect library.
2.Exception aspects from Spring.NET
I would like create a handler for log exception and this handler will use the Log4net logger.
Handler for exception:
<object id="exLogHandler"
type="Spring.Aspects.Exceptions.LogExceptionHandler, Spring.Aop">
<property name="LogName" value="???"/>
<property name="LogLevel" value="Error"/>
</object>
and then use this handler in exception handle advice:
<object id="exLogAspect"
type="Spring.Aspects.Exceptions.ExceptionHandlerAdvice, Spring.Aop">
<property name="ExceptionHandlerDictionary">
<dictionary>
<entry key="log" ref="exLogHandler"/>
</dictionary>
</property>
<property name="ExceptionHandlers">
<list>
<value>on exception name SomeException log 'Ex:' + #e</value>
</list>
</property>
I am not sure if second way is good. Maybe it is stupidity.
It is possible configure LogExceptionHandler to use the Log4net logger?
I'm not sure if it's the best, but a SimpleLoggingAdvice logs exceptions for you. Furthermore, you can configure a SimpleLoggingAdvice to log execution time, method arguments and return values. Configuration looks like this (from the docs):
<object name="loggingAdvice" type="Spring.Aspects.Logging.SimpleLoggingAdvice, Spring.Aop">
<property name="LogUniqueIdentifier" value="true"/>
<property name="LogExecutionTime" value="true"/>
<property name="LogMethodArguments" value="true"/>
<property name="LogReturnValue" value="true"/>
<property name="Separator" value=";"/>
<property name="LogLevel" value="Info"/>
<property name="HideProxyTypeNames" value="true"/>
<property name="UseDynamicLogger" value="true"/>
</object>
Of course, you still have to configure a proxy factory and logging, but you know how to do that already.

how to configure spring mvc 3 to not return "null" object in json response?

a sample of json response looks like this:
{"publicId":"123","status":null,"partner":null,"description":null}
It would be nice to truncate out all null objects in the response. In this case, the response would become {"publicId":"123"}.
Any advice? Thanks!
P.S: I think I can do that in Jersey. Also I believe they both use Jackson as the JSON processer.
Added Later:
My configuration:
<?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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<!-- Scans the classpath of this application for #Components to deploy as beans -->
<context:component-scan base-package="com.SomeCompany.web" />
<!-- Application Message Bundle -->
<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basename" value="/WEB-INF/messages/messages" />
<property name="cacheSeconds" value="0" />
</bean>
<!-- Configures Spring MVC -->
<import resource="mvc-config.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:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<!-- Configures the #Controller programming model -->
<mvc:annotation-driven />
<!-- Forwards requests to the "/" resource to the "welcome" view -->
<!--<mvc:view-controller path="/" view-name="welcome"/>-->
<!-- Configures Handler Interceptors -->
<mvc:interceptors>
<!-- Changes the locale when a 'locale' request parameter is sent; e.g. /?locale=de -->
<bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor" />
</mvc:interceptors>
<!-- Saves a locale change using a cookie -->
<bean id="localeResolver" class="org.springframework.web.servlet.i18n.CookieLocaleResolver" />
<!--<bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
<property name="mediaTypes">
<map>
<entry key="atom" value="application/atom+xml"/>
<entry key="html" value="text/html"/>
<entry key="json" value="application/json"/>
<entry key="xml" value="text/xml"/>
</map>
</property>
<property name="viewResolvers">
<list>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/"/>
<property name="suffix" value=".jsp"/>
</bean>
</list>
</property>
<property name="defaultViews">
<list>
<bean class="org.springframework.web.servlet.view.json.MappingJacksonJsonView" />
<bean class="org.springframework.web.servlet.view.xml.MarshallingView" >
<property name="marshaller">
<bean class="org.springframework.oxm.jaxb.Jaxb2Marshaller" />
</property>
</bean>
</list>
</property>
</bean>
-->
<!-- Resolves view names to protected .jsp resources within the /WEB-INF/views directory -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/"/>
<property name="suffix" value=".jsp"/>
</bean>
My code:
#Controller
public class SomeController {
#RequestMapping(value = "/xyz", method = {RequestMethod.GET, RequestMethod.HEAD},
headers = {"x-requested-with=XMLHttpRequest","Accept=application/json"}, params = "!closed")
public #ResponseBody
List<AbcTO> getStuff(
.......
}
}
Yes, you can do this for individual classes by annotating them with #JsonSerialize(include=JsonSerialize.Inclusion.NON_NULL) or you can do it across the board by configuring your ObjectMapper, setting the serialization inclusion to JsonSerialize.Inclusion.NON_NULL.
Here is some info from the Jackson FAQ: http://wiki.fasterxml.com/JacksonAnnotationSerializeNulls.
Annotating the classes is straightforward, but configuring the ObjectMapper serialization config slightly trickier. There is some specific info on doing the latter here.
Doesn't answer the question but this is the second google result.
If anybody comes here and wants do do it for Spring 4 (as it happened to me), you can use the annotation
#JsonInclude(Include.NON_NULL)
on the returning class.
As mentioned in the comments, and in case anyone is confused, the annotation should be used in the class that will be converted to JSON.
If using Spring Boot for REST, you can do it in application.properties:
spring.jackson.serialization-inclusion=NON_NULL
source
Java configuration for the above. Just place the below in your #Configuration class.
#Bean
public Jackson2ObjectMapperBuilder objectMapperBuilder() {
Jackson2ObjectMapperBuilder builder = new Jackson2ObjectMapperBuilder();
builder.serializationInclusion(JsonInclude.Include.NON_NULL);
return builder;
}