Your SlideShare is downloading. ×
0
Jersey
Jersey
Jersey
Jersey
Jersey
Jersey
Jersey
Jersey
Jersey
Jersey
Jersey
Jersey
Jersey
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

Jersey

1,716

Published on

Published in: Technology, Education
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
1,716
On Slideshare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
36
Comments
0
Likes
1
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 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 Requestsurlpatterns = 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);}
  • 7. URL AND PATTERN MATCHING• @Path("/rest/1.0/model/{modelId}")• @GET, @POST, @PUT, @DELETE, @HEAD• @PathParam, @QueryParam, @FormParam, @HeaderParam, @CookieParam and @MartixParam• @DefaultValue
  • 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.
  • 11. WADL• @Path("/form")@ProduceMime("text/html") public class Form { @Path("colours") public Colours getColours() { return coloursResource; } }<?xml version="1.0" encoding="UTF-8" standalone="yes"?><application xmlns="http://research.sun.com/wadl/2006/10"> <resources base="http://localhost:9998/resources/"> <resource path="/form"> <method name="GET"> <response> <representation mediaType="text/html" /> </response> </method> <method name="POST"> <request> <representation mediaType="application/x-www-form-urlencoded" /> </request> <response> <representation mediaType="text/html" /> </response> </method> <resource path="colours" /> </resource> </resources></application>
  • 12. CLIENT API• https://github.com/yunglin/common-jaxrs-client SearchServiceClient client = ClientFactory.getClient( SearchServiceClient.class, endPoint, myModel);•
  • 13. QA

×