restful in java

34

Upload: saeed-satari

Post on 22-Jul-2015

74 views

Category:

Education


3 download

TRANSCRIPT

Page 1: Restful in Java
Page 2: Restful in Java

RESTFUL

غالمعلی نژاد حاجعلی ایرانیمهندس: راهنمااستاد

سعید ستاری: دهندهارائه

دانشگاه بناب

Page 3: Restful in Java

28 / REST3

مقدمه

RESTمعرفی

RESTقوانین

RESTمشخصات

بررسی یک سیستم واقعی

JAX-RSمعرفی

JAX-RSمفاهیم

فهرست

Page 4: Restful in Java

28 / REST

RESTمعرفی

4

Make Ant Maven

•Representational State Transfer

2000در سال Roy Fieldingمعرفی شده توسط •

Word Wide Webیک مدل معماری برای سیستم های توزیع شده مانند •

Page 5: Restful in Java

28 / REST

RESTقوانین

5

می باشندمنابعهمه چیز به صورت Customer, Locations, Item, List of users: مثال•

هویت می باشندمعین کننده منابع دارای URIs for web: مثال•

برای دسترسی به منابعواسط یکنواخت HTTP methods (GET, POST, PUT, DELETE, HEAD):مثال•

می باشندنماینده منابع دارایXML, JSON, Binary: مثال•

می شوند Linkمنابع با یکدیگر Hyperlinks:مثال•

1

2

3

4

5

Page 6: Restful in Java

28 / REST

RESTمشخصات

6

StatelessCacheable

الیه بندی

نیازی به سادهمکانیزم پی بری

اضافی ندارد

Page 7: Restful in Java

28 / REST

بررسی یک سیستم واقعی

7

(Bug System Tracking) سیستمپیگیری خطا

خطاهاsummary, priority, user who reported it

کاربران

User name

Page 8: Restful in Java

28 / REST

بررسی یک سیتم واقعی

8

UML Notation:“whole-part” relationship (composition)“part” is exclusively owned by “whole”

UML Notation:One-way association

UML Notation:Generalization / inheritance

Page 9: Restful in Java

28 / REST

منابع و مشخص کننده ها-بررسی یک سیستم واقعی

9

منابع•

•User,Bug,Note•All users, All bugs, All notes for a particular bug

1 باشدمی منبعهمه چیز یک

2 هستند مشخص کننده ها منابع دارای

Identifiers Note

http://<host>/users All users

http://<host>/users/{userId} A particular user

http://<host>/bugs All bugs

http://<host>/bugs/{bugId} A particular bug

http://<host>/bugs/{bugId}/notes All notes for a particular bug

http://<host>/bugs/{bugId}/notes/{noteId}

A particular note for a particular bug

مشخص کننده ها•

Page 10: Restful in Java

28 / REST

واسط های یکنواخت

10

HTTPمتدهای •

HTTP verb Meaning Safe? Idempotent? Cacheable?

POST Create*

GET Retrieve

PUT Update*

DELETE Delete

)*(POST وPUT دارندمعانی دیگری هم

POSTمی تواند برای به روز رسانی جزی و یا اضافه کردن چیزی به منابع استفاده شود(PASTE AFTER )

PUT می تواند به عنوانcreate استفاده شود اگر تمام محتوی منابع تعیین شده کهURI شوند آن را می شناسیم فرستاده(PASTE OVER)

Page 11: Restful in Java

28 / REST

واسط یکنواخت-بررسی یک سیستم واقعی

11

عملیات توضیحات

GET http://<host>/users هاuserتمام لیست

POST http://<host>/users جدیدuserساخت

GET http://<host>/users/345 بخصوصuserیک بازیابی

PUT http://<host>/users/345 بخصوصuserیک اصالح

DELETE http://<host>/users/345 بخصوصuserیک حذف

عملیات توضیحات

GET http://<host>/bugs/234/notes 234تمام متن های خطای لیست

POST http://<host>/bugs/234/notes 234یک متن جدید برای خطای ساخت

[GET | PUT | DELETE] .../bugs/234/notes/34 یک متن خطای ]حذف[اصالح ]بازیابی[

234

3 برای دسترسی به منابع واسط یکنواخت

Page 12: Restful in Java

28 / REST

نمایش ها و منابع لینک شده-بررسی یک سیستم واقعی

12

:XMLنمایش در •GET http://localhost:9000/bugs/1/notes/1Content-type: application/xml<Note href=“http://localhost:9000/bugs/1/notes/1 ">

<description>It is really broken</description>

<owner>http://localhost:9000/users/1</owner></Note>

POST http://localhost:9000/bugs Content-type: application/json{

"Bug" : {"priority" : "P1","reporter" : "http://localhost:9000/users/1","summary" : "Something is wrong"

}}

4 می باشندنمایندهمنابع دارای

5 می شوندLinkیکدیگر منابع با

:JSONنمایش در •

Page 13: Restful in Java

28 / REST

JAX-RSمعرفی

13

JAX-RS :API جاوا برایRESTFulوب سرویس

Page 14: Restful in Java

28 / REST

JAX-RSمفاهیم کلیدی

14

کالس های منابع•یا متدهای Path@کالس های جاوایی که حداقل دارای یک متد با حاشیه نویسی •

@GET , @PUT, @POST, @DELETE چرخه حیات•با توجه به قرارداد یک منبع جدید کالس برای هر •

درخواست ساخته می شود•JSR دیگر پیاده سازی های چرخه حیات را محدود نمی

کند متدهای منابع•از یک کالس منبع توسط متد درخواست نقش دهنده حاشیه نویسی Publicیک متد •

(GET, @PUT, @POST, @DELETE, @OPTIONS, @HEAD@)می شود

کالس های مهیا •interfaceحاشیه نویسی می شوند و با یک یا چند Provider@این کالس ها توسط •کنندهJAX-RSپیاده سازی می شوند

•MessageBodyReader/MessageBodyWriter•ExceptionMapper•ContextResolver

Page 15: Restful in Java

28 / REST

حاشیه نویسی های پرکاربرد/ JAX-RSمفاهیم

15

نویسیحاشیه هدف شرح

@Path Class or Method به مسیر یک منبعمنسوب

@Consumes@Produces

Class or Method لید کنداز نوع های واسط ها که توانایی تحلیل و تولیستی

@GET@POST@PUT@DELETE@OPTIONS@HEAD

Method تدبکار برده شده توسط حاشیه نویسی م HTTPافعال

@PathParam Parameter (also field, POJO method)

استخراج شودURIکه می تواند توسط ارزشی

Page 16: Restful in Java

28 / REST

JAX-RSمثالی از

16

@Path("/users")

public class UserHandler {

@GET

@Path("{id}")

@Produces("application/xml”)

public JaxbUser getUser(@PathParam("id") long id) {

...

}

URI پایه که متصل میشود به منبع

Page 17: Restful in Java

28 / REST

JAX-RSمثالی از

16

@Path("/users")

public class UserHandler {

@GET

@Path("{id}")

@Produces("application/xml”)

public JaxbUser getUser(@PathParam("id") long id) {

...

}

برای متد HTTPمتد getUser()

Page 18: Restful in Java

28 / REST

JAX-RSمثالی از

16

@Path("/users")

public class UserHandler {

@GET

@Path("{id}")

@Produces("application/xml”)

public JaxbUser getUser(@PathParam("id") long id) {

...

}

/ قطعه URIمسیر پارامتر

Page 19: Restful in Java

28 / REST

JAX-RSمثالی از

16

@Path("/users")

public class UserHandler {

@GET

@Path("{id}")

@Produces("application/xml”)

public JaxbUser getUser(@PathParam("id") long id) {

...

}GET …/users/{id}

Page 20: Restful in Java

28 / REST

JAX-RSمثالی از

16

@Path("/users")

public class UserHandler {

@GET

@Path("{id}")

@Produces("application/xml”)

public JaxbUser getUser(@PathParam("id") long id) {

...

}

مقدار و نوع را بر می گرداند

Page 21: Restful in Java

28 / REST

JAX-RSمثالی از

16

@Path("/users")

public class UserHandler {

@GET

@Path("{id}")

@Produces("application/xml”)

public JaxbUser getUser(@PathParam("id") long id) {

...

}

را در URIارزش قسمت انتقال می دهدidپارامتر

Page 22: Restful in Java

28 / REST

JAX-RSمثالی از

16

@Path("/users")

public class UserHandler {

@GET

@Path("{id}")

@Produces("application/xml”)

public JaxbUser getUser(@PathParam("id") long id) {

...

}

GET http://localhost:9000/users/2

Content-Type: application/xml

<?xml version="1.0" encoding="UTF-8"?>

<User> … </User>

Page 23: Restful in Java

28 / REST23

Page 24: Restful in Java

28 / REST

مباحث پیشرفته

18

Page 25: Restful in Java

28 / REST

Path and regular expression mapping@/ مباحث پیشرفته

19

• @Path(path-expression)"{" variable-name [ ":" regular-expression ] "}“

• Examples• @Path("example2/{var:.+}") matches following:

• …/example2/a

• …/example2/a/b/c

• @Path("example3/{var:\\d+}") matches following:

• …/example3/345

• …/example3/23

• @Path(“example4/{name}-{id}”) matches following:

• …/example4/a-1

• …/example4/a----1

Page 26: Restful in Java

28 / REST

Param[Query|Header|Matrix|Cookie|Form]@ /پیشرفتهمباحث

20

• @QueryParam• For example …/query?sorted=true

@GET

@Path("query")

public String queryParamExample(@QueryParam(“sorted") String var) {

return var;

}

• @MartixParam• For example …/users;sorted=true

• @HeaderParam• For example “Date” header

• @CookieParam / @FormParam

Page 27: Restful in Java

28 / REST

Exception Mappers/پیشرفته مباحث

21

package javax.ws.rs.ext

public interface ExceptionMapper<E extends

Throwable> {

Response toResponse(E exception);

}

Page 28: Restful in Java

28 / REST

MessageBodyReader/ مباحث پیشرفته

22

package javax.ws.rs.ext

public interface MessageBodyReader<T> {

boolean isReadable(Class<?> type, Type genericType,

Annotation annotations[], MediaType mediaType);

T readFrom(Class<T> type, Type genericType,

Annotation annotations[], MediaType mediaType,

MultivaluedMap<String, String> httpHeaders,

InputStream entityStream) throws IOException,

WebApplicationException;

}

Page 29: Restful in Java

28 / REST

MessageBodyWriter/ مباحث پیشرفته

23

package javax.ws.rs.ext

public interface MessageBodyWriter<T> {

boolean isWriteable(Class<?> type, Type genericType,

Annotation annotations[], MediaType mediaType);

long getSize(T t, Class<?> type, Type genericType, Annotation annotations[],

MediaType mediaType);

void writeTo(T t, Class<?> type, Type genericType, Annotation annotations[],

MediaType mediaType,

MultivaluedMap<String, Object> httpHeaders,

OutputStream entityStream) throws IOException, WebApplicationException;

}

Page 30: Restful in Java

28 / REST

مباحث پیشرفته

ویژگی های دیگرJAX-RS

24

@Encoded

@ApplicationPath

Cookies

SecurityContext

CacheControl

Application

Complex negotiation / Varian

Integration with Spring

t processing Security

@OPTIONS, @HEAD, @HttpMethod

PathSegment

Page 31: Restful in Java

28 / REST

مباحث پیشرفته

25

ویژگی های بیشتر

WADL

Restful java clients

REST support in Spring 3.0

Page 32: Restful in Java

28 / REST

منابع

26

• Apache CXF (JAX-RS part):http://cxf.apache.org/docs/jax-rs.html

• RESTEasy users guides: http://docs.jboss.org/resteasy/docs/1.1.GA/userguide/pdf/RESTEasy_Reference_Guide.pdf

• WizTools REST Client:http://code.google.com/p/rest-client/

Page 33: Restful in Java

28 / REST33با تشکر از تمامی حضار

Page 34: Restful in Java

28 / REST28

؟؟ سوال؟؟