question

BreathtakingCoconut-9661 avatar image
0 Votes"
BreathtakingCoconut-9661 Suspended asked BobJohnson-6874 edited

How to create a Server and an Application in Restlet

---------------------------------------- Server --------------------------------------------
public class Server {
public static void main(String[] args) throws Exception {
Component component = new Component();
component.getServers().add(Protocol.HTTP, 9999);
component.getDefaultHost().attach("/api", new ServerApp());
component.start();
}
}

------------------------------------ Application -----------------------------------------
public class ServerApp extends Application {
@Override
public Restlet createInboundRoot() {
Router r = new Router();
r.attach("/School", SchoolResource.class);
r.attach("/School/Class/{classId}", ClassroomResource.class);
r.attach("/School/Class/{classId}/Student/{studentId}", StudentResource.class);
return r;
}
}

not-supported
· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

A Client Method:

ClientResource clientResource = new ClientResource("http://localhost:9999/api/School/Class/"+classId+"/Student/"+studentId);
IStudent resource = clientResource.wrap(IStudent.class);
return resource.getStudent();

0 Votes 0 ·

Full Client:
public class Client {
public static void main(String[] args){
Client client1 = new Client();
EmployeeList empList = client1.getEmployeeList();
System.out.println(client1.getEmployee(0).getLastname());
}
public EmployeeList getEmployeeList(){
ClientResource cr = new ClientResource("http://localhost:9999/api/employee");
EmployeeResource er = cr.wrap(EmployeeResource.class);
return er.read(); //return EmployeeList
}
public void newEmployeeList(EmployeeList el){
ClientResource cr = new ClientResource("http://localhost:9999/api/employee");
EmployeeResource er = cr.wrap(EmployeeResource.class);
er.create(el);
}
public Employee getEmployee(int index){
ClientResource cr = new ClientResource("http://localhost:9999/api/employee/"+index);
ISingleEmpRes sm = cr.wrap(ISingleEmpRes.class);
return sm.read();
}
}

0 Votes 0 ·

0 Answers