Alfred’s Computing Weblog

Alfred Java-cored Computing Weblog


leave a comment »


I was setting up a Spring powered web application for testing.
I edited the configurations files (*.xml) to add in some new configuration for testing / POC.
The following error is shown when I deployed the WAR file in tomcat
org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException: Line 40 in XML document from ServletContext resource [/WEB-INF/applicationContext.xml] is invalid; nested exception is org.xml.sax.SAXParseException: cvc-complex-type.2.4.a: Invalid content was found starting with element 'bean'. One of '{"":description, "":meta, "":constructor-arg, "":property, "":qualifier, "":lookup-method, "":replaced-method, WC[##other:""]}' is expected.

As highlighted above, there is error in the mentioned XML file.
After I opened the XML file, I found that is some missed-closed bean definition ABOVE the mentioned line (Line 40 in XML).
The error bean definition as below:

<bean id="accountManager" class="alfred.sandbox.AccountManager">

Added in a BLACK SLASH “/” to close the bean definition properly will solve the error:

<bean id="accountManager" class="alfred.sandbox.AccountManager"/>

Written by Alfred

September 15, 2011 at 17:40

java.lang.NoClassDefFoundError: org/aspectj/lang/annotation/Aspect

leave a comment »

Hits the error java.lang.NoClassDefFoundError: org/aspectj/lang/annotation/Aspect when I try to start a webapp in tomcat.

I was trying to add AOP to an existing web application. The AOP configuration I added is:

<aop:aspectj-autoproxy />

Add the AspectJ library to the classpath. The required AspectJ library are:
1. aspectjrt
2. aspectjweaver

My webapps is using Maven, so I added the dependencies to pom.xml as below:

<!-- AspectJ -->

Written by Alfred

September 14, 2011 at 10:41

Linux | Find string in files

leave a comment »

I am trying to search for a string from the XML files (yup, more than an XML file)

I use grep with option “-an” which will print out the file name and line number of the found string.
The command below will search for text “my text” from all XML files in my current working directory.

grep -an "my text" *.xml

15 Practical GREP, resource

Written by Alfred

August 17, 2011 at 14:58

Posted in linux

Tagged with , , ,

SSL ya Tomcat

leave a comment »

Steps to enable SSL for your tomcat

1. Generate SSL key for your computer.

$JAVA_HOME/bin/keytool -genkey -alias tomcat -keyalg RSA -keystore /path/to/my/keystore

Reminder: check ../tomcat/conf/tomcat-user.xml for tomcat user’s password

2. Edit server.xml
– Open ../tomcat/conf/server.xml
– Uncomment following lines and add in “keystoreFile” and “keystorePass”

 <Connector port="8443" protocol="HTTP/1.1" SSLEnabled="true"
               maxThreads="150" scheme="https" secure="true"
               keystoreFile="/path/to/my/keystore" keystorePass="password"
               clientAuth="false" sslProtocol="TLS" />

3. You are DONE. You can try to access HTTPS with Port 8443 now.

Reference: Tomcat 6.0 SSL Configuration, here.

My environment:
Apache Tomcat v6.0.32
Sun Java v1.6.0_21
Red Hat 4.1.2-50

Written by Alfred

July 25, 2011 at 14:20

Posted in tomcat

Tagged with ,

Java | Loop a Map

leave a comment »

I think this is the best way I found to loop through a Map in Java.

public void printMap(Map mp) {
    Iterator it = mp.entrySet().iterator();
    while (it.hasNext()) {
        Map.Entry pairs = (Map.Entry);
        System.out.println("Key:" + pairs.getKey() + ", Value:" + pairs.getValue());

Reference Source

Written by Alfred

June 29, 2011 at 11:27

Posted in Java

Tagged with , ,

Plain text shows Encoded Chars

leave a comment »

Just started to have a chance to try out Spring 3.0, a lot of new features introduced, yet not enough time to read up all features.

I started a simple web apps project to try out Spring 3.0. The web apps is pretty simple, a REST server alike. Client init HTTP calls to the web apps with specific Servlet name and parameter, the web apps will returns the result JSON with Plain text mode. Since I am using Spring MVC, the result is printed with JSTL.

Sample call from client:

Expected result from server:

I get what I expected when I test my web apps with browser.

But the ‘interesting’ comes when I test it with little Java Post program (use HttpClient, init HTTP call and inspect the result).
I did not get the expected result, I get below:

All double-quat / quotation being replaced with ", its clearly the contain is being HTML encoded.

Below are my trouble-shooting steps:
Step 1. Check tomcat configuration
– Ensure the tomcat UTF-8 by default. More info
Step 2. Check web apps JSPs
– Ensure all JSP are UTF-8 as default encoding.

But both Step 1 & 2 does not fix my problem yet.
Step 3. Google “jstl without html encoded”
– Finally, I get the answer 🙂
– add in escapeXML properties when printing the result in JSTL
Before: <c:out value="${result}" />
After: <c:out value="${result}" escapeXml=”false”/>
Tutorial – Output with / without Encode

Lesson Learn:
When you want to print a PLAIN TEXT with JSTL, do remember to include escapeXML="false" to ensure that end user will get the actual expected result without HTML/XML encoded.

Written by Alfred

June 28, 2011 at 15:54

Posted in HTML, Java, Spring

Tagged with ,

Spring | NoSuchMethodError: GenericTypeResolver

with 4 comments

ERROR: java.lang.NoSuchMethodError: org.springframework.core.GenericTypeResolver.resolveTypeArguments(Ljava/lang/Class;Ljava/lang/Class;)[Ljava/lang/Class;

I am starting a new webapps recently, since its a new webapps, I decided to use the latest Spring version 3.1.0.M2. Everything looks fine when the development is still in standalone environment. After dive into the development, the webapps I’m working on need to ‘talk’ to some other existing components in the system. After I have integrated my webapps with the existing components and I restart my tomcat, I get the error java.lang.NoSuchMethodError: org.springframework.core.GenericTypeResolver.resolveTypeArguments(Ljava/lang/Class;Ljava/lang/Class;)[Ljava/lang/Class;

First of all, for Java developer, when you see ‘NoSuchMethodError’, you will directly check the existence of the mentioned method in your JAR file. But ‘sadly’ the mentioned class file and method is there.

Thanks to Google, I got my answer in Spring Forum. The answer is pretty simple, I have more than 1 version of Spring in the classpath. As mentioned earlier, the new webapps is running on latest Spring (v3.1.0.M2) but the existing components I integrates with only running on Spring v2.5. So, pls ensure that there is only ONE SPRING VERSION in your classpath.

Written by Alfred

June 20, 2011 at 18:02