Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Jersey
1. JAX-RS & JERSEY
Yung-Lin Ho <yho@bluetangstudio.com>
founder of blue tang studio
2. ABOUT ME
• Yung-Lin Ho. AKA HYL.
• founder of blue tang studio
• Working on Location Based Platform
• Cassandra / Zookeeper
• Lucene
• Tapestry / Jersey
• Scala !
3. WEBSERVICE IN YEAR 2000
• Servlet API
• public void doGet(HttpServletRequest request, HttpServletResponse response) {
String name = request.getParameter(“name”);
NameParameterValidator.validate(name);
Object resObj = requestProcessor.process(name);
Serializer ser = ObjectSerializerFactory.getSerializer(request.getParameter(“format”))
ser.write(resObj, response);
}
• Struts
• Action, Validator, Processor...
4. YEAR 2005
• SOAP, Apache Axis, Apache CXF
• XML configuration HELL.
• no compile time check.
• keep code and configuration in-sync.
5. OPEN SOURCE WORLD
• Restful Requests
urlpatterns = patterns('',
(r'^articles/2003/$', 'news.views.special_case_2003'),
(r'^articles/(?P<year>d{4})/$', 'news.views.year_archive'),
(r'^articles/(?P<year>d{4})/(?P<month>d{2})/$', 'news.views.month_archive'),
(r'^articles/(?P<year>d{4})/(?P<month>d{2})/(?P<day>d+)/$',
'news.views.article_detail'),
)
• Rest won the battle. SOAP started to fad.
• why?
6. ANNOTATION COME TO RESCUE
@Path("/rest/1.0/model/{modelId}")
@Produces({"application/json"})
public interface DataService {
@GET
@Path("/doc/{docId}")
public Document get(@PathParam(“modelId”) UserModelId modelId,
@PathParam("docId") DocumentId documentId);
@PUT
@Path("/doc/{docId}")
@Consumes({"application/json"})
public void saveOrUpdate(@PathParam(“modelId”) UserModelId modelId
@PathParam("docId") DocumentId documentId,
Document document);
@DELETE
@Path("/doc/{docId}")
public void delete(@PathParam(“modelId”) UserModelId modelId
@PathParam("docId") DocumentId documentId);
}
8. OBJECT SERIALIZATION
• @Produce & @Consume
• POJO, JAXB, Jackson-Json.
@XmlRootElement
{"name":"Agamemnon", "age":"32"}
public class MyJaxbBean {
public String name;
public int age;
public MyJaxbBean() {} // JAXB needs this
public MyJaxbBean(String name, int age) {
this.name = name;
this.age = age;
}
}
9. FINALLY. JERSEY.
• what is jersey - a servlet javax.ws.rs.core.Application
• ways to contributes RootResource(s) to Application.
<servlet>
<servlet-name>Jersey Web Application</servlet-name>
<servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>com.sun.jersey.config.property.packages</param-name>
<param-value>org.foo.rest;org.bar.rest</param-value>
</init-param>
</servlet>
public class MyApplicaton extends Application {
public Set<Class<?>> getClasses() {
Set<Class<?>> s = new HashSet<Class<?>>();
s.add(HelloWorldResource.class);
return s;
}
}
10. ROOTRESOURCE LIFECYCLE
• New class instance for each request if you put class into
Application
• Same instance for all requests if you put class instance into
Application.