Skip to content

Quickstart 1.2.3

Lahiru Sahan Jayasinghe edited this page Jan 2, 2014 · 1 revision

ZombieLink works by allowing you to define an endpoint contract which matches that of the service to be accessed; this can be done quickly, with ease. The example below uses Google's currency converter to demonstrate endpoint creation.

####1. Creating an endpoint contract

  • Use the @Endpoint annotation to identify your contract as an endpoint by supplying the host and any other bare essential information (scheme, port, root-path) required to reach it.
@Endpoint("www.google.com")
public interface CurrencyConverterEndpoint {

}

####2. Mirroring a service method

  • Create a method signature which matches a public service on the endpoint and annotate it with @Request. Provide the sub-path on which this service is located.

Tip: If the path is common to all services, consider using the root path property on @Endpoint instead.

  • Identify any request parameters with the @Param annotation and provide the parameter name.
public class Conversion implements Serializable {

        private String lhs;
        private String rhs;
        private String error;
        private boolean icc;
        
        /* serialVersionUID, Accessors, Mutators, 
         * hashCode(), equals() and toString() omitted */
}
@Endpoint("www.google.com")
public interface CurrencyConverterEndpoint {

	@Request(path = "/ig/calculator" )
	Conversion convert(@Param("q") String query);
}

####3. Using response parsers

  • Service calls may return results of various types. Response Parsers can be used to define the type and the subsequent conversion necessary for making the result available on return.

  • Parsers can be defined at the endpoint level or at the request level. If defined on an endpoint, that parser is to be used on all requests associated with the endpoint. This can be overridden by defining a new parser at request level to hint that specified service call should use a different parser instead of the one assigned to the endpoint.
@Endpoint("www.google.com")
@Parser(PARSER_TYPE.JSON)
public interface CurrencyConverterEndpoint {

        @Request(path = "/ig/calculator")
        Conversion convert(@Param("q") String query);
}

####4. Injecting the endpoint

  • The services can by invoked by injecting a proxy instance of the endpoint contract into your code at runtime.

  • Injection is as simple as annotating the endpoint reference with @Bite and using the Zombie to infect the instance.

public class CurrencyService implements CurrencyContract {

  	@Bite
  	private CurrencyConverterEndpoint ccEndpoint;
        {
	    Zombie.infect(this);
	}

        @Override
	public float usdToAud() {
     
     	    Conversion rate = ccEndpoint.convert("1USD=?AUD");
     	    ...
	}
}

Clone this wiki locally