-
-
Notifications
You must be signed in to change notification settings - Fork 0
endpoint
Roland Schuller edited this page Apr 4, 2019
·
1 revision
An Endpoint is the last Part of an Url.
Let's make an time Endpoint. It should return the Time in Hour,Minute,Second
First lets make an Object for that (Just POJO)
public class DateExport {
private final int hour;
private final int minute;
private final int second;
private final Date date;
public DateExport(Date date) {
this.date=date;
GregorianCalendar calendar=new GregorianCalendar();
calendar.setTime(date);
hour=calendar.get(GregorianCalendar.HOUR_OF_DAY);
minute=calendar.get(GregorianCalendar.MINUTE);
second=calendar.get(GregorianCalendar.SECOND);
}
public int getHour() {
return hour;
}
public int getMinute() {
return minute;
}
public int getSecond() {
return second;
}
}
Every getter will be shown in the json but not the fields. So if you want to show it in the json them make a Getter;
Now we have to make the Endpoint
public class TimeEndpoint extends GetEndpoint{
public TimeEndpoint(String endpointName) {
super(endpointName);
}
@Override
public void Call(Conversion conversion, Map<String, String> UrlParameter) {
conversion.getResponse().setData(new DateExport(new Date()));
}
}
Simple!
Last thing to do! Where should it be called?
RestHttpServer server=RestHttpServer.Start(3000); // Start the Server
server.getRootEndpoint().addRestEndpoint(new TimeEndpoint("time"));
Done!
call http://localhost:3000/time
{
"code":200,
"message":"The request sent by the client was successful.",
"generationMsSeconds":1.981296,
"data":{
"hour":15,
"minute":49,
"second":11
}
}