在 Rest 中使用路由器 API - java
Use router in Rest API - java
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;
}
}
以上代码无效。在 运行 服务器之后,当我打开 url http://localhost:8080/attendance/faculty/select
时,它无法工作。我怎样才能使这项工作?有人可以帮忙吗?
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;
}
}
}
错误:
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.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 -
这是我在刷新浏览器时遇到的错误。我不知道是什么导致了这个问题。
我认为您可以使事情变得更简单,而且您的应用程序并不是真正的 RESTful ;-) 我强烈认为以正确的方式使用 Restlet 将有助于解决您的问题。您似乎使用了方法 getQuery
而您没有查询字符串。此外,此类查询参数似乎不适用于这种情况。
在继续之前,我建议您看一下这个 link 来设计 Web API / RESTful 服务:https://templth.wordpress.com/2014/12/15/designing-a-web-api/.
以下是我对您在问题中提供的代码的评论:
您应该将一种资源用于列表资源,一种用于单个资源。所以我想要这样的东西:
@Override
public Restlet createInboundRoot() {
Router router = new Router(getContext());
router.attach("/attendance/faculty/{name}", FacultyServerResource.class);
router.attach("/attendance/faculty/", FacultyListServerResource.class);
return router;
}
在资源路径中使用操作名称(如 select
或 insert
)不是 RESTful。您应该利用现有的 HTTP 方法来满足您的需求。
令牌{name}
对应一个路径变量。这意味着附加的服务器资源将被称为任何值。例如,URL 像 /attendance/faculty/facultyName1
或 /attendance/faculty/facultyName2
将匹配。此外,Restlet 会自动设置属性内的值name
。对于 URL #1,名称是 facultyName1
,对于 URL #2,名称是 facultyName2
.
您提供的作为方法第一个参数的字符串 attach
可以看作是一种正则表达式。
您应该使用路径变量来指定加载您的教员的标准(教员姓名)。查看资源路径 /attendance/faculty/{name}
中的元素 {name}
。 Restlet 将允许您通过其 API 简单地获得此提示。所以我会按如下所述调整您的服务器资源的代码:
public class FacultyServerResource extends ServerResource {
@Get ("json")
public String present() throws Exception {
String facultyName = (String)getAttribute("name");
(...)
}
提醒一下,在注释 Get
中指定值 json
允许配置内容协商,并表示此方法仅在客户端想要接收 JSON内容
您应该在服务器资源中处理关于 returned 内容的 bean。您可以通过您的方法 present
简单地 return 它。根据您的代码,我将创建一个这样的 bean:
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
(...)
}
并更新方法 present
:
@Get ("json")
public Faculty present() throws Exception {
(...)
Faculty faculty = new Faculty();
faculty.setName("...");
faculty.setPost("...");
(...)
return faculty;
}
要使用这种方法,您只需在 class 路径中添加扩展 org.restlet.ext.jackson
(及其依赖项)。
我在服务器资源 class 中不明白的是为什么要使用方法 getQuery
。后者用于从查询参数中获取提示,并且通常必须与 URL 一起使用,如下所示:/attendance/faculty/facultyName?param1=something¶m2=somethingelse
。您将获得如下参数值:
String param1Value = getQuery().getValues("param1");
// or
String param2Value = getQueryValue("param2");
在您的情况下,这将为空,因为您没有查询字符串/查询参数。我不明白你的查询参数 data
.
的用途
您应该考虑的另一件事是使用连接池,因为创建到数据库的 JDBC 连接在 Web 环境中效率不高。此外,您不能限制打开的连接数。有 DBCP 或 C3P0 等工具提供此类功能。
希望对您有所帮助,有什么不明白的可以随时问我!
蒂埃里
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;
}
}
以上代码无效。在 运行 服务器之后,当我打开 url http://localhost:8080/attendance/faculty/select
时,它无法工作。我怎样才能使这项工作?有人可以帮忙吗?
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;
}
}
}
错误:
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.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 -
这是我在刷新浏览器时遇到的错误。我不知道是什么导致了这个问题。
我认为您可以使事情变得更简单,而且您的应用程序并不是真正的 RESTful ;-) 我强烈认为以正确的方式使用 Restlet 将有助于解决您的问题。您似乎使用了方法 getQuery
而您没有查询字符串。此外,此类查询参数似乎不适用于这种情况。
在继续之前,我建议您看一下这个 link 来设计 Web API / RESTful 服务:https://templth.wordpress.com/2014/12/15/designing-a-web-api/.
以下是我对您在问题中提供的代码的评论:
您应该将一种资源用于列表资源,一种用于单个资源。所以我想要这样的东西:
@Override public Restlet createInboundRoot() { Router router = new Router(getContext()); router.attach("/attendance/faculty/{name}", FacultyServerResource.class); router.attach("/attendance/faculty/", FacultyListServerResource.class); return router; }
在资源路径中使用操作名称(如
select
或insert
)不是 RESTful。您应该利用现有的 HTTP 方法来满足您的需求。令牌
{name}
对应一个路径变量。这意味着附加的服务器资源将被称为任何值。例如,URL 像/attendance/faculty/facultyName1
或/attendance/faculty/facultyName2
将匹配。此外,Restlet 会自动设置属性内的值name
。对于 URL #1,名称是facultyName1
,对于 URL #2,名称是facultyName2
.您提供的作为方法第一个参数的字符串
attach
可以看作是一种正则表达式。您应该使用路径变量来指定加载您的教员的标准(教员姓名)。查看资源路径
/attendance/faculty/{name}
中的元素{name}
。 Restlet 将允许您通过其 API 简单地获得此提示。所以我会按如下所述调整您的服务器资源的代码:public class FacultyServerResource extends ServerResource { @Get ("json") public String present() throws Exception { String facultyName = (String)getAttribute("name"); (...) }
提醒一下,在注释
Get
中指定值json
允许配置内容协商,并表示此方法仅在客户端想要接收 JSON内容您应该在服务器资源中处理关于 returned 内容的 bean。您可以通过您的方法
present
简单地 return 它。根据您的代码,我将创建一个这样的 bean: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 (...) }
并更新方法
present
:@Get ("json") public Faculty present() throws Exception { (...) Faculty faculty = new Faculty(); faculty.setName("..."); faculty.setPost("..."); (...) return faculty; }
要使用这种方法,您只需在 class 路径中添加扩展
org.restlet.ext.jackson
(及其依赖项)。我在服务器资源 class 中不明白的是为什么要使用方法
getQuery
。后者用于从查询参数中获取提示,并且通常必须与 URL 一起使用,如下所示:/attendance/faculty/facultyName?param1=something¶m2=somethingelse
。您将获得如下参数值:String param1Value = getQuery().getValues("param1"); // or String param2Value = getQueryValue("param2");
在您的情况下,这将为空,因为您没有查询字符串/查询参数。我不明白你的查询参数
data
. 的用途
您应该考虑的另一件事是使用连接池,因为创建到数据库的 JDBC 连接在 Web 环境中效率不高。此外,您不能限制打开的连接数。有 DBCP 或 C3P0 等工具提供此类功能。
希望对您有所帮助,有什么不明白的可以随时问我! 蒂埃里