http://mbranko.github.io/webkurs
Ovo je deo web kursa
http://www.ftn.uns.ac.rs
{
"name": "Žika",
"age": 20
}
[
{
"name": "Žika",
"age": 20
},
{
"name": "Laza",
"age": 21
}
]
http://www.ftn.uns.ac.rs/students
je lista studenatahttp://www.ftn.uns.ac.rs/students/<ime>
je konkretan student
GET /students/Žika HTTP/1.1
Host: www.ftn.uns.ac.rs
Date: Fri, 20 May 2011 12:00:00 GMT
Accept: application/json
HTTP/1.1 200 OK
Date: Fri, 20 May 2011
Server: Apache 2.2.0
Content-Length: 123
Connection: close
Content-Type: application/json
{
"name": "Žika",
"age": 20
}
POST /students HTTP/1.1
Host: www.ftn.uns.ac.rs
Date: Fri, 20 May 2011 12:00:00 GMT
Accept: text/xml
Content-Length: 123
Content-Type: application/json
{
"name": "Pera",
"age": 20
}
HTTP/1.1 201 Created
Date: Fri, 20 May 2011 12:00:00 GMT
Location: http://www.ftn.uns.ac.rs/students/Pera
Content-Length: nnn
Content-Type: application/json
{
"name": "Pera",
"age": 20
}
http://www.ftn.uns.ac.rs/students
http://www.ftn.uns.ac.rs/students/<ime>
Collection URI | Element URI | |
---|---|---|
GET | izlista URI-je i eventualno druge podatke o elementima kolekcije | dobija reprezentaciju elementa kolekcije u obliku odgovarajućeg MIME tipa |
POST | kreira novi element kolekcije; URL novog elementa se vraća u odgovoru | tretira dati element kao kolekciju i kreira novi element u njoj |
PUT | zameni celu kolekciju novom | Zameni element novim; ako ne postoji, kreira ga |
DELETE | uklanja celu kolekciju | uklanja dati element kolekcije |
URL twitter = new URL(
"http://twitter.com/statuses/public_timeline.xml");
URLConnection tc = twitter.openConnection();
BufferedReader in = new BufferedReader(
new InputStreamReader(tc.getInputStream(), "UTF8"));
<?xml version="1.0" encoding="UTF-8"?>
<statuses type="array">
<status>
<created_at>Fri May 20 18:49:46 +0000 2011</created_at>
<id>71648829237248000</id>
<text>So high school is done. LET THE PARTY BEGIN</text>
<truncated>false</truncated>
<favorited>false</favorited>
<in_reply_to_status_id></in_reply_to_status_id>
<in_reply_to_user_id></in_reply_to_user_id>
<in_reply_to_screen_name></in_reply_to_screen_name>
<retweet_count>0</retweet_count>
<retweeted>false</retweeted>
<user>
<id>250394499</id>
<name>Taylor Stricklin</name>
<screen_name>TaylorStricklin</screen_name>
...
</statuses>
http://twitter.com/statuses/public_timeline.xml
http://twitter.com/statuses/public_timeline.json
http://twitter.com/statuses/public_timeline.rss
http://twitter.com/statuses/public_timeline.atom
HttpClient client = new HttpClient();
GetMethod get = new GetMethod(
"http://twitter.com/statuses/public_timeline.json");
int statusCode = client.executeMethod(get);
if (statusCode == HttpStatus.SC_OK) {
... method.getResponseBody() ...
}
@Path("/students")
public class Students {
...
@Path("/students/{username}")
public class Student {
...
@Path("/teachers/{username: [a-zA-Z]}")
public class Teacher {
...
@Path("/students")
public class Students {
@GET
public String handleGet() { ... }
@POST
public String handlePost(String payload) { ... }
@PUT
public String handlePut(String payload) { ... }
@DELETE
public String handleDelete() { ... }
...
@Path("/student/{username}")
public class Student {
@GET
public String handleGet(@PathParam("username") String username) { ... }
...
@Path("/student/{username}")
public class Student {
@PUT
@Consumes("application/xml")
public String updateXML(String payload) { ... }
@PUT
@Consumes("application/json")
public String updateJSON(String payload) { ... }
...
@Path("/student/{username}")
public class Student {
@GET
@Produces("application/xml")
public String getXML() { ... }
@GET
@Produces("application/json")
public String getJSON() { ... }
...
<form action="users" method="POST">
Name: <input type="text" name="name"/>
Age: <input type="text" name="age"/>
Address: <input type="text" name="address"/>
</form>
@Path("/students")
public class Students {
@POST
@Consumes("application/x-www-form-urlencoded")
public void addUser(
@FormParam("name") String name,
@FormParam("age") String age,
@FormParam("address") String address) { ... }
...