Remove duplicate entries in a Vector in java

One of the solution which normally every java developer faced once.We have to remove the duplicate
value from a vector in java.

There are two solution for it one.

1.

Vector newVect = new Vector(new LinkedHashSet(originalVect));


or this, if you do not need a new Vector object created

2

Collection noDup = new LinkedHashSet(vectorContainingDup);
vectorContainingDup.clear();
vectorContainingDup.addAll(noDup);

In both cases we puting duplicate vector in linkedHashSet , which will remove the duplicate values.

Enjoy coding 🙂 with techartifact………………………….

RESTFul Service example using Apache CXF and Spring

In recent times there is lot of growth in RESTFul services. I thought it would be nice to talk about it.

This example is using Apache CXF and Spring. There are some other frameworks e.g Jersey (Reference Sun implementation), RestEasy, the JBoss choice and Apache CXF.

Here is bacic web configuration adding Spring context and CXF tranport servlet.

  <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/services.xml</param-value>
    </context-param>


    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <servlet>
        <servlet-name>CXFServlet</servlet-name>
        <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>CXFServlet</servlet-name>
        <url-pattern>/rest/*</url-pattern>
    </servlet-mapping>

There is Cities service using Spring annotations, this has been configured using Spring annotation
Second annottatiions is of CXF to define the mount point REST service

@Service("timeService")
@Path("cities")
public class CitiesListingService {

    @GET
    @Produces({"application/json", "application/xml"})
    public RestFulCities getCities() {
        List<City> cities = new LinkedList<City>();
        cities.add(new City("New Delhi", "011", "19M"));
        cities.add(new City("Mumbai", "022", "21M"));
        cities.add(new City("Chennai", "044", "10M"));
        RestFulCities restFulCities = new RestFulCities();
        restFulCities.setRestFulCityList(getCities(cities));
        return restFulCities;
    }

Here is Spring configuration for connection cxf and spring beans together.

<context:component-scan base-package="com.techartifact.example.spring"/>

    <import resource="classpath:META-INF/cxf/cxf.xml"/>
    <import resource="classpath:META-INF/cxf/cxf-extension-jaxrs-binding.xml"/>
    <import resource="classpath:META-INF/cxf/cxf-servlet.xml"/>

    <jaxrs:server id="restContainer" address="/">
        <jaxrs:serviceBeans>
            <ref bean="timeService"/>
        </jaxrs:serviceBeans>
        <jaxrs:extensionMappings>
            <entry key="json" value="application/json"/>
            <entry key="xml" value="application/xml"/>
        </jaxrs:extensionMappings>
        <jaxrs:providers>
            <ref bean="jaxbXmlProvider"/>
        </jaxrs:providers>
    </jaxrs:server>

    <!-- Webservice message handlers -->
    <bean id="jaxbXmlProvider" class="org.apache.cxf.jaxrs.provider.JAXBElementProvider">
        <property name="jaxbElementClassNames" ref="elements"/>
    </bean>

    <util:list id="elements">
        <value>com.techartifact.example.spring.model.RestFulCity</value>
        <value>com.techartifact.example.spring.model.RestFulCities</value>
    </util:list>

Yes that’s it. Now build the code and run it.

Run example

Download full example code from here spring-RESTapplication

Go to project directory [spring-RESTapplication] in command shell and run following command using maven

mvn clean package
mvn -Pcargo-run

http://localhost:8080/springrest/rest/cities

CXF can also produces output in json format, use following url to see out put in json

http://localhost:8080/springrest/rest/cities.json

What is IoC or Spring IOC?

There is a lot of confusion about the definition of the IoC container – some equate it with a design pattern called Dependency Injection – but in reality IoC is much larger than dependency injection.

Inversion of Control (IoC) is an object-oriented programming practice where the object coupling is bound at run time by an assembler object and is typically not known at compile time using static analysis.

The basic concept of the Inversion of Control pattern (also known as dependency injection) is that you do not create your objects but describe how they should be created. You don’t directly connect your components and services together in code but describe which services are needed by which components in a configuration file. A container (in the case of the Spring framework, the IOC container) is then responsible for hooking it all up.

Steps to use the inversion of control (IOC) with spring framework.

=> Create one .xml file to configure the components and services.

=> We need to define classes and its properties.

=> Mention the service with its component as per springs framework
guidelines so that Spring container can create a object for us.

Explanation:

Class

package com.techartifact.vinay;

public class RefClass {

                private RefClass newRefClass;

                public RefClass getRefClass() {

                                return newRefClass;

                }

                public void setNewRefClass(RefClass newRefClass) {

                                this.newRefClass = newRefClass;

                }

}

Configuration file


 <bean id="createRefClass" class="com.techartifact.vinay.RefClass">

        <property name="newRefClass"/>

 </bean>


Here RefClass is a class which has getter and setter for newRefClass property.

Now spring container takes the responsibility to create the object of RefClass class with object name newRefClass which defined in the property tag with name attribute and set the value automatically when it is used.