ActFramework 1.8.32 released - high quality Java Web application framework

1. ActFramework 1.8.32

ActFramework is a high quality Java Web application framework The latest 1.8.32 version brings 20 bug fixes and updates where there is concern..:

1.1 HTTP access through the CLI command # 1305

Users familiar with the Act are known to provide a number of built-in CLI commands Act, also provides a very convenient CLI command to create a mechanism if you need to create a user in the back end, you can just write code like this:

@PropertySpec("id")
@Command(name = "user.create", help = "create user")  
public User create(  
        @Required("specify user email") String email,  
        @Required("specify user password") char[] password,  
        @Optional("specify user role") Role role  
) {  
  User user = findByEmail(email);  
  badRequestIf(null != user, "email[%s] registered already", email);  
  user = new User(email);  
  user.setPassword(password);  
  user.role = null == role ? Role.SUPER_USER : Role.RESTRICTED_USER;  
  return save(user);  
}

We need to use this command in the background through the CLI service port telnet or nc other tools to access the server:

This mechanism is very convenient but the link CLI service port will have a problem in the development, is the server breaks the TCP link code update is triggered when the heat load, and therefore need to rebuild the debug command on the CLI service port, a little trouble. 1.8. 32, we bring the convenience of CLI Over Http service mechanism, allows developers only in the development through  /~/cmd to access the command:

So developers do not need to keep a link CLI port in the background at any time to debug CLI command via HTTP web page.

1.2 Enhanced SampleData API, allows you to specify to create Mock data when creating List / Set the number # 1301

ActFramework of SampleData Mock provides a powerful data generation API, in this version we have further enhanced this feature, you can specify the data entry Mock created.

Sample code:

public static class User {  
  public String firstName;  
  public String lastName;  
  public String email;  
}
@GetAction("users/mock")
public List<User> test() {  
  return SampleData.generateList(User.class, 7);  
}

test:

1.3 Access return Iterable types of data directly from the browser terminal, # 1298 in response to automatically generate HTML table form

这个增强的结果在上面已经显示出来了. 以前的版本从浏览器访问直接数据返回端点都是以 JSON 形式显示结果的. 看官可能会提问题了, 如果我通过 ajax 形式访问数据端点也会拿到 HTML table 形式的响应吗? 答案是在 ajax 请求中设定好 Accept=application/json ActFramework 会以 JSON 形式发回数据的. 拿刚刚上面的例子来测试:

较真的看官可能又要问了, 如果我就想在浏览器中看 JSON 数据而不是 HTML table 数据怎么办. 这个也不是问题, 使用 _accept=json 请求参数即可:

1.4 HTML-Table 增强 - 表头始终处于页面顶部

1.5 IStorageService 对错误处理的增强 #1295

以前当 IStorageService 发生存储项目未找到, 或者访问受限的错误, 都会导致发出 500 服务器错响应. 现在 ActFramework 能更好地处理这些错误情况, 当发生资源未找到时, 会处理为 404 响应. 访问受限会处理为 403 响应.

1.6 异步结果处理增强 - 使用最初指定的 Content-Type 来生成异步处理结果 #1286

在 ActFramework 中如果有工作是耗时较长的, 比如某些数据报表生成, 可以采用异步处理方式. 

示例代码:

@Async  
@ReportProgress  
@GetAction("/users/async")  
public List<User> simulateLongTimeOperation(ProgressGauge gauge) {  
  final int sz = 100;  
  List<User\> userList = new ArrayList<>(sz);  
  gauge.updateMaxHint(sz);  
  for (int i = 0; i < sz; ++i) {  
  $.sleep(50);  
  userList.add(SampleData.generate(User.class));  
  }  
  return userList;  
}

访问该接口:

以前的版本无论用那种请求类型访问最后都只能看到上面的结果 - JSON 形式生成的响应. 在这个版本里我们缓存了最初的请求响应类型, 并在最后生成结果响应的时候应用请求的响应类型. 下面是演示:

用 html-table 方式访问 /users/async:

用 xlsx 方式访问 /users/async:

2. Act-Beetl 1.7.2

更新 beetl 至 3.0.19.RELEASE

3. Act-BeetlSQL 1.8.2

更新 beetlsql 至 2.12.20.RELEASE

4. Act-Morphia 1.9.0

Act-morphia 是转为 ActFramework 应用程序设计的 MongoDB 访问库. v1.9.0 带来的改进有:

4.1 Dao.update API 改进:

下面的情况如果 firstName 为 null, 则会调用 mongodb 的 $unset 操作删掉 firstName 字段:

4.2 加载 Entity 的时候自动初始化集合类型字段

假设你有下面的 Model 类型:

@Entity("emp")
public class Employee extends MorphiaModel<Employee> {
    public String firstName;
    public String lastName;
    public List<AuditRecord> auditRecords;
}

从数据库中 load 一个 Employee 实例, 假设该记录没有 auditRecords 数据, 以前该字段会是 null, 现在则自动将 auditRecords 字段设置为空 List. 这样做的好处是避免对 auditRecord 字段进行操作的时候还需要进行空值检查.

5. Act-Excel 1.9.0, Act-Excel-java7 1.9.0

5.1 支持不同的输出主题

示例代码:

默认主题:

春意盎然:

五十度灰:

金秋:

5.2 提供帮助方法让应用自己控制 Excel 文档生成

群里有人提出需要定时生成 Excel 文档, 不希望还需要从 HTTP 走一遍, 于是将内部的逻辑抽取出来提供了下面的静态方法方便程序员使用:

ExcelDirectRender.generateExcelFile(Object data, File targetFile);

6. OSGL-Tool 1.24.0 

osgl-tool 是一套 Java 工具库, ActFramework 中大量使用了 osgl-tool 来简化开发. v1.24.0 版本带来一下改变:

6.1 UserAgent 使用 LFU Cache 来替代 HashMap #234

UserAgent 字串解析是一件耗时的工作. 因此我们总是希望将结果缓存下来. 以前的版本采用简单的 HashMap 来缓存 UserAgent 解析结果. 这个速度当然很快,  然而带来的麻烦是 UserAgent 的种类基本上是一个开发的数量, 根据 whatismybrowser 的统计, 有超过 2400 万的不同的 UserAgent 字串. 随着时间的推移, HashMap 的 UserAgent 缓存将吃光服务器上的堆空间.

这个版本中我们使用了 LFU (最低访问次数) 缓存来存储 1000 个 UserAgent 解析结果, 这样大多数常用的 UserAgent 会被缓存起来, 既满足了性能的要求, 也不会对服务器堆空间带来长期的压力.

6.2 Crypto 增加 RSA 方法 #233

在 Crypto 工具类上增加 RSA 的方法:

public static KeyPair generateKeyPair();
public static KeyPair generateKeyPair(int keysize);
public static String encryptRSA(String value, byte[] publicKey);
public static String encryptRSA(String value, String urlSafeBase64EncodedPublicKey);
public static String decryptRSA(String value, byte[] privateKey);
public static String decryptRSA(String value, String urlSafeBase64EncodedPrivateKey);

6.3 UserAgent - 支持 Microsoft Edge #230

6.4 添加 S.acronym(CharSequence) 静态方法

String a = S.acronym("OpenSourceGeneralLibrary"); // a = 'OSGL'

7. 总结

以上就是本次更新中值得关注的部分. 最后给自己的博客做一个友情链接, 请大家观赏一下 如何用不到 70 行 Java 代码撸一个简单的文件上传服务

Guess you like

Origin www.oschina.net/news/113849/actframework-1-8-32-released