Tuesday, July 29, 2014

JAXB marshalling and unmarshalling of Hashtable

It is not possible to Marshall or Unmarshall a Hashtable directly however you can achieve it using a Holder class (wrapper class). With the help of Holder class you can get the default XML format as shown below -

Default XML format -
<mapHolder>
    <htmlColorCode>
        <entry>
            <key>blue</key>
            <value>0000FF</value>
        </entry>
        <entry>
            <key>green</key>
            <value>00FF00</value>
        </entry>
        <entry>
            <key>red</key>
            <value>FF0000</value>
        </entry>
    </htmlColorCode>
</mapHolder>

However if you need to customize the XML format like the one shown below you may need to use XmlAdapter.

<colorMap>
    <colors>
        <item code="0000FF" color="blue"/>
        <item code="00FF00" color="green"/>
        <item code="FF0000" color="red"/>
    </colors>
</colorMap>

Method 1 - Using Wrapper class

Output -
--Marshalling--
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<mapHolder>
    <htmlColorCode>
        <entry>
            <key>blue</key>
            <value>0000FF</value>
        </entry>
        <entry>
            <key>green</key>
            <value>00FF00</value>
        </entry>
        <entry>
            <key>red</key>
            <value>FF0000</value>
        </entry>
    </htmlColorCode>
</mapHolder>

--Unmarshalling--
{blue=0000FF, green=00FF00, red=FF0000}


Java - JAX-WS Jar files for SOAP web services

If you are writing a SOAP based webservice you may need to add below JAR files in your applications classpath

Error: java.lang.ClassNotFoundException: com.sun.xml.ws.transport.http.servlet.WSServletContextListener
Add: jaxws-rt.jar

Error: java.lang.NoClassDefFoundError: com/sun/istack/localization/Localizable
Add: jaxb-core.jar

Error: java.lang.NoClassDefFoundError: com/sun/xml/stream/buffer/XMLStreamBuffer
Add: streambuffer.jar

Error: java.lang.NoClassDefFoundError: com/sun/xml/bind/api/JAXBRIContext
Add: jaxb-impl.jar

Error: java.lang.NoClassDefFoundError: com/sun/xml/ws/policy/PolicyException
Add: policy.jar

Error: java.lang.NoClassDefFoundError: org/jvnet/staxex/XMLStreamReaderEx
Add: stax-ex.jar

Error: java.lang.NoClassDefFoundError: org/glassfish/gmbal/ManagedObjectManager
Add: gmbal-api-only.jar

Error: java.lang.NoClassDefFoundError: org/glassfish/external/amx/AMXGlassfish
Add: management-api.jar

Error: java.lang.NoClassDefFoundError: org/glassfish/ha/store/api/BackingStoreException
Add: ha-api.jar

You can download latest version of JAR file (E.g. jaxws-ri-2.2.8.zip ) from https://jax-ws.java.net/2.2.8/. For more information please see the link https://jax-ws.java.net/.

Java - JAXB marshalling and unmarshalling of ArrayList

Marshalling and unmarshalling of ArrayList using JAXB is rather simple however you cannot directly marshall ArrayList. You will need a holder class (wrapper class). It will hold the arraylist that you want to marshall or unmarshall.

Please see the self explanatory java code below -

Output -

--Marshalling--
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<List>
    <Value>one</Value>
    <Value>two</Value>
    <Value>three</Value>
</List>

--Unmarshalling--

[one, two, three]

Wednesday, July 23, 2014

Integration of DWR using DWRSpringServlet

DWR is a Easy Ajax for Java. To know more click here.

In this article we will see sample codes for integration of DWR with DWRSpringServlet

Step 1 - Create a web project in eclipse.

Step 2 - Add below Spring libraries

  • Spring Core libraries
  • Spring Web libraries
  • Spring AOP libraries
Step 3 - Add dwr.jar
Download appropriate version of dwr.jar and add it in your application classpath. For example DWR 3 requires Spring version 2.5 or greater.

Step 4 - Create dependent files

web.xml
The DwrSpringServlet can be used if you are not using Spring MVC. This servlet gains access to the Spring context configured in your web.xml. You can create your own context file, in our case it is dwrcontext.xml.


dwrcontext.xml


DWRService.java


index.jsp


Step 5 - Deploy the project in tomcat (or any other web server) and check the output


Integration of DWR using Spring MVC

DWR is a Easy Ajax for Java. To know more click here.

In this article we will see sample codes for integration of DWR with spring MVC

Step 1 - Create a web project in eclipse.

Step 2 - Add below Spring libraries

  • Spring Core libraries
  • Spring Web libraries
  • Spring AOP libraries

Step 3 - Add dwr.jar
Download appropriate version of dwr.jar and add it in your application classpath. For example DWR 3 requires Spring version 2.5 or greater.

Step 4 - Create dependent files

web.xml
Create spring DispatcherServlet and map URL pattern as shown below

javaxp-servlet.xml
If you are using Spring MVC you must declare dwr controller and url mapping i.e. SimpleUrlHandlerMapping as shown below

DWRService.java

index.jsp

Step 5 - Deploy the project in tomcat (or any other web server) and check the output