Posts

Showing posts from 2012

Spring 3 part 3: Standard wiring

This is the 3rd part of the Spring 3 Series . In the part 2 of this series explained the Spring way of wiring. The part 2 examples are continued in this blog.mThis part is dedicated to standard wiring that is proposed by the Java Community Process. Spring 3 implements the JSR-330 that is @Inject annotation. The @Inject is lot common to the @Autowire but no “required” attribute. import javax.inject.Inject; import javax.inject.Named; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Parent { @Inject @Named("grandChild") private Child child; The Parent class’s child property is annotated with @Inject and @Named. Here @Named is similar to @Qualifier, for instance grandChild is declared as a Spring bean in the spring-config.xml file which is referenced in the @Named annotation. It is also possible to create custom qualifier as an alternative way. Here the custom qual

Spring 3 part 2: Auto wiring

Image
This is the 2nd part of the Spring 3 Series . In the first part of this series, I discussed how to create maven Spring 3 project.The objective of the second part is to discuss the Spring 3 auto-wiring. Fist look into the Spring 2.5 auto-wiring which is based on the byName, byType, constructor and autodetect. <?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.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd"> <!-- context:annotation-config/--> <bean id="parent" class="au.com.blogspot.ojitha.spring3tut1.Pa

Spring 3 part 1: Getting started with maven 3

Image
This is the 1st part of the Spring 3 Series . I know most of you know how to create spring project from maven. This blog I write as a part of the completion of series of my blogs on Spring 3. The my way of creating spring project is, first creating a simple java project and enhanced it up to the Spring project as I want. As shown above figure, the maven command will create first-tut project underneath the maven-projects directory. Now you are ready to import existing maven project to STS. I you are typical Eclipse user, then you can use mvn eclipse:eclipse before import. And you need to create global variable M2_REPO in the Eclipse. In STS there is m2Eclipse integration, therefore you can directly import as shown in the following figure Via STS, you can add spring dependencies as shown in the following You can find the following dependencies just typing “org.spring”. This will update the pom.xml file as follows. < project xmlns = " http://maven.apache.org/POM/4.0.

RESTFul Servlets

Image
I already wrote the blog on RESTFull Dynamic service proxy ( have a ). In this blog, address the use of Servlets, because Servlets are best candidates for RESTFul services. The main advantage is HTTPServlet which cover the HTTP communication completely. RESTFul (HTTP method) HTTPServlet GET doGet() POST doPost() PUT doPut() DELETE doDelete() In this blog, I just use doPost() to store Person object in the server side Map (employees) and get the complete list of people (employees) as a response. All of the implementation from the RESTFull Dynamic service proxy blog, except the PersonService, which is replaced by the PersonServlet.java as shown in the following listing. package com.blogspot.ojitha.wsex7.web; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import javax.servlet.ServletException; import javax.servlet

RESTFull Dynamic service proxy

Image
A RESTFull provider implements the method public Source invoke(Source request) in the server side and the twin Dispatch object in the client side.  This way of handling the RESTFull is not easy. The best way is to use of JAX-RS as shown in my previous blog entries JAX-RS: 1 min RESTFul with JAXB   and  JAX-RS Tutorial: 1 min RESTFul service .   As shown in the above figure, The person is JavaBean with JAXB xml root as shown in the following listing. As well it compose an Address object for each and every Person. package com.blogspot.ojitha.wsex6.person; import javax.xml.bind.annotation.XmlRootElement; import javax.xml.bind.annotation.XmlType; import com.blogspot.ojitha.wsex6.common.Address; @XmlRootElement @XmlType(propOrder={"id", "firstName", "lastName", "age","address"}, namespace="com.blogspot.ojitha.wsex6.person") public class Person { private String firstName; private String lastName; private in

JAX-RS: 1 min RESTFul with JAXB

Image
This is second post of the JAX-RS Tutorial: 1 min RESTFul service post extended to use JAXB. Follow the first tutorial to understand the Grizzly framework usage. I’ve created two classes Person and the Address as shown in the above class diagram. package com.blogspot.ojitha.rest.example.first; import javax.xml.bind.annotation.XmlRootElement; @XmlRootElement public class Person { private String firstName; private String lastName; private Long id; private Address address; public String getFirstName() { return firstName; } public void setFirstName(String firstName) { this.firstName = firstName; } public String getLastName() { return lastName; } public void setLastName(String lastName) { this.lastName = lastName; } public Long getId() { return id; } public void setId(Long id) { this.id = id; } @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + ((id == null) ? 0 : id.hashCode()); return result;

JAX-RS Tutorial: 1 min RESTFul service

This is not intended to explain the REST, but one of the best ways to create simplest web service in JAX-RS 311. However, you need to install maven as a prerequisite. Eclipse is the IDE for me. mvn archetype:generate -DarchetypeCatalog=http://download.java.net/maven/2 This will prompt you for option to create project with different configurations. Select option 1, that is the simplest. If you are suppose to go with Eclipse, then don’t forget to execute the following command, that is mvn eclipse:eclipse Then you can import this project as an existing project in the Eclipse. In the service, important to provide the path in @path annotation. HelloWorldResource is the simplest web service. package com.blogspot.ojitha.rest.example.first; import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.Produces; @Path("/hello") public class HelloWorldResource { @GET @Produces("plain/text") private String hello(){ return "Hello World&qu

JAX-WS Binary Data passing: using MTOM

Image
The preferred way of binary data passing in the JAX-WS is MTOM. In the previous blog ( JAX-WS Binary Data Passing: using base64binary ), binary data has been passed in the body of the SOAP. But in the MTOM, binary data is passed as a attachment. See the web service (which is modified from the previous blog). package org.ojitha.wsex4; import java.awt.Image; import java.io.File; import java.io.IOException; import javax.imageio.ImageIO; import javax.jws.WebMethod; import javax.jws.WebService; import javax.xml.ws.Endpoint; import javax.xml.ws.soap.MTOM; import javax.xml.ws.soap.SOAPBinding; @WebService(serviceName="ImageService", wsdlLocation="org/ojitha/wsex4/ImageService.wsdl") @MTOM public class ImageService { @WebMethod public Image getImage(String imgName) throws IOException{ System.out.println("Image name "+imgName); return ImageIO.read(new File(imgName)); } public static void main(String[] args){ Endpoint endpoint = Endpoint.create(n

JAX-WS Binary Data Passing: using base64binary

Instead of passing binary data as an attachment to web services using either SOAP with Attachment or MTOM, in this blog, I use base64binary to pass in the SOAP body. For the MTOM (preferred way of sending binary data) see the next blog JAX-WS Binary Data passing: using MTOM . Web service as follows: package org.ojitha.wsex4; import java.awt.Image; import java.io.File; import java.io.IOException; import javax.imageio.ImageIO; import javax.jws.WebMethod; import javax.jws.WebService; import javax.xml.ws.Endpoint; @WebService(serviceName="ImageService") public class ImageService { @WebMethod public Image getImage(String imgName) throws IOException{ return ImageIO.read(new File(imgName)); } public static void main(String[] args){ Endpoint.publish("http://127.0.0.1:9876/image", new ImageService()); } } The client program is, package org.ojitha.wsex4.client; import java.awt.image.BufferedImage; import java.io.ByteArrayInputStream; import java.i

Learn XML Schema by Example: 2 Polymorphism

Image
In the previous blog, Learn XML Schema by Example: 1. Complex type , I introduced the very simple idea of ComplexType in Schema. In this blog, I am going to introduce idea of polymorphism which is one of the very important concept in Object orientation. As shown in the above figure, I need Address to be replaced by AUAddress or USAddress in the instance xml file. All the Address, USAddress and AUAddress are complex types in the XML schema. These types are defined in the address.xsd file as shown in the following code. <?xml version="1.0" encoding="utf-8"?> <xs:schema xmlns="http://www.ojitha.org/address" targetNamespace="http://www.ojitha.org/address" xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified"> <xs:complexType name="AUAddress"> <xs:complexContent> <xs:extension base="Address"&

Learn XML Schema by Example: 1. Complex type

Image
XML Schema development is one of the major requirements for the Web services. It is time to get rid of the RPC style and move to DOCUMENT style. First of this series I would like to start with complex type. <?xml version="1.0" encoding="utf-8"?> <xs:schema xmlns="http://www.ojitha.org" targetNamespace="http://www.ojitha.org" xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified"> <xs:element name="process"> <xs:complexType> <xs:sequence> <xs:element name="person" type="personType"/> </xs:sequence> </xs:complexType> </xs:element> <xs:complexType name="personType"> <xs:sequence> <xs:element name="name" type="xs:string"/> <xs:element name=&qu

SAAJ Unit testing client for JAX-WS web services

Image
JAX-WS is the simplest java web services. In this blog, I would like to use first example explained in the book Java Web Services: Up and Running , by Martin Kalin ( thanks Martin, great book). Here the SEI and SBI created from the above book. SEI package com.ojitha.ws.ex1; import javax.jws.WebMethod; import javax.jws.WebService; import javax.jws.soap.SOAPBinding; import javax.jws.soap.SOAPBinding.Style; @WebService @SOAPBinding(style=Style.RPC) public interface TimeServer { @WebMethod String getTimeAsString(); @WebMethod long getTimeAsElapsed(); } SBI package com.ojitha.ws.ex1; import java.util.Date; import javax.jws.WebService; @WebService(endpointInterface="com.ojitha.ws.ex1.TimeServer") public class TimeSeverImpl implements TimeServer { @Override public String getTimeAsString() { return new Date().toString(); } @Override public long getTimeAsElapsed() { return new Date().getTime(); } } The main objective of this blog is to introduce a SAA

Tip: Java Instance initialization is critical

One of my previous blogs, I showed how the instances are initiated in the Java, if you wish see the  Java Constructor Loading Tips . In this blog, I will give an example, where developer can confused. For example, the following code compiles but throws the NPE in the runtime. package com.ojitha.green; /** * @author Ojitha * */ public class Child { //singleton public static final Child GIRL = new Child(); private Child() {} private static final Boolean GENDER = true; private Boolean gender = GENDER; public Boolean isMale(){ return gender;} public static void main(String... args){ String gender = Child.GIRL.isMale() ? "Female":"Male"; System.out.println(gender); } } In the line 17, you get the exception. At the time of creating GIRL, initializing the object variable is happen after the constructor is called. At the time of the GIRL singleton instance is created, Boolean type gender variable is already initialized to null, that cause to the NPE.