Reputation: 593
package firstREST;
import org.restlet.Application;
import org.restlet.Component;
import org.restlet.Restlet;
import org.restlet.data.Protocol;
import org.restlet.routing.Router;
public class Faculty extends Application {
public static void main(String[] args) {
Component comp = new Component();
comp.getServers().add(Protocol.HTTP, 8080);
Application app = new Faculty();
comp.getDefaultHost().attach(app);
try {
comp.start();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Override
public Restlet createInboundRoot() {
Router router = new Router(getContext());
router.attach("/attendance/faculty/select", Faculty_Get.class);
router.attach("/attendance/faculty/insert", Faculty_Insert.class);
return router;
}
}
the above code is not working. after running the server when i open a url http://localhost:8080/attendance/faculty/select
it won't work. How can i make this work? anyone can help?
Faculty_Get Class:
package firstREST;
import org.restlet.resource.Get;
import org.restlet.resource.ServerResource;
import org.json.JSONArray;
import org.json.JSONObject;
import java.sql.*;
public class Faculty_Get extends ServerResource {
@Get ("json")
public String present( String name ) throws Exception {
// Values.
String getName = null;
String getPost = null;
String getCourse = null;
String getDepartment = null;
String getPresents = null;
String getAbsents = null;
// Get values.
String jSonString = getQuery().getValues( "data" );
// Decode jSon.
JSONArray mJsonArray = new JSONArray( jSonString );
JSONObject mJsonObject = new JSONObject();
for ( int i = 0; i < mJsonArray.length(); i++ ) {
mJsonObject = mJsonArray.getJSONObject(i);
getName = mJsonObject.getString( "name" );
}
// Database.
try
{
Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection myconn = DriverManager.getConnection("jdbc:mysql://localhost:3306/attendance", "root", "");
PreparedStatement ps = myconn.prepareStatement("SELECT * FROM faculty where name = '" + getName + "'");
ResultSet rs = ps.executeQuery();
while( rs.next() )
{
getName = rs.getString( "name" );
getPost = rs.getString( "post" );
getCourse = rs.getString( "course" );
getDepartment = rs.getString( "department" );
getPresents = rs.getString( "presents" );
getAbsents = rs.getString( "absents" );
}
return "name="+getName+"&post="+getPost+"&course="+getCourse+"&department="+getDepartment+"&presents="+getPresents+"&absents="+getAbsents;
}
catch(Exception e)
{
throw e;
}
}
}
Error:
May 18, 2015 6:17:10 PM org.restlet.resource.UniformResource doCatch
WARNING: Exception or error caught in resource
java.lang.NullPointerException
at java.io.StringReader.<init>(Unknown Source)
at org.json.JSONTokener.<init>(JSONTokener.java:83)
at org.json.JSONArray.<init>(JSONArray.java:145)
at firstREST.Faculty_Get.present(Faculty_Get.java:24)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.restlet.resource.ServerResource.doHandle(ServerResource.java:449)
at org.restlet.resource.ServerResource.get(ServerResource.java:648)
at org.restlet.resource.ServerResource.doHandle(ServerResource.java:530)
at org.restlet.resource.ServerResource.doNegotiatedHandle(ServerResource.java:590)
at org.restlet.resource.ServerResource.doConditionalHandle(ServerResource.java:302)
at org.restlet.resource.ServerResource.handle(ServerResource.java:849)
at org.restlet.resource.Finder.handle(Finder.java:513)
at org.restlet.routing.Filter.doHandle(Filter.java:159)
at org.restlet.routing.Filter.handle(Filter.java:206)
at org.restlet.routing.Router.doHandle(Router.java:500)
at org.restlet.routing.Router.handle(Router.java:740)
at org.restlet.routing.Filter.doHandle(Filter.java:159)
at org.restlet.routing.Filter.handle(Filter.java:206)
at org.restlet.routing.Filter.doHandle(Filter.java:159)
at org.restlet.routing.Filter.handle(Filter.java:206)
at org.restlet.routing.Filter.doHandle(Filter.java:159)
at org.restlet.engine.application.StatusFilter.doHandle(StatusFilter.java:154)
at org.restlet.routing.Filter.handle(Filter.java:206)
at org.restlet.routing.Filter.doHandle(Filter.java:159)
at org.restlet.routing.Filter.handle(Filter.java:206)
at org.restlet.engine.ChainHelper.handle(ChainHelper.java:114)
at org.restlet.engine.application.ApplicationHelper.handle(ApplicationHelper.java:75)
at org.restlet.Application.handle(Application.java:391)
at org.restlet.routing.Filter.doHandle(Filter.java:159)
at org.restlet.routing.Filter.handle(Filter.java:206)
at org.restlet.routing.Router.doHandle(Router.java:500)
at org.restlet.routing.Router.handle(Router.java:740)
at org.restlet.routing.Filter.doHandle(Filter.java:159)
at org.restlet.routing.Filter.handle(Filter.java:206)
at org.restlet.routing.Router.doHandle(Router.java:500)
at org.restlet.routing.Router.handle(Router.java:740)
at org.restlet.routing.Filter.doHandle(Filter.java:159)
at org.restlet.engine.application.StatusFilter.doHandle(StatusFilter.java:154)
at org.restlet.routing.Filter.handle(Filter.java:206)
at org.restlet.routing.Filter.doHandle(Filter.java:159)
at org.restlet.routing.Filter.handle(Filter.java:206)
at org.restlet.engine.ChainHelper.handle(ChainHelper.java:114)
at org.restlet.Component.handle(Component.java:391)
at org.restlet.Server.handle(Server.java:491)
at org.restlet.engine.http.connector.BaseServerHelper.handle(BaseServerHelper.java:161)
at org.restlet.engine.http.connector.BaseServerHelper.handleInbound(BaseServerHelper.java:170)
at org.restlet.engine.http.connector.BaseHelper.handleNextInbound(BaseHelper.java:421)
at org.restlet.engine.http.connector.Connection.readMessages(Connection.java:698)
at org.restlet.engine.http.connector.Controller$2.run(Controller.java:98)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
May 18, 2015 6:17:10 PM org.restlet.engine.log.LogFilter afterHandle
INFO: 2015-05-18 18:17:10 127.0.0.1 - - 8080 GET /attendance/faculty/select - 500 486 0 30 http://localhost:8080 Mozilla/5.0 (Windows NT 6.1; rv:40.0) Gecko/20100101 Firefox/40.0 -
This is the error i am getting when i refresh my browser. I don't know what's causing this issue.
Upvotes: 1
Views: 5352
Reputation: 202216
I think that you could make things much simpler and moreover your application isn't really RESTful ;-) I strongly think that using Restlet the right way will contribute to fix your issue. It seems that you use the method getQuery
whereas you don't have query string. Moreover such query parameters don't seem to apply in such case.
Before going further, I would recommend you to have a look at this link to design a Web API / RESTful service: https://templth.wordpress.com/2014/12/15/designing-a-web-api/.
Here are the comments I would have regarding the code you provided in your questions:
You should use one resource for list resource and one for single resource. So I would something like that:
@Override
public Restlet createInboundRoot() {
Router router = new Router(getContext());
router.attach("/attendance/faculty/{name}", FacultyServerResource.class);
router.attach("/attendance/faculty/", FacultyListServerResource.class);
return router;
}
It's not RESTful to use operation names (like select
or insert
) within the resource path. You should leverage the existing HTTP methods for your needs.
The token {name}
corresponds to a path variable. This means that the attached server resource will be called whatever the value. For example, URLs like /attendance/faculty/facultyName1
or /attendance/faculty/facultyName2
will match. In addition, Restlet will automatically set the value within the attribute name
. For URL #1, name is facultyName1
and for URL #2, name is facultyName2
.
The string you provide as first parameter of the method attach
can be seen as a kind of regular expression.
You should use a path variable to specify the criterion (the faculty name) to load your faculty. See the element {name}
within the resource path /attendance/faculty/{name}
. Restlet will allow you to get simply this hint with its API. So I would adapt the code of your server resource as described below:
public class FacultyServerResource extends ServerResource {
@Get ("json")
public String present() throws Exception {
String facultyName = (String)getAttribute("name");
(...)
}
As a reminder, specifying the value json
in the annotation Get
allows to configuration content negotiation and says that this method will only be used if the client wants to receive a JSON content.
You should work on bean within the server resource regarding the returned content. You can simply return it from your method present
. From your code, I would create a bean like that:
public class Faculty {
private String name;
private String post;
private String course;
private String department;
private List<String> presents;
private List<String> absents;
// Getters and setters
(...)
}
and update the method present
:
@Get ("json")
public Faculty present() throws Exception {
(...)
Faculty faculty = new Faculty();
faculty.setName("...");
faculty.setPost("...");
(...)
return faculty;
}
To make work such approach, you simply need to add the extension org.restlet.ext.jackson
(and its dependencies) within your classpath.
Something I don't understand in the server resource class is why you use the method getQuery
. The latter is made to get hints from query parameters and must be typically used with URLs like this: /attendance/faculty/facultyName?param1=something¶m2=somethingelse
. You will get the parameter values like this:
String param1Value = getQuery().getValues("param1");
// or
String param2Value = getQueryValue("param2");
In you case, this will be null since you don't have query string / query parameters. I don't understand the purpose of your query parameter data
.
Another thing that you should take into account is to use a connection pool since creating a JDBC connection to the database isn't efficient within Web environment. Moreover you can't limit the number of opened connection. There are tools like DBCP or C3P0 that provides such feature.
Hope it will help you and feel free to ask me if something isn't clear! Thierry
Upvotes: 3