请指点下Restlet2.0 post方法无法调用的问题
最近公司做一个项目接口协议用到rest,所以下载了个restlet2.0框架来学习。在应用的appliaciton 配置了2个路由地址。在soucre写了2个post方法
但是只能进第一个("/sns/basicinfosyn")无法进第二个("/queueinfosyn"),并且访问时的第二个地址也路由到第一个去了。
是一个sourece只能有1个post方法,还是我写错了呢?请各位同道中人指点下。
麻烦熟悉restlet框架的兄弟帮忙看看。
代码如下:
application
public class UnitviewSycApplication extends Application{
@Override
public Restlet createInboundRoot() {
Router router = new Router();
router.attach("/sns/basicinfosyn",UnitviewSycSource.class);
router.attach("/queueinfosyn",UnitviewSycSource.class);
return router;
}
}
Source类代码
public class UnitviewSycSource extends ServerResource{
@Override
protected void doInit() throws ResourceException {
// TODO Auto-generated method stub
super.doInit();
}
@Post("basicinfosyn")
public Representation sycWblog(Representation entity){
Representation re = null;
JSONObject result = null;
try {
Form form = new Form(entity);
System.out.println("收到同步信息");
} catch (Exception e) {
e.printStackTrace();
}
re = new StringRepresentation(result.toString(),MediaType.APPLICATION_JSON);
re.setCharacterSet(new CharacterSet("UTF-8"));
return re;
}
@Post("queueinfosyn")
public Representation sycQueueinfosyn(Representation entity){
Representation re = null;
JSONObject result = null;
try {
Form form = new Form(entity);
System.out.println("收到排队同步信息");
} catch (Exception e) {
e.printStackTrace();
}
re = new StringRepresentation(result.toString(),MediaType.APPLICATION_JSON);
re.setCharacterSet(new CharacterSet("UTF-8"));
return re;
}
}
配置文件restlet.xml
<?xml version="1.0" encoding="UTF-8"?>
<component xmlns="http://www.restlet.org/schemas/2.0/Component"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.restlet.org/schemas/2.0/Component">
<!--
<client protocol="CLAP" />
<client protocol="FILE" />
<client protocols="HTTP HTTPS" />
<server protocols="HTTP HTTPS" port="6080"/>
-->
<defaultHost>
<attach uriPattern="/customers" targetClass="com.rest.test.server.study2.application.CustomerApplication" />
<attach uriPattern="/orders" targetClass="com.rest.test.server.study2.application.OrderApplication" />
<attach uriPattern="/magw" targetClass="com.rest.combat.unitview.application.UnitviewSycApplication" />
<attach uriPattern="/external" targetClass="com.rest.combat.magw.application.MagwQueryApplication" />
</defaultHost>
</component>
客户端调用:
public class UnitviewClient {
public static void basicinfosyn(String url){
ClientResource client = new ClientResource(url+"/magw/sns/basicinfosyn");
try {
Form form = new Form();
form.add("vcid", "2");
form.add("dialogid","DID0001");
form.add("flowno","FL0001");
form.add("skillno","2");
form.add("priority","222");
String result = client.post(form.getWebRepresentation()).getText();
System.out.println(result);
} catch (Exception e) {
e.printStackTrace();
}
}
public static void queueinfosyn(String url){
ClientResource client = new ClientResource(url+"/magw/queueinfosyn");
try {
Form form = new Form();
form.add("dialogid", "DID0001");
form.add("flowno","FL0001");
form.add("queuebegintm",new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(System.currentTimeMillis()));
form.add("queueendtm",new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(System.currentTimeMillis()));
form.add("queueresult","1");
form.add("operid","61113");
String result = client.post(form.getWebRepresentation()).getText();
System.out.println(result);
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
String url = "http://133.37.147.144:6080/restStudy";
//UnitviewClient.basicinfosyn(url);
UnitviewClient.queueinfosyn(url);
}
}
调用UnitviewClient.basicinfosyn(url); 是没问题的。 但是调用UnitviewClient.queueinfosyn(url); 就不行了。并且被路由到第一个方法去了。 --------------------编程问答-------------------- 你可以把两个Post方法放到两个不同类中,这样就可以路由到了
补充:Java , Java EE