SerfJ REST代码实例

2024-02-14 08:08
文章标签 代码 实例 rest serfj

本文主要是介绍SerfJ REST代码实例,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

SerfJ 是一个最简单的框架用来开发Java的REST的Web应用。可帮助你开发优雅的MVC架构的应用,使用惯例重于配置的思路,无需配置文件和注解。

 

[代码] web.xml

01<servlet>
02    <servlet-name>RestServlet</servlet-name>
03    <servlet-class>net.sf.serfj.RestServlet</servlet-class>
04    <load-on-startup>5</load-on-startup>
05</servlet>
06  
07<servlet-mapping>
08    <servlet-name>RestServlet</servlet-name>
09    <url-pattern>/banks/*</url-pattern>
10</servlet-mapping>
11  
12<servlet-mapping>
13    <servlet-name>RestServlet</servlet-name>
14    <url-pattern>/accounts/*</url-pattern>
15</servlet-mapping>

[代码] serfj.properties

1# Main package where looking for classes (controllers, serializers)
2main.package=net.sf.serfj.test

[代码] Bank.java

01public class Bank extends RestController {
02    @GET
03    public void index() {
04        // By default, this action redirects to index.jsp (or index.html or index.htm)
05    }
06  
07    @GET
08    public void show() {
09        // Gets ID from URL /banks/1
10        String id = this.getId("bank");
11         
12        // Gets account's ID from URL /banks/1/accounts/2
13        String accountId = this.getId("account");
14  
15        // Gets the account
16        Account account = // Code that gets the account 2 from bank 1
17         
18        // Put account into the request so the page will be able to use it
19        this.addObject2Request("account", account);
20          
21        // By default, this action redirects to show.jsp (or show.html or show.htm)
22    }
23  
24    @GET
25    public void newResource() {
26        // By default, this action redirects to new.jsp (or new.html or new.htm)
27    }
28  
29    @GET
30    public void edit() {
31        // By default, this action redirects to edit.jsp (or edit.html or edit.htm)
32    }
33  
34    @POST
35    public void create() {
36        // By default, this action redirects to create.jsp (or create.html or create.htm)
37    }
38  
39    @PUT
40    public void update() {
41        // Gets bank's ID
42        String id = this.getId("bank");
43  
44        Bank bank = // Code that gets the bank object     
45  
46        // Gets new name for the bank
47        String name = this.getStringParam("name");
48         
49        // Updating the bank
50        // ... Code that updates the bank's information
51  
52        // By default, this action redirects to update.jsp (or update.html or update.htm)
53    }
54  
55    @DELETE
56    public void delete() {
57        // By default, this action redirects to delete.jsp (or delete.html or delete.htm)
58    }
59  
60    @GET
61    public void someAction() {
62        // By default, this action redirects to someAction.jsp (or someAction.html or someAction.htm)
63    }
64}

[代码] Account.java

view source print ?
01public class Account extends RestController {
02    @GET
03    public void index() {
04        // By default, this action redirects to index.jsp (or index.html or index.htm)
05    }
06  
07    @GET
08    public void show() {
09        // By default, this action redirects to show.jsp (or show.html or show.htm)
10    }
11  
12    @GET
13    public void newResource() {
14        // By default, this action redirects to new.jsp (or new.html or new.htm)
15    }
16  
17    @GET
18    public void edit() {
19        // By default, this action redirects to edit.jsp (or edit.html or edit.htm)
20    }
21  
22    @POST
23    public void create() {
24        // By default, this action redirects to create.jsp (or create.html or create.htm)
25        // But I want to render another page!... easy
26        this.renderPage("mypage.jsp");
27    }
28  
29    @PUT
30    public void update() {
31        // By default, this action redirects to update.jsp (or update.html or update.htm)
32        // But I want to render another page... from another controller!... easy
33        this.renderPage("bank""another_page.jsp");
34    }
35  
36    @DELETE
37    public void delete() {
38        // By default, this action redirects to delete.jsp (or delete.html or delete.htm)
39        // Well, if something happens, I want to redirect to mypage.jsp
40        if (somethingHappens) {
41            this.renderPage("mypage.jsp");
42        else {
43            // Default page
44            this.renderPage();
45        }
46    }
47  
48    /**
49     * If this method is called as /accounts/1/accountBalance.xml, then the balance object will
50     * be serialized as an XML, whereas if it's called as /accounts/1/accountBalance.json, the
51     * object will be serialized as a JSON object.
52     */
53    @POST
54    public Balance accountBalance() {
55       // Gets account's Id
56       String id = this.getId("account");
57         
58       // Calculate balance
59       BalanceManager manager = new BalanceManager();
60       Balance balance = manager.getBalance(id);
61       this.serialize(balance);
62    }
63}

这篇关于SerfJ REST代码实例的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/707943

相关文章

Java调用DeepSeek API的最佳实践及详细代码示例

《Java调用DeepSeekAPI的最佳实践及详细代码示例》:本文主要介绍如何使用Java调用DeepSeekAPI,包括获取API密钥、添加HTTP客户端依赖、创建HTTP请求、处理响应、... 目录1. 获取API密钥2. 添加HTTP客户端依赖3. 创建HTTP请求4. 处理响应5. 错误处理6.

使用 sql-research-assistant进行 SQL 数据库研究的实战指南(代码实现演示)

《使用sql-research-assistant进行SQL数据库研究的实战指南(代码实现演示)》本文介绍了sql-research-assistant工具,该工具基于LangChain框架,集... 目录技术背景介绍核心原理解析代码实现演示安装和配置项目集成LangSmith 配置(可选)启动服务应用场景

前端原生js实现拖拽排课效果实例

《前端原生js实现拖拽排课效果实例》:本文主要介绍如何实现一个简单的课程表拖拽功能,通过HTML、CSS和JavaScript的配合,我们实现了课程项的拖拽、放置和显示功能,文中通过实例代码介绍的... 目录1. 效果展示2. 效果分析2.1 关键点2.2 实现方法3. 代码实现3.1 html部分3.2

Python中顺序结构和循环结构示例代码

《Python中顺序结构和循环结构示例代码》:本文主要介绍Python中的条件语句和循环语句,条件语句用于根据条件执行不同的代码块,循环语句用于重复执行一段代码,文章还详细说明了range函数的使... 目录一、条件语句(1)条件语句的定义(2)条件语句的语法(a)单分支 if(b)双分支 if-else(

MySQL数据库函数之JSON_EXTRACT示例代码

《MySQL数据库函数之JSON_EXTRACT示例代码》:本文主要介绍MySQL数据库函数之JSON_EXTRACT的相关资料,JSON_EXTRACT()函数用于从JSON文档中提取值,支持对... 目录前言基本语法路径表达式示例示例 1: 提取简单值示例 2: 提取嵌套值示例 3: 提取数组中的值注意

CSS3中使用flex和grid实现等高元素布局的示例代码

《CSS3中使用flex和grid实现等高元素布局的示例代码》:本文主要介绍了使用CSS3中的Flexbox和Grid布局实现等高元素布局的方法,通过简单的两列实现、每行放置3列以及全部代码的展示,展示了这两种布局方式的实现细节和效果,详细内容请阅读本文,希望能对你有所帮助... 过往的实现方法是使用浮动加

JAVA调用Deepseek的api完成基本对话简单代码示例

《JAVA调用Deepseek的api完成基本对话简单代码示例》:本文主要介绍JAVA调用Deepseek的api完成基本对话的相关资料,文中详细讲解了如何获取DeepSeekAPI密钥、添加H... 获取API密钥首先,从DeepSeek平台获取API密钥,用于身份验证。添加HTTP客户端依赖使用Jav

Java实现状态模式的示例代码

《Java实现状态模式的示例代码》状态模式是一种行为型设计模式,允许对象根据其内部状态改变行为,本文主要介绍了Java实现状态模式的示例代码,文中通过示例代码介绍的非常详细,需要的朋友们下面随着小编来... 目录一、简介1、定义2、状态模式的结构二、Java实现案例1、电灯开关状态案例2、番茄工作法状态案例

nginx-rtmp-module模块实现视频点播的示例代码

《nginx-rtmp-module模块实现视频点播的示例代码》本文主要介绍了nginx-rtmp-module模块实现视频点播,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习... 目录预置条件Nginx点播基本配置点播远程文件指定多个播放位置参考预置条件配置点播服务器 192.

CSS自定义浏览器滚动条样式完整代码

《CSS自定义浏览器滚动条样式完整代码》:本文主要介绍了如何使用CSS自定义浏览器滚动条的样式,包括隐藏滚动条的角落、设置滚动条的基本样式、轨道样式和滑块样式,并提供了完整的CSS代码示例,通过这些技巧,你可以为你的网站添加个性化的滚动条样式,从而提升用户体验,详细内容请阅读本文,希望能对你有所帮助...