基于Maven web项目,用SSM框架(Spring、SpringMVC、Mybatis)搭建restful API协议框架,支持四种HTTP协议方法:POST、GET、PUT、DELETE等操作。
客户端通过HTTP协议方法进行URL访问,获得相应的接口服务或json数据包。
Web端可以通过jQuery解析获得的Json数据包(可能需要注意安全性,如:客户端加入token,将API_token、请求时间、请求方法名、请求客户端信息等等的信息加密作为token,在服务器端验证token是否正确,与请求时间的间隔是否在有效期内),IOS和Andriod也可以采用第三方库来解析Json数据包。
先从网上找一个SSM的模板,然后配置restful API的相关设置。
1、pom.xml中加入依赖
<dependency>
<groupId>ognl</groupId>
<artifactId>ognl</artifactId>
<version>2.6.9</version>
</dependency>
<dependency>
<groupId>net.sf.json-lib</groupId>
<artifactId>json-lib</artifactId>
<version>2.4</version>
<classifier>jdk15</classifier>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>1.9.13</version>
</dependency>
2、springmvc-servlet.xml中加入对json的支持
<!-- 输出对象转JSON支持 -->
<bean id="jsonConverter"
class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"></bean>
<bean
class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="messageConverters">
<list>
<ref bean="stringConverter" />
<ref bean="jsonConverter" />
</list>
</property>
</bean>
3、编写异常处理
StatusCode状态码domain:
public class StatusCode {
private int statuscode;
private String msg;
public StatusCode(int statuscode, String msg) {
this.statuscode = statuscode;
this.msg = msg;
}
public int getStatuscode() {
return statuscode;
}
public void setStatuscode(int statuscode) {
this.statuscode = statuscode;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
}
定义一个异常类,使用和其对应的状态码:
import com.XXXXX.domain.StatusCode;
public class ParamsErrorException extends Exception {
//无效的参数
//错误码:10000 错误信息:invalid parameters
StatusCode sc = new StatusCode(10000, "invalid parameters");
public ParamsErrorException(){
}
public ParamsErrorException(String msg){
sc.setMsg(msg);
}
public StatusCode getSc() {
return sc;
}
}
提供给API用户的状态错误码:
响应状态码 | 信息 | 备注 |
---|---|---|
200 | OK | 请求成功 |
10000 | invalid parameters | 无效的参数 |
注意:
这里的状态码和http中的状态码不一样,这是放在json包中的,目的是告诉API调用者出现了什么错误。
4、编写Controller
import net.sf.json.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import com.xxxxx.domain.StatusCode;
import com.xxxxx.domain.Test;
import com.xxxxx.exception.ParamsErrorException;
import com.xxxxx.exception.QueryFailedException;
import com.xxxxx.service.TestService;
@Controller
public class TestController{
@Resource
TestService testService;
final private StatusCode successcode = new StatusCode(200, "OK");
@RequestMapping(value = "/test", method = RequestMethod.GET)
public @ResponseBody
JSONObject responseGET(String id) {
Test test = null;
JSONObject json = new JSONObject();
try{
if(id==null) throw new ParamsErrorException();
test = testService.get(id);
if(test==null) throw new QueryFailedException();
json.putAll(json.fromObject(successcode));
json.put("test",test);
return json;
}catch(ParamsErrorException e){
return json.fromObject(e.getSc());
}catch (QueryFailedException e) {
return json.fromObject(e.getSc());
}
}
}
5、Postman测试
点击前往Postman官网下载,Postman提供很多版本的选择合适的下载。
http请求(GET方法)URL地址:
http://xxx/test?id=0b117007-6de6-4763-b332-cb109a9e7bfe返回Json包:
{
“msg”: “OK”,
“statuscode”: 200,
“test”: {
“createTime”: “2017-6-9”,
“id”: “6157f400-97a2-4e34-a55f-0aaf433f3578”,
“remark”: “test”,
“username”: “test”
}
}请求成功,返回如下信息
响应状态码 响应信息
200 OK如没有id信息将返回如下信息:
响应状态码 响应信息
10000 invalid paramters