扩展RestLet应用来创建和更新资源

十月 22, 2008 in Oracle 融合中间件

 

 

上一篇博文中学习了RestLet如何使用它在JDevloper 11g中创建一个简单的Restful服务,它只支持只读操作:GET请求,下面将添加创建和更改Note的功能。

 

一、创建新的资源

 

 

创建一个客户端页面来创建Notes

 

创建是通过HTTP请求的POST来实现,我们需要设计一个HTML页面来提交给我们的RESTful服务的资源链接URL,提交的页面包括了我们需要创建Note的相关数据,Restful服务将接收提交的请求,NotesResource资源类将接收Post请求,将其发送给/notes服务来创建一个Note。

需要保证这个页面不在我们的项目下,否则由于web.xml配置过滤无法运行,因此可以在任何其他目录下创建此静态HTML页面,如test.html 。

 

1
2
3
4
5
6
7
8
9
10
11
12
13
<form name="NoteCreator" action="http://127.0.0.1:7101/oracleseeker-restful/notes" method="post">
  <table cellspacing="2" cellpadding="3" width="100%" border="1">
    <tbody>
      <tr>
        <td width="28%">Name</td>
        <td width="72%"> </td>
      </tr><tr><td width="28%">Description</td>
        <td width="72%"> </td>
      </tr>
     </tbody>
    </table>
 
</form>

 

HTML静态页面的主要功能是提交一个表单执行一个POST方法,将表单数据发送给Notes服务

 

添加POST处理逻辑到NotesResource资源类中

 

首先需要在资源类NotesResource类的构造函数中设置属性,让资源类能够接受POST请求来修改资源

 

1
2
// Allow modifications of this resource via POST requests.   
setModifiable(true);

 

然后重载Resource类的acceptRepresentation方法,这个方法监控请求的内容,获取创建资源所需的信息,然后创建资源,将其添加到收集器中

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
    @Override
    public void acceptRepresentation(Representation entity) throws ResourceException {
        Form form = new Form(entity);
        String name = form.getFirstValue(&quot;name&quot;);
        String description = form.getFirstValue(&quot;description&quot;);
 
        getNotes().add(new Note(name, description, new Date()));
 
        // Set the response's status and entity
        getResponse().setStatus(Status.SUCCESS_CREATED);
        Representation rep =  new StringRepresentation(&quot;Note created&quot;, MediaType.TEXT_PLAIN);
        // Indicates where is located the new resource.        
        rep.setIdentifier(getRequest().getResourceRef().getIdentifier() + &quot;/&quot; + name);        
        getResponse().setEntity(rep);
    }

 

创建Note资源

 

运行restful.jsp页面用来发布Web服务到WebLogic,然后运行静态HTML文件

4.create_note

输入Note信息,点击Create按钮,然后会弹出一个窗口显示创建成功的消息。

 

修改URL,检测Note创建是否成功

5.create_complete 

 

 

二、更新现有的资源

 

要POST请求能够支持更新现有资源的功能,只要修改NotesResource资源来,根据请求的URL参数来进行资源定位并进行更新就可以了。

 

在资源类NotesResource类的构造函数中设置可修改资源,并获取URL中的资源参数

 

1
2
3
setModifiable(true); 
String noteName = (String)getRequest().getAttributes().get(&quot;noteName&quot;);
note = getTheNote(noteName);

 

重载acceptRepresentation() 方法,在方法中定位资源,并进行修改,如果没有找到资源,返回提示信息

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
    @Override
    public void acceptRepresentation(Representation entity) throws ResourceException {
        Form form = new Form(entity);
        Representation rep = null;
        if(note != null){
            String description = form.getFirstValue(&quot;description&quot;);            
            note.setDescription(description);            
            getResponse().setStatus(Status.SUCCESS_CREATED);            
            rep = new StringRepresentation(&quot;Note updated&quot;, MediaType.TEXT_PLAIN);            
            rep.setIdentifier(getRequest().getResourceRef().getIdentifier() + &quot;/&quot; + note.getName());
        }
        else{
            getResponse().setStatus(Status.CLIENT_ERROR_NOT_FOUND);                        
            rep = new StringRepresentation(&quot;Note could not be found&quot;, MediaType.TEXT_PLAIN);
        }
        getResponse().setEntity(rep);
    }

 

根据URL中的参数,找到资源,并对资源中的描述进行修改

 

修改HTML静态页面test.html ,添加Update按钮来提交资源的更新,如下列代码:

 

1
 

 

 

更新资源展示

 

还是和之前一样,先运行restful.jsp页面,再运行静态HTML文件

 

8.update_form

 

更新之后会有成功的消息返回。

6.update_success

 

 

输入URL检测更新是否成功

7.update_complete

 

应用扩展结束!

 

 

三、资源和链接

 

Extending the RestLet application with support for POST/PUT to create and update Resources by Lucas Jellema

http://technology.amis.nl/blog/3592/extending-the-restlet-application-with-support-for-postput-to-create-and-update-resources

 

下载本文的JDeveloper 11g应用:Download 扩展RestLet应用来创建和更新资源 Version 1

 

 

相关文章:

  1. JDeveloper 11g中利用RestLet开发RESTful服务
  2. 通过ADF Data Control 发布RESTFul 服务的资源
  3. 修改XML DB的HTTP端口

Leave a reply

You must be logged in to post a comment.