论坛首页 Java企业应用论坛

10个类搞定无任何侵入的开放API服务

浏览 9580 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (11)
作者 正文
   发表时间:2012-06-02   最后修改:2012-06-03
1.简介
   看到了http://www.iteye.com/topic/1121252,
   以前也开发了个简单的开放api系统, 总共用了10个左右的类就可以搞定(使用spring mvc).现在把代码整理下发出.

2. 我们最重要的要求是:简单开发,简单使用, Service要写的干干净净,让开发API的coder在开发一个API的时候不用学习任何额外的东西, 而是在写一个普通的企业应用里面的Service(or Manager)代码, 下面就是开发一个API的步骤
  1)定义API接口

package ws.service;


import ws.annotation.HttpWebService;
import ws.annotation.Path;
import ws.service.impl.Hello;

@HttpWebService
public interface HelloService {


    @Path(value = "/heloWorld", paramNames = "name")
    Hello helloWorld2(String name);

    @Path(value = "/test")
    void test();

    @Path(value = "/exception")
    void exception();

}

  2.API实现
package ws.service.impl;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import ws.service.HelloService;

public class HelloServiceImpl implements HelloService {

    private static Log log = LogFactory.getLog(HelloServiceImpl.class);

    public HelloServiceImpl() {
        log.debug("Hello service init!!!");
    }


    @Override
    public Hello helloWorld2(String name) {
        return new Hello(name);
    }

    @Override
    public void test() {
       log.debug("test");
    }

    @Override
    public void exception() {
        if(1==1) {
            throw new RuntimeException("error");
        }
    }

}

  3.Spring配置
  
<bean id="hello" class="ws.service.impl.HelloServiceImpl"/>


这样我们就已经定义了3个API了.分别是 /heloWorld ,/test ,/exception

  下面看调用, 直接使用浏览器,







完整代码已经放在

https://github.com/jqq/rest

上面只是一个简单示例, 保证API的访问安全性代码问题稍后添加,

是实现方式添加interceptor.
1.授权校验
2.hmac校验(对称、非对称)
3.防重放攻击
4.超时控制
5.审计

实现方式参照
http://www.thebuzzmedia.com/designing-a-secure-rest-api-without-oauth-authentication/

未完待续.
  • 大小: 22.4 KB
  • 大小: 18.3 KB
  • 大小: 133.6 KB
   发表时间:2012-06-03  
https://github.com/jqq/rest 打不开

嫩这个题目真诱人
0 请登录后投票
   发表时间:2012-06-03  
更新 删除数据如何操作?
0 请登录后投票
   发表时间:2012-06-04   最后修改:2012-06-04
godson_2003 写道
更新 删除数据如何操作?



更新操作一般是这样的:

Server端:

 @Path(value="/update", paramNames = "hello")
 void update(Hello hello) ;


@Override
    public void update(Hello hello) {

        //update
        log.debug(hello.toString());

    }


Client端调用方式为:

http://localhost:8080/update.json?hello={name:2}


server端自动解析domain参数,转换为Object.
0 请登录后投票
   发表时间:2012-06-04  
godson_2003 写道
https://github.com/jqq/rest 打不开

嫩这个题目真诱人

谢谢关注,我能打开

https://github.com/jqq/rest
0 请登录后投票
   发表时间:2012-06-04  
传统的webservice又笨重又难用,楼主这个看起来相当简洁明了,期待继续完善。
0 请登录后投票
   发表时间:2012-06-04  
说说我的观点:
1. HelloService可以进一步拆成两个层次:api和service。拆分后自然就没有原先那么简洁了,但优点在于更灵活:service可以专注于业务逻辑,以service层作为业务逻辑的复用单元,内部代码和api层和ui都可以调用service层。
2. api层可以用java restful API做,譬如jersey
3. exception可以分内部的和外部的两类,内部exception包含了敏感信息不适合让客户端知道,应当转换成外部exception(譬如告诉客户端有internal error,再加个标识信息),而外部exception允许stacktrace。
0 请登录后投票
   发表时间:2012-06-04  
还是需要json lib 搞序列化..
0 请登录后投票
   发表时间:2012-06-05  
godson_2003 写道
更新 删除数据如何操作?


Of course you should use REST.
0 请登录后投票
   发表时间:2012-06-05  
guanlicome 写道
godson_2003 写道
更新 删除数据如何操作?


Of course you should use REST.

可能大家没有了解jqq的主要意义, 我也没有说的很清楚, jqq完成的功能和

http://www.iteye.com/topic/1121252 类似,只不过实现方式可能不一样,
大家也可以参看下

[url]http://www.thebuzzmedia.com/designing-a-secure-rest-api-without-oauth-authentication/ [/url]

总结下:就是用一个url请求实现业务逻辑,但是同时解决了验证身份,防网络篡改等安全功能.
designing-a-secure-rest-api-without-oauth-authentication


我最近工作比较忙,会尽快把代码,文档整理出来...
0 请登录后投票
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics