Thursday, March 26, 2015

Calling a Rest service from Springs

In Springs we have RestTemplate class. from this class we call the rest services.Below are the sample code for Get and Post requests.

@Autowired
RestTemplate restTemplate;


Calling a Get method:
Map<String, String> domainCategories = restTemplate.getForObject("http://test.com?reportId=100" Map.class);

Calling a Post Method:

TriageFirstCutResponse response = restTemplate.postForObject(
"rest URL", triageFirstCutRequest,TriageFirstCutResponse.class);

here triageFirstCutRequest is an object.which is the post parameter.

Tuesday, March 17, 2015

Marshalling and un-marshalling.

In few words, "marshalling" refers to the process of converting the data or the objects inbto a byte-stream, and "unmarshalling" is the reverse process of converting the byte-stream beack to their original data or object. The conversion is achieved through "serialization".

The purpose of the "marshalling/unmarshalling" process is to transfer data between the RMI system.


JAXB(Java Architecture for XML Binding) allows Java developers to map Java classes to XML representations. JAXB provides two main features: the ability to marshal Java objects into XML and the inverse, i.e. to unmarshal XML back into Java objects. JAXB mostly is used while implementing webservices or any other such client interface for an application where data needs to be transferred in XML format instead of HTML format which is default in case of visual client like web browsers.

Tuesday, March 10, 2015

Method level Injection with spring (Injecting prototype model in to singleton model)

When we want to inject prototype model object into singleton object we will be having an issue.

How we can achieve this in spring? we have a concept calling method injection. All beans in the spring's are singleton by default,
meaning spring instantiates them during context creation,caches them in context, when ever it needed it will load from cache,
if you make a particular object scope as prototype and you want to inject this object into other singleton object the actual problem arrives.

public class Singleton {

    private Prototype prototype;

    public Singleton(Prototype prototype) {

        this.prototype = prototype;
    }

    public void doSomething() {

        prototype.foo();
    }

    public void doSomethingElse() {

        prototype.bar();
    }
}

The below code displays the correct code of method injection:

public abstract class Singleton {

    protected abstract Prototype createPrototype();

    public void doSomething() {

        createPrototype().foo();
    }

    public void doSomethingElse() {

        createPrototype().bar();
    }
}

As you noticed, code doesn’t specify the createPrototype() implementation. This responsibility is delegated to Spring, hence the following needed configuration:

<bean id="prototype" class="ch.frankel.blog.Prototype" scope="prototype">

<bean id="singleton" class="sample.MySingleton">
  <lookup-method name="createPrototype" bean="prototype">
</lookup-method></bean></bean>