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中调用数据库存储过程的示例代码

《Java中调用数据库存储过程的示例代码》本文介绍Java通过JDBC调用数据库存储过程的方法,涵盖参数类型、执行步骤及数据库差异,需注意异常处理与资源管理,以优化性能并实现复杂业务逻辑,感兴趣的朋友... 目录一、存储过程概述二、Java调用存储过程的基本javascript步骤三、Java调用存储过程示

Visual Studio 2022 编译C++20代码的图文步骤

《VisualStudio2022编译C++20代码的图文步骤》在VisualStudio中启用C++20import功能,需设置语言标准为ISOC++20,开启扫描源查找模块依赖及实验性标... 默认创建Visual Studio桌面控制台项目代码包含C++20的import方法。右键项目的属性:

java向微信服务号发送消息的完整步骤实例

《java向微信服务号发送消息的完整步骤实例》:本文主要介绍java向微信服务号发送消息的相关资料,包括申请测试号获取appID/appsecret、关注公众号获取openID、配置消息模板及代码... 目录步骤1. 申请测试系统2. 公众号账号信息3. 关注测试号二维码4. 消息模板接口5. Java测试

MySQL数据库的内嵌函数和联合查询实例代码

《MySQL数据库的内嵌函数和联合查询实例代码》联合查询是一种将多个查询结果组合在一起的方法,通常使用UNION、UNIONALL、INTERSECT和EXCEPT关键字,下面:本文主要介绍MyS... 目录一.数据库的内嵌函数1.1聚合函数COUNT([DISTINCT] expr)SUM([DISTIN

Java实现自定义table宽高的示例代码

《Java实现自定义table宽高的示例代码》在桌面应用、管理系统乃至报表工具中,表格(JTable)作为最常用的数据展示组件,不仅承载对数据的增删改查,还需要配合布局与视觉需求,而JavaSwing... 目录一、项目背景详细介绍二、项目需求详细介绍三、相关技术详细介绍四、实现思路详细介绍五、完整实现代码

Go语言代码格式化的技巧分享

《Go语言代码格式化的技巧分享》在Go语言的开发过程中,代码格式化是一个看似细微却至关重要的环节,良好的代码格式化不仅能提升代码的可读性,还能促进团队协作,减少因代码风格差异引发的问题,Go在代码格式... 目录一、Go 语言代码格式化的重要性二、Go 语言代码格式化工具:gofmt 与 go fmt(一)

HTML5实现的移动端购物车自动结算功能示例代码

《HTML5实现的移动端购物车自动结算功能示例代码》本文介绍HTML5实现移动端购物车自动结算,通过WebStorage、事件监听、DOM操作等技术,确保实时更新与数据同步,优化性能及无障碍性,提升用... 目录1. 移动端购物车自动结算概述2. 数据存储与状态保存机制2.1 浏览器端的数据存储方式2.1.

基于 HTML5 Canvas 实现图片旋转与下载功能(完整代码展示)

《基于HTML5Canvas实现图片旋转与下载功能(完整代码展示)》本文将深入剖析一段基于HTML5Canvas的代码,该代码实现了图片的旋转(90度和180度)以及旋转后图片的下载... 目录一、引言二、html 结构分析三、css 样式分析四、JavaScript 功能实现一、引言在 Web 开发中,

Python如何去除图片干扰代码示例

《Python如何去除图片干扰代码示例》图片降噪是一个广泛应用于图像处理的技术,可以提高图像质量和相关应用的效果,:本文主要介绍Python如何去除图片干扰的相关资料,文中通过代码介绍的非常详细,... 目录一、噪声去除1. 高斯噪声(像素值正态分布扰动)2. 椒盐噪声(随机黑白像素点)3. 复杂噪声(如伪

Java Spring ApplicationEvent 代码示例解析

《JavaSpringApplicationEvent代码示例解析》本文解析了Spring事件机制,涵盖核心概念(发布-订阅/观察者模式)、代码实现(事件定义、发布、监听)及高级应用(异步处理、... 目录一、Spring 事件机制核心概念1. 事件驱动架构模型2. 核心组件二、代码示例解析1. 事件定义